redis集群之哨兵模式
1、集群部署
安装配置可参考一下地址:
https://www.cnblogs.com/zhoujinyi/p/5569462.html
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
public class RedisComponent {
@Autowired
//操作字符串的template,StringRedisTemplate是RedisTemplate的一个子集
private StringRedisTemplate stringRedisTemplate;
@Autowired
// RedisTemplate,可以进行所有的操作
private RedisTemplate<Object,Object> redisTemplate;
public void set(String key, String value){
ValueOperations<String, String> 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);
}
}