luttuce 和redisson配置过程中实际上是独立的,他们两个可以同时集成,但是没有直接相关关系,配置相对独立。
所以分为Lettuce 和 Redisson 两套配置
父pom
<!-- Spring Data Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.3.11.RELEASE</version></dependency><!--连接池 不集成redisTempate配置工厂类会报错--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</version></dependency><!-- Redisson --><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-data-22</artifactId><version>3.23.3</version></dependency>
子pom
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-data-22</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>
lettuce
单机模式
cluster集群模式
lettuce yaml配置
配置文件
@Configuration
public class RedisConfig {@BeanRedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){RedisTemplate redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(lettuceConnectionFactory);// 使用Jackson2JsonRedisSerializer 替换默认的JDKSerializationRedisSerializer 来序列化和反序列化redis 的value值Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash的value序列化方式采用jacksonredisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.afterPropertiesSet();return redisTemplate;}
}
Redisson
单机模式
新建yaml文件:
singleServerConfig:# 连接空闲超时,单位:毫秒idleConnectionTimeout: 10000# 连接超时,单位:毫秒connectTimeout: 10000# 命令等待超时,单位:毫秒 默认3000timeout: 3000# 命令失败重试次数retryAttempts: 3# 命令重试发送时间间隔,单位:毫秒retryInterval: 1500# 无密码则设置 nullpassword: 123456# 单个连接最大订阅数量subscriptionsPerConnection: 5# 客户端名称clientName: null# redis 节点地址address: "redis://127.0.0.1:6379"# 从节点发布和订阅连接的最小空闲连接数subscriptionConnectionMinimumIdleSize: 1# 发布和订阅连接池大小subscriptionConnectionPoolSize: 50# 发布和订阅连接的最小空闲连接数connectionMinimumIdleSize: 32# 发布和订阅连接池大小connectionPoolSize: 64# 数据库编号database: 10# DNS监测时间间隔,单位:毫秒 在启用该功能以后,Redisson将会监测DNS的变化情况dnsMonitoringInterval: 5000
threads: 0
nettyThreads: 0
codec: !<org.redisson.codec.JsonJacksonCodec> {}
transportMode: "NIO"
集群模式
新建yaml文件:
clusterServersConfig:idleConnectionTimeout: 10000connectTimeout: 10000timeout: 3000retryAttempts: 3retryInterval: 1500password: xxxxsubscriptionsPerConnection: 5clientName: nullloadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}slaveSubscriptionConnectionMinimumIdleSize: 1slaveSubscriptionConnectionPoolSize: 50slaveConnectionMinimumIdleSize: 32slaveConnectionPoolSize: 64masterConnectionMinimumIdleSize: 32masterConnectionPoolSize: 64readMode: "SLAVE"nodeAddresses:- "redis://xxx:6379"- "redis://xxx:6379"- "redis://xxx:6379"- "redis://xxx:6379"- "redis://xxx:6379"- "redis://xxx:6379"- "redis://xxx:6379"- "redis://xxx:6379"- "redis://xxx:6379"- "redis://xxx:6379"- "redis://xxx:6379"- "redis://xxx:6379"scanInterval: 1000
threads: 0
nettyThreads: 0
codec: !<org.redisson.codec.JsonJacksonCodec> {}
"transportMode":"NIO"
redisson配置文件
@Configuration
public class RedissonConfig {@Value("${spring.profiles.active}")private String active;@Bean(destroyMethod="shutdown")public RedissonClient redissonClient() throws IOException {// 开发环境-单机模式if ("dev".equals(active)){Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson-single-dev.yaml"));return Redisson.create(config);}// 测试环境-测试cluster集群模式if ("test".equals(active)){Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson-cluster-test.yaml"));return Redisson.create(config);}// 开发环境-正式cluster集群模式if ("online".equals(active)){Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson-cluster-online.yaml"));return Redisson.create(config);}return null;}
}