1. 程式人生 > >redis叢集之哨兵模式

redis叢集之哨兵模式

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](https://my.oschina.net/u/3907912)
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);
	}
}