Redis分布式客户端-Redisson介绍
来源:互联网 发布:银行数据安全 编辑:程序博客网 时间:2024/05/21 09:54
简介
Redisson - 是一个高级的分布式协调Redis客服端,能帮助用户在分布式环境中轻松实现一些Java的对象 (Bloom filter, BitSet, Set, SetMultimap, ScoredSortedSet, SortedSet, Map, ConcurrentMap, List, ListMultimap, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, ReadWriteLock, AtomicLong, CountDownLatch, Publish / Subscribe, HyperLogLog)。
支持Redis多种连接模式
集群模式
Config config = new Config();config.useClusterServers() .setScanInterval(2000) // cluster state scan interval in milliseconds .addNodeAddress("127.0.0.1:7000", "127.0.0.1:7001") .addNodeAddress("127.0.0.1:7002");RedissonClient redisson = Redisson.create(config);
单例模式
// connects to 127.0.0.1:6379 by defaultRedissonClient redisson = Redisson.create();Config config = new Config();config.useSingleServer().setAddress("myredisserver:6379");RedissonClient redisson = Redisson.create(config);
哨兵模式
Config config = new Config();config.useSentinelServers() .setMasterName("mymaster") .addSentinelAddress("127.0.0.1:26389", "127.0.0.1:26379") .addSentinelAddress("127.0.0.1:26319");RedissonClient redisson = Redisson.create(config);
主从模式
Config config = new Config();config.useMasterSlaveServers() .setMasterAddress("127.0.0.1:6379") .addSlaveAddress("127.0.0.1:6389", "127.0.0.1:6332", "127.0.0.1:6419") .addSlaveAddress("127.0.0.1:6399");RedissonClient redisson = Redisson.create(config);
操作执行
Redisson支持自动重试策略,默认是重试3次,间隔为1000ms。除了支持同步操作外,还支持异步方式和响应方式。
RedissonClient client = Redisson.create(config);RAtomicLong longObject = client.getAtomicLong('myLong');// sync waylongObject.compareAndSet(3, 401);// async waylongObject.compareAndSetAsync(3, 401);RedissonReactiveClient client = Redisson.createReactive(config);RAtomicLongReactive longObject = client.getAtomicLong('myLong');// reactive waylongObject.compareAndSet(3, 401);
多种序列号方式
Codec class name Description
org.redisson.codec.JsonJacksonCodec Jackson JSON codec. Default codec
org.redisson.codec.CborJacksonCodec CBOR binary json codec
org.redisson.codec.MsgPackJacksonCodec MsgPack binary json codec
org.redisson.codec.KryoCodec Kryo binary codec
org.redisson.codec.SerializationCodec JDK Serialization codec
org.redisson.codec.FstCodec FST up to 10 times faster and 100% JDK Serialization compatible codec
org.redisson.codec.LZ4Codec LZ4 compression codec
org.redisson.codec.SnappyCodec Snappy compression codec
org.redisson.client.codec.StringCodec String codec
org.redisson.client.codec.LongCodec Long codec
分布式对象
分布式Object
RBucket<AnyObject> bucket = redisson.getBucket("anyObject");bucket.set(new AnyObject(1));AnyObject obj = bucket.get();bucket.trySet(new AnyObject(3));bucket.compareAndSet(new AnyObject(4), new AnyObject(5));bucket.getAndSet(new AnyObject(6));
分布式BitSet
RBitSet set = redisson.getBitSet("simpleBitset");set.set(0, true);set.set(1812, false);set.clear(0);set.addAsync("e");set.xor("anotherBitset");
分布式Lock
Redisson redisson = Redisson.create();RLock lock = redisson.getLock("anyLock");// Most familiar locking methodlock.lock();// Lock time-to-live support// releases lock automatically after 10 seconds// if unlock method not invokedlock.lock(10, TimeUnit.SECONDS);// Wait for 100 seconds and automatically unlock it after 10 secondsboolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);...lock.unlock();
分布式MultiLock
RLock lock1 = redissonInstance1.getLock("lock1");RLock lock2 = redissonInstance2.getLock("lock2");RLock lock3 = redissonInstance3.getLock("lock3");RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);lock.lock();
分布式ReadWriteLock
RReadWriteLock rwlock = redisson.getLock("anyRWLock");// Most familiar locking methodrwlock.readLock().lock();// orrwlock.writeLock().lock();// Lock time-to-live support// releases lock automatically after 10 seconds// if unlock method not invokedrwlock.readLock().lock(10, TimeUnit.SECONDS);// orrwlock.writeLock().lock(10, TimeUnit.SECONDS);// Wait for 100 seconds and automatically unlock it after 10 secondsboolean res = rwlock.readLock().tryLock(100, 10, TimeUnit.SECONDS);// orboolean res = rwlock.writeLock().tryLock(100, 10, TimeUnit.SECONDS);...lock.unlock();
分布式Semaphore
RSemaphore semaphore = redisson.getSemaphore("semaphore");semaphore.acquire();semaphore.acquire(23);semaphore.tryAcquire();semaphore.tryAcquire(23, TimeUnit.SECONDS);semaphore.release(10);semaphore.release();
分布式AtomicLong
RAtomicLong atomicLong = redisson.getAtomicLong("myAtomicLong");atomicLong.set(3);atomicLong.incrementAndGet();atomicLong.get();
分布式AtomicDouble
RAtomicDouble atomicDouble = redisson.getAtomicDouble("myAtomicDouble");atomicDouble.set(2.81);atomicDouble.addAndGet(4.11);atomicDouble.get();
分布式CountDownLatch
RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");latch.trySetCount(1);latch.await();// in other thread or other JVMRCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");latch.countDown();
Topic
RTopic<SomeObject> topic = redisson.getTopic("anyTopic");topic.addListener(new MessageListener<SomeObject>() { @Override public void onMessage(String channel, SomeObject message) { //... }});// in other thread or JVMRTopic<SomeObject> topic = redisson.getTopic("anyTopic");long clientsReceivedMessage = topic.publish(new SomeObject());
Topic patttern
// subscribe to all topics by `topic1.*` patternRPatternTopic<Message> topic1 = redisson.getPatternTopic("topic1.*");int listenerId = topic1.addListener(new PatternMessageListener<Message>() { @Override public void onMessage(String pattern, String channel, Message msg) { Assert.fail(); }});
分布式集合
分布式Map
除此之外,还支持Multimap,这里不列出
RMap<String, SomeObject> map = redisson.getMap("anyMap");SomeObject prevObject = map.put("123", new SomeObject());SomeObject currentObject = map.putIfAbsent("323", new SomeObject());SomeObject obj = map.remove("123");map.fastPut("321", new SomeObject());map.fastRemove("321");Future<SomeObject> putAsyncFuture = map.putAsync("321");Future<Void> fastPutAsyncFuture = map.fastPutAsync("321");map.fastPutAsync("321", new SomeObject());map.fastRemoveAsync("321");
Map eviction
现在Redis没有过期清空Map中的某个entry的功能,只能是清空Map所有的entry。Redission提供了这种功能。
RMapCache<String, SomeObject> map = redisson.getMapCache("anyMap");// ttl = 10 minutes, map.put("key1", new SomeObject(), 10, TimeUnit.MINUTES);// ttl = 10 minutes, maxIdleTime = 10 secondsmap.put("key1", new SomeObject(), 10, TimeUnit.MINUTES, 10, TimeUnit.SECONDS);// ttl = 3 secondsmap.putIfAbsent("key2", new SomeObject(), 3, TimeUnit.SECONDS);// ttl = 40 seconds, maxIdleTime = 10 secondsmap.putIfAbsent("key2", new SomeObject(), 40, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
分布式Set
RSet<SomeObject> set = redisson.getSet("anySet");set.add(new SomeObject());set.remove(new SomeObject());
除此之外还有,还支持Set eviction, SortedSet, ScoredSortedSet, LexSortedSet
分布式List
RList<SomeObject> list = redisson.getList("anyList");list.add(new SomeObject());list.get(0);list.remove(new SomeObject());
分布式Blocking Queue
RBlockingQueue<SomeObject> queue = redisson.getBlockingQueue("anyQueue");queue.offer(new SomeObject());SomeObject obj = queue.peek();SomeObject someObj = queue.poll();SomeObject ob = queue.poll(10, TimeUnit.MINUTES);
除此之外,还支持Queue, Deque, Blocking Deque
其他功能
执行批量命令
RBatch batch = redisson.createBatch();batch.getMap("test").fastPutAsync("1", "2");batch.getMap("test").fastPutAsync("2", "3");batch.getMap("test").putAsync("2", "5");batch.getAtomicLongAsync("counter").incrementAndGetAsync();batch.getAtomicLongAsync("counter").incrementAndGetAsync();List<?> res = batch.execute();
- Redis分布式客户端-Redisson介绍
- Redis分布式客户端-Redisson介绍
- Redis分布式客户端-Redisson介绍
- Redis客户端Redisson中分布式锁RLock实现
- redis客户端 Redisson
- redis客户端redisson实战
- Redis实现分布式锁全局锁—Redis客户端Redisson中分布式锁RLock实现
- 基于Redis的分布式框架Redisson
- redis分布式锁redisson(下)
- 基于Redis实现分布式锁-Redisson使用及源码分析
- 基于Redis实现分布式锁,Redisson使用及源码分析
- 基于Redis实现分布式锁,Redisson使用及源码分析
- 基于Redis实现分布式锁,Redisson使用及源码分析
- 基于Redis实现分布式锁,Redisson使用及源码分析
- 通过Redisson实现基于redis的分布式锁
- java基于redis客户端redisson的RPC远程服务调用
- Java之——redis并发读写锁,使用Redisson实现分布式锁
- Java之——redis并发读写锁,使用Redisson实现分布式锁
- 当当网的内部框架开源策略案例分享
- JVM介绍
- spring mvc 的 resources 注解 用来处理静态文件
- Android之网络编程
- 积分图像
- Redis分布式客户端-Redisson介绍
- Emacs 自带输入法
- win7-64bit 安装Python3.5
- 【Android】SlidingMenu属性详解
- java中File类的使用方法
- CoordinatorLayout、Toolbar、CollapsingToolbarLayout、AppBarLayout和NestedScrollView组合滑动折叠或拉伸Toolbar的demo
- “Microsoft Office Professional Plus 2010在安装中出错”解决办法
- cmake和make
- codeforces #345 (Div. 1) D. Zip-line (线段树+最长上升子序列)