springboot2.0下集成redis-sentinel和主從集群模式
1.引入maven依賴:
<dependency>
? ? <groupId>org.springframework.boot</groupId>
? ? <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
里面已經(jīng)默認集成了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.在項目中直接調(diào)用RedisTemplate 的相關方法即可
要試故障轉(zhuǎn)移桑孩,哨兵自動主從切換可以參考上篇文章: