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 {
@Bean
RedisTemplate 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序列化方式采用jackson
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
Redisson
单机模式
新建yaml文件:
singleServerConfig:
# 连接空闲超时,单位:毫秒
idleConnectionTimeout: 10000
# 连接超时,单位:毫秒
connectTimeout: 10000
# 命令等待超时,单位:毫秒 默认3000
timeout: 3000
# 命令失败重试次数
retryAttempts: 3
# 命令重试发送时间间隔,单位:毫秒
retryInterval: 1500
# 无密码则设置 null
password: 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: 10000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
password: xxxx
subscriptionsPerConnection: 5
clientName: null
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
slaveSubscriptionConnectionMinimumIdleSize: 1
slaveSubscriptionConnectionPoolSize: 50
slaveConnectionMinimumIdleSize: 32
slaveConnectionPoolSize: 64
masterConnectionMinimumIdleSize: 32
masterConnectionPoolSize: 64
readMode: "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;
}
}