springboot2.0下集成redis-sentinel和主从集群模式
1.引入maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
里面已经默认集成了lettuce的redis连接池.
2.application.yml的配置文件项:
spring:
redis:
database: 0
timeout: 3000
password: 123456
lettuce:
pool:
max-idle: 500
min-idle: 50
max-active: 2000
max-wait: 1000
sentinel: #哨兵模式
master: mymaster #主服务器所在集群名称
nodes:
- 192.168.110.130:6000
- 192.168.110.130:6001
- 192.168.110.130:6002
3.配置类:
@Bean
public RedisTemplate newRedisTemplate(LettuceConnectionFactory redisConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
4.在项目中直接调用RedisTemplate 的相关方法即可
要试故障转移,哨兵自动主从切换可以参考上篇文章:
网友评论