Redisson底层采用的是Netty 框架。支持Redis 2.8以上版本,支持Java1.6+以上版本。 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),从而让使用者能够将精力更集中地放在处理业务逻辑上。
SpringBoot集成Redisson 1、pom中maven配置
1 2 3 4 5 <dependency > <groupId > org.redisson</groupId > <artifactId > redisson-spring-boot-starter</artifactId > <version > 3.12.3</version > </dependency >
2、yml配置集成Redisson
1 2 3 4 spring: #redisson 配置信息 redis: redisson: config: classpath:config/SingleServerConfig.yml
config/SingleServerConfig.yml配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 singleServerConfig: idleConnectionTimeout: 10000 pingTimeout: 1000 connectTimeout: 10000 timeout: 3000 retryAttempts: 3 retryInterval: 1500 reconnectionTimeout: 3000 failedAttempts: 3 password: passwd subscriptionsPerConnection: 5 clientName: "redisGo" address: "redis://domian.com:6379" subscriptionConnectionMinimumIdleSize: 1 subscriptionConnectionPoolSize: 50 connectionMinimumIdleSize: 32 connectionPoolSize: 64 database: 0 dnsMonitoringInterval: 5000 threads: 0 nettyThreads: 0 codec: !<org.redisson.codec.JsonJacksonCodec> {} transportMode: "NIO"
请参考:https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95
3、redisson订阅实战
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Autowired private RedissonClient redissonClient; RTopic rTopic = redissonClient.getTopic("RTopicTest" , new SerializationCodec()); JSONObject msg = new JSONObject(); msg.put("msg" ,"订阅测试" ); rTopic.publish(msg); RTopic rTopic = redissonClient.getTopic("RTopicTest" ,new SerializationCodec()); rTopic.addListener(JSONObject.class ,new MessageListener <JSONObject >() { @Override public void onMessage (CharSequence charSequence, JSONObject msg) { System.out.println("MSG:" msg.get("msg" )); } });
4、redisson之分布式锁:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 redissonClient.getBucket("MY_LOCK_VALUES" ).set("1111111" ); RLock lock = redissonClient.getLock("myLock" ); boolean res = lock.tryLock(100 , 10 , TimeUnit.SECONDS);String result = "" ; if (res) { try { result = redissonClient.getBucket("MY_LOCK_VALUES" ).get().toString(); } finally { lock.unlock(); } }