redisson简单介绍(*)

一.什么是redisson

Redisson 是一个高级的分布式协调Redis客服端,能帮助用户在分布式环境中轻松实现一些Java的对象。Redisson、Jedis、Lettuce 是三个不同的操作 Redis 的客户端,Jedis、Lettuce 的 API 更侧重对 Reids 数据库的 CRUD(增删改查),而 Redisson API 侧重于分布式开发。

二.支持Redis多种连接模式

1.集群模式

    Config config = new Config();
	config.useClusterServers()
    .setScanInterval(2000) // cluster state scan interval in milliseconds
    .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);

2.单例模式

    RedissonClient redisson = Redisson.create();
    Config config = new Config();
	config.useSingleServer().setAddress("myRedisServer:6379");
    RedissonClient redisson = Redisson.create(config);

3.哨兵模式

    Config config = new Config();
	config.useSentinelServers()
    .setMasterName("mymaster")
    .addSentinelAddress("redis://127.0.0.1:26389", "redis://127.0.0.1:26379")
    .addSentinelAddress("redis://127.0.0.1:26319");
    RedissonClient redisson = Redisson.create(config);

4.主从模式

    Config config = new Config();
	config.useMasterSlaveServers()
	.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);

三.分布式相关工具

1.支持使用的分布式对象:
包含通用对象桶(Object Bucket)、二进制流(Binary Stream)、地理空间对象桶(Geospatial Bucket)、BitSet、原子整长形(AtomicLong)、原子双精度浮点数(AtomicDouble)、话题(订阅分发)、模糊话题、布隆过滤器(Bloom Filter)、基数估计算法(HyperLogLog)

(1)分布式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));

(2)分布式BitSet

    RBitSet set = redisson.getBitSet("simpleBitset");
    set.set(0, true);
    set.set(1812, false);
    set.clear(0);
    set.addAsync("e");
    set.xor("anotherBitset");

(3)分布式Lock

    Redisson redisson = Redisson.create();
    RLock lock = redisson.getLock("anyLock");
	lock.lock();
	lock.lock(10, TimeUnit.SECONDS);
    boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
    lock.unlock();

(4)分布式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();

(5)分布式ReadWriteLock

    RReadWriteLock rwlock = redisson.getLock("anyRWLock");

    rwlock.readLock().lock();
    rwlock.writeLock().lock();

    // Lock time-to-live support
    // releases lock automatically after 10 seconds
    // if unlock method not invoked
    rwlock.readLock().lock(10, TimeUnit.SECONDS);
    rwlock.writeLock().lock(10, TimeUnit.SECONDS);

    // Wait for 100 seconds and automatically unlock it after 10 seconds
    boolean res = rwlock.readLock().tryLock(100, 10, TimeUnit.SECONDS);
    boolean res = rwlock.writeLock().tryLock(100, 10, TimeUnit.SECONDS);
    ....
    
    lock.unlock();

(6)分布式Semaphore

   RSemaphore semaphore = redisson.getSemaphore("semaphore");
    semaphore.acquire();
    semaphore.acquire(23);
    semaphore.tryAcquire();
    semaphore.tryAcquire(23, TimeUnit.SECONDS);
    semaphore.release(10);
    semaphore.release();

(7)分布式AtomicLong

    RAtomicLong atomicLong = redisson.getAtomicLong("myAtomicLong");
    atomicLong.set(3);
    atomicLong.incrementAndGet();
    atomicLong.get();

(8)分布式AtomicDouble

    RAtomicDouble atomicDouble = redisson.getAtomicDouble("myAtomicDouble");
    atomicDouble.set(2.81);
    atomicDouble.addAndGet(4.11);
    atomicDouble.get();

(9)分布式CountDownLatch

    RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
    latch.trySetCount(1);
    latch.await();
    // in other thread or other JVM
    RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
	latch.countDown();

(10)分布式Topic

    RTopic<SomeObject> topic = redisson.getTopic("anyTopic");
	topic.addListener(new MessageListener<SomeObject>() {
        @Override
        public void onMessage(String channel, SomeObject message) {
            //...
        }
    });
    // in other thread or JVM
    RTopic<SomeObject> topic = redisson.getTopic("anyTopic");
    long clientsReceivedMessage = topic.publish(new SomeObject());

(11)分布式Topic patttern

    // subscribe to all topics by `topic1.*` pattern
    RPatternTopic<Message> topic1 = redisson.getPatternTopic("topic1.*");
    int listenerId = topic1.addListener(new PatternMessageListener<Message>() {
        @Override
        public void onMessage(String pattern, String channel, Message msg) {
            Assert.fail();
        }
    });

2.分布式集合
包含映射(Map)、多值映射(Multimap)、集(Set)、有序集(SortedSet)、计分排序集(ScoredSortedSet)、字典排序集(LexSortedSet)、列表(List)、列队(Queue)、双端队列(Deque)、阻塞队列(Blocking Queue)、有界阻塞列队(Bounded Blocking Queue)、阻塞双端列队(Blocking Deque)、阻塞公平列队(Blocking Fair Queue)、延迟列队(Delayed Queue)、优先队列(Priority Queue)、优先双端队列(Priority Deque)
(1)分布式Map

   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");

(2)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 seconds
map.put("key1", new SomeObject(), 10, TimeUnit.MINUTES, 10, TimeUnit.SECONDS);
// ttl = 3 seconds
map.putIfAbsent("key2", new SomeObject(), 3, TimeUnit.SECONDS);
// ttl = 40 seconds, maxIdleTime = 10 seconds
map.putIfAbsent("key2", new SomeObject(), 40, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);

(3)分布式Set
​ 除此之外还有,还支持Set eviction, SortedSet, ScoredSortedSet, LexSortedSet

RSet<SomeObject> set = redisson.getSet("anySet");
set.add(new SomeObject());
set.remove(new SomeObject());

(4)分布式List

RList<SomeObject> list = redisson.getList("anyList");
list.add(new SomeObject());
list.get(0);
list.remove(new SomeObject());

(5)分布式Blocking Queue
还支持Queue, Deque, Blocking Deque

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);

(6)具体服务结果可以参数类
RedissonClient

3.分布式锁(Lock)和同步器(Synchronizer)
包含可重入锁(Reentrant Lock)公平锁(Fair Lock)联锁(MultiLock)红锁(RedLock)读写锁(ReadWriteLock)信号量(Semaphore)可过期性信号量(PermitExpirableSemaphore)闭锁(CountDownLatch)。

4.分布式服务
包含分布式远程服务(Remote Service)、分布式实时对象(Live Object)服务、分布式执行服务(Executor Service)、分布式调度任务服务(Scheduler Service)、分布式映射归纳服务(MapReduce)

5.额外功能
包含对Redis节点的操作、复杂多维对象结构和对象引用的支持、命令的批量执行、脚本执行、底层Redis客户端

四.WiKi

https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95.
https://www.javadoc.io/doc/org.redisson/redisson/3.6.5/org/redisson/api/RFuture.html.

五.参考

https://blog.csdn.net/u010963948/article/details/79240050.
https://blog.csdn.net/zilong_zilong/article/details/78252037.
https://zhuanlan.zhihu.com/p/135864820.

六.Redisson基本配置参考

http://www.voidcc.com/redisson/redisson-common-configuration

slaveAddresses是一个数组

你可能感兴趣的:(分布式微服务,redis,数据库,缓存)