redis叢集之哨兵模式
2、與springboot內建 這裡哨兵模式暫時隻提供了故障自動轉移等,暫時不提供負載均衡功能,自動提供了故障轉移和主從複制功能
配置
spring.redis.database=0
spring.redis.password=123456
# pool settings ...池配置
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
#哨兵監聽redis server名稱
spring.redis.sentinel.master=mymaster
#哨兵的配置清單
spring.redis.sentinel.nodes=192.168.12.194:26379,192.168.12.194:36379,192.168.12.194:4637
調用封裝
[@Component](https://my.oschina.net/u/3907912)
public class RedisComponent {
@Autowired
//操作字元串的template,StringRedisTemplate是RedisTemplate的一個子集
private StringRedisTemplate stringRedisTemplate;
@Autowired
// RedisTemplate,可以進行所有的操作
private RedisTemplate redisTemplate;
public void set(String key, String value){
ValueOperations ops = this.stringRedisTemplate.opsForValue();
boolean bExistent = this.stringRedisTemplate.hasKey(key);
if (bExistent) {
System.out.println("this key is bExistent!");
}else{
ops.set(key, value);
}
}
public String get(String key){
return this.stringRedisTemplate.opsForValue().get(key);
}
public void del(String key){
this.stringRedisTemplate.delete(key);
}
public void sentinelSet(User user){
String key = null;
try {
key = new String(user.getId().getBytes("gbk"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(key);
redisTemplate.opsForValue().set(key, user.toString());
}
public String sentinelGet(String key){
return stringRedisTemplate.opsForValue().get(key);
}
}