1 概述
Redisson实现了分布式和可扩展的Java数据结构,功能较为简单,不支持字符串操作,不支持排序、事务、管道、分区等Redis特性。Redisson的宗旨是促进使用者对Redis的关注分离,从而让使用者能够将精力更集中地放在处理业务逻辑上。
2 特点
- Redisson中的方法则是进行比较高的抽象,每个方法调用可能进行了一个或多个Redis方法调用。
- Redisson使用非阻塞的I/O和基于Netty框架的事件驱动的通信层,其方法调用是异步的。Redisson的API是线程安全的,所以可以操作单个Redisson连接来完成各种操作。
- Redisson不仅提供了一系列的分布式Java常用对象,基本可以与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可提供更便捷的方法。
3 第三方框架整合
- Redisson提供了和Spring框架的各项特性类似的,以Spring XML的命名空间的方式配置RedissonClient实例和它所支持的所有对象和服务;
- Redisson完整的实现了Spring框架里的缓存机制;
- Redisson在Redis的基础上实现了Java缓存标准规范;
- Redisson为Apache Tomcat集群提供了基于Redis的非黏性会话管理功能。该功能支持Apache Tomcat的6、7和8版。
- Redisson还提供了Spring Session会话管理器的实现。
4 spring boot 整合 Redisson
pom
org.redisson redisson 3.10.6
properties
spring: redisson: enabled: true cluster: false nodes: x.x.x.x:x password: xxxx
redisson配置类RedissonConfig
package com.yx.redis.redisson; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.ClusterServersConfig; import org.redisson.config.Config; import org.redisson.config.SingleServerConfig; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.util.ObjectUtils; /** * @Auther: yx * @Date: 2019-04-30 15:55 * @Description: RedissonConfig */ @Configuration @Conditional(RedissonCondition.class) @EnableConfigurationProperties({RedissonProperties.class}) @Slf4j public class RedissonConfig { @Bean public RedissonProperties getRedissonProperties() { return new RedissonProperties(); } /** * 单机模式自动装配 * @return */ @Bean @ConditionalOnProperty(name="spring.redisson.cluster", havingValue = "false") public RedissonClient getSingleRedisson() { Config config = new Config(); RedissonProperties redissonProperties = getRedissonProperties(); String nodes = redissonProperties.getNodes(); if (StringUtils.isEmpty(nodes)) { throw new RuntimeException("nodes is not allow empty! please check your redis config"); } SingleServerConfig serverConfig = config.useSingleServer() .setAddress("redis://" + nodes) .setTimeout(redissonProperties.getTimeout()) .setConnectionPoolSize(redissonProperties.getConnectionPoolSize()) .setConnectionMinimumIdleSize(redissonProperties.getConnectionMinimumIdleSize()); if(StringUtils.isNotBlank(redissonProperties.getPassword())) { serverConfig.setPassword(redissonProperties.getPassword()); } log.info("init redisson success .... "); return Redisson.create(config); } @Bean @ConditionalOnProperty(name="spring.redisson.cluster", havingValue = "true") public RedissonClient getClusterRedisson(){ RedissonProperties redissonProperties = getRedissonProperties(); String[] nodes = redissonProperties.getNodes().split(","); if (ObjectUtils.isEmpty(nodes)) { throw new RuntimeException("nodes is not allow empty! please check your redis config"); } for(int i=0;i<nodes.length;i++){ nodes[i] = "redis://"+nodes[i]; } RedissonClient redisson = null; Config config = new Config(); ClusterServersConfig clusterServersConfig = config.useClusterServers() .setScanInterval(2000) .setTimeout(redissonProperties.getTimeout()) .addNodeAddress(nodes); if(StringUtils.isNotBlank(redissonProperties.getPassword())) { clusterServersConfig.setPassword(redissonProperties.getPassword()); } redisson = Redisson.create(config); log.info("init redisson cluster success .... "); return redisson; } }
redisson配置类RedissonCondition
package com.yx.redis.redisson; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.util.ObjectUtils; /** * @Auther: yx * @Date: 2019-04-30 15:56 * @Description: RedissionCondition */ public class RedissonCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return propertyIsTrue(context, "spring.redisson.enabled"); } private static boolean propertyIsTrue(ConditionContext context, String key) { return context.getEnvironment().getProperty(key, Boolean.TYPE, Boolean.FALSE); } }
redisson配置类RedissonProperties
package com.yx.redis.redisson; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; /** * @Auther: yx * @Date: 2019-04-30 15:52 * @Description: RedissonProperties */ @Data @ConfigurationProperties(prefix = "spring.redisson") public class RedissonProperties { private int timeout = 3000; private Boolean cluster; private String nodes; private String password; private int database = 0; private int connectionPoolSize = 64; private int connectionMinimumIdleSize = 10; private int slaveConnectionPoolSize = 250; private int masterConnectionPoolSize = 250; }