分布锁框架Redisson
- Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid)。它不仅提供了一系列的分布式的Java常用对象,还提供了许多分布式服务。其中包括(BitSet, Set, Multimap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish / Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service, Scheduler service) Redisson提供了使用Redis的最简单和最便捷的方法。Redisson的宗旨是促进使用者对Redis的关注分离(Separation of Concern),从而让使用者能够将精力更集中地放在处理业务逻辑上。
程序化配置方法
Redisson程序化的配置方法是通过构建Config对象实例来实现的。例如
1 2 3 4
|
Config config = new Config(); config.setTransportMode(TransportMode.EPOLL); //可以用"rediss://"来启用SSL连接 config.useClusterServers().addNodeAddress("redis://127.0.0.1:7181");
|
文件方式配置
Redisson既可以通过用户提供的JSON或YAML格式的文本文件来配置,也可以通过含有Redisson专有命名空间的,Spring框架格式的XML文本文件来配置。
1 2
|
Config config = Config.fromJSON(new File("config-file.json")); RedissonClient redisson = Redisson.create(config);
|
也通过调用config.fromYAML方法并指定一个File实例来实现读取YAML格式的配置:
1 2
|
Config config = Config.fromYAML(new File("config-file.yaml")); RedissonClient redisson = Redisson.create(config);
|
集群模式
集群模式除了适用于Redis集群环境,也适用于任何云计算服务商提供的集群模式,例如AWS ElastiCache集群版、Azure Redis Cache和阿里云(Aliyun)的云数据库Redis版。
程序化配置集群的用法:
1 2 3 4 5 6 7 8
|
Config config = new Config(); config.useClusterServers() .setScanInterval(2000) // 集群状态扫描间隔时间,单位是毫秒 //可以用"rediss://"来启用SSL连接 .addNodeAddress("redis://127.0.0.1:7000", "redis://127.0.0.1:7001") .addNodeAddress("redis://127.0.0.1:7002");
RedissonClient redisson = Redisson.create(config);
|
主从模式
1 2 3 4 5 6 7 8
|
Config config = new Config(); config.useMasterSlaveServers() //可以用"rediss://"来启用SSL连接 .setMasterAddress("redis://127.0.0.1:6379") .addSlaveAddress("redis://127.0.0.1:6389", "redis://127.0.0.1:6332", "redis://127.0.0.1:6419") .addSlaveAddress("redis://127.0.0.1:6399");
RedissonClient redisson = Redisson.create(config);
|
RedissonLock分布式锁的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
public class RedissonLock { private static RedissonClient redissonClient; static{ Config config = new Config(); config.useSingleServer().setAddress("redis://127.0.0.1:6379").setPassword(null); //config.useSingleServer().setAddress("redis://192.168.188.4:6380").setPassword("ty3foGTrNiKi"); redissonClient = Redisson.create(config); }
public static RedissonClient getRedisson(){ return redissonClient; }
public static void main(String[] args) throws InterruptedException{
RLock fairLock = getRedisson().getLock("TEST_KEY"); System.out.println(fairLock.toString()); //fairLock.lock(); // 尝试加锁,最多等待10秒,上锁以后10秒自动解锁 boolean res = fairLock.tryLock(10, 10, TimeUnit.SECONDS); System.out.println(res); fairLock.unlock();
//有界阻塞队列 RBoundedBlockingQueue<JSONObject> queue = getRedisson().getBoundedBlockingQueue("anyQueue"); // 如果初始容量(边界)设定成功则返回`真(true)`, // 如果初始容量(边界)已近存在则返回`假(false)`。 System.out.println(queue.trySetCapacity(10)); JSONObject o=new JSONObject(); o.put("name", 1); if(!queue.contains(o)){ queue.offer(o); }
JSONObject o2=new JSONObject(); o2.put("name", 2); // 此时容量已满,下面代码将会被阻塞,直到有空闲为止。
if(!queue.contains(o2)){ queue.offer(o2); }
// 获取但不移除此队列的头;如果此队列为空,则返回 null。 JSONObject obj = queue.peek(); //获取并移除此队列的头部,在指定的等待时间前等待可用的元素(如果有必要)。 JSONObject ob = queue.poll(10, TimeUnit.MINUTES);
//获取并移除此队列的头,如果此队列为空,则返回 null。 Iterator<JSONObject> iterator=queue.iterator(); while (iterator.hasNext()){ JSONObject i =iterator.next(); System.out.println(i.toJSONString()); iterator.remove(); } while(queue.size()>0){ JSONObject obs = queue.poll(); System.out.println(obs.toJSONString()); }
JSONObject someObj = queue.poll(); System.out.println(someObj.toJSONString()); } }
|
Table of Content | 目录
Overview
概述
Configuration
配置方法
Operations execution
程序接口调用方式
Data serialization
数据序列化
Data partitioning (sharding)
单个集合数据分片(Sharding)
Distributed objects
分布式对象
Distributed collections
分布式集合
Distributed locks and synchronizers
分布式锁和同步器
Distributed services
分布式服务
Additional features
额外功能
Redis commands mapping
Redis命令和Redisson对象匹配列表
Standalone node
独立节点模式
Tools
工具
Integration with frameworks
第三方框架整合
Dependency list
项目依赖列表
FAQ
评论