简介

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 default

RedissonClient 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 way

longObject.compareAndSet(3, 401); // async way

longObject.compareAndSetAsync(3, 401);

RedissonReactiveClient client = Redisson.createReactive(config);

RAtomicLongReactive longObject = client.getAtomicLong('myLong'); // reactive way

longObject.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 method lock.lock(); // Lock time-to-live support // releases lock automatically after 10 seconds // if unlock method not invoked lock.lock(10, TimeUnit.SECONDS); // Wait for 100 seconds and automatically unlock it after 10 seconds boolean 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 method

rwlock.readLock().lock(); // or 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); // or 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); // or boolean 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 JVM

RCountDownLatch 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 JVM

RTopic<SomeObject> topic = redisson.getTopic("anyTopic");

long clientsReceivedMessage = topic.publish(new SomeObject());

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

});

分布式集合

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

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

http://blog.csdn.net/csujiangyu/article/details/51005342

redisson笔记的更多相关文章

  1. Redisson实现Redis分布式锁的底层原理

    一.写在前面 现在面试,一般都会聊聊分布式系统这块的东西.通常面试官都会从服务框架(Spring Cloud.Dubbo)聊起,一路聊到分布式事务.分布式锁.ZooKeeper等知识.所以咱们这篇文章 ...

  2. Redisson 分布式锁源码 01:可重入锁加锁

    前言 相信小伙伴都是使用分布式服务,那一定绕不开分布式服务中数据并发更新问题! 单系统很容易想到 Java 的各种锁,像 synchronize.ReentrantLock 等等等,那分布式系统如何处 ...

  3. Redisson 分布式锁实现之源码篇 → 为什么推荐用 Redisson 客户端

    开心一刻 一男人站在楼顶准备跳楼,楼下有个劝解员拿个喇叭准备劝解 劝解员:兄弟,别跳 跳楼人:我不想活了 劝解员:你想想你媳妇 跳楼人:媳妇跟人跑了 劝解员:你还有兄弟 跳楼人:就是跟我兄弟跑的 劝解 ...

  4. git-简单流程(学习笔记)

    这是阅读廖雪峰的官方网站的笔记,用于自己以后回看 1.进入项目文件夹 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file ...

  5. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  6. SQL Server技术内幕笔记合集

    SQL Server技术内幕笔记合集 发这一篇文章主要是方便大家找到我的笔记入口,方便大家o(∩_∩)o Microsoft SQL Server 6.5 技术内幕 笔记http://www.cnbl ...

  7. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  8. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  9. NET Core-学习笔记(三)

    这里将要和大家分享的是学习总结第三篇:首先感慨一下这周跟随netcore官网学习是遇到的一些问题: a.官网的英文版教程使用的部分nuget包和我当时安装的最新包版本不一致,所以没法按照教材上给出的列 ...

随机推荐

  1. RocketMQ 拉取消息-通信模块

    首先看server端:class NettyRemotingServer extends NettyRemotingAbstract implements RemotingServer 下面这个实现了 ...

  2. minic 动作句型处理

    #include "lex_define.h" enum keywords_type//代表一些关键字 { loop_for=,//代表for关键字 loop_while,//代表 ...

  3. EffectiveJava(29)优先考虑类型安全的异构容器

    当你的泛型集合需要更多的灵活性,你可以将键进行参数化而不是将容器进行参数化.然后将参数化的键提交给容器,来插入或者获取值.用泛型系统来确保值得类型与它的键相符. 我们创建一个Favorite类来模拟这 ...

  4. hdu1700 Points on Cycle (数学)

    Problem Description There is a cycle with its center on the origin. Now give you a point on the cycl ...

  5. 运用Unity结合PolicyInjection实现拦截器[结合操作日志实例]

    上一篇文章我们通过Unity自身Unity.InterceptionExtension.IInterceptionBehavior实现一个有系统关异常日志记录:解决代码中到处充满的异常记录的代码: 本 ...

  6. bin和sbin区别

    据说这个目录结构是沿袭unix的,不大清楚. bin是binary的缩写,是可执行的二进制文件./bin里面一般是基本的,大家都要用的工具:sbin里面的s是system的意思,是供system ad ...

  7. html热点区域

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 06-编写Hibernate API编写访问数据库的代码,使用Junit进行测试

    用到的注解: @Test:测试方法 @Before:初始化方法. @After:是否资源. 先执行Befere,然后执行Test,最后执行After. 第一步:新建一个Junit目录. 第二步:取名 ...

  9. No suitable driver found for jdbc:mysql://localhost:3306/hibernate_basic

    Struts Problem Report Struts has detected an unhandled exception: Messages: No suitable driver found ...

  10. mysql课程记录

    thread_pool可以使用Percona的版本和Mariadb的版本  都是支持的  主从切换是根据HA的方式,TDDL(Taobao Distribute Data Layer) 的方式的话,推 ...