一、什麼是Sentinel(哨兵)
Sentinel(哨兵)是Redis 的高可用性解決方案:由一個或多個Sentinel 實例 組成的Sentinel 系統可以監視任意多個主服務器,以及這些主服務器屬下的所有從服務器,並在被監視的主服務器進入下線狀態時,自動將下線主服務器屬下的某個從服務器升級爲新的主服務器。
注意:配置哨兵必須實現Redis讀寫分離如何配置請看:https://blog.csdn.net/qq_43791724/article/details/104908590
二、Sentinel(哨兵)執行流程
三、配置Sentinel(哨兵)
修改文件: sentinel.conf(三臺節點都需要配置)
#修改bind配置,每臺機器修改爲自己對應的主機名
bind node01
#配置sentinel服務後臺運行
daemonize yes
#修改三臺機器監控的主節點,現在主節點是node01服務器
sentinel monitor mymaster node01 6379 2
啓動測試:(三臺搜需要啓動)
src/redis-sentinel sentinel.conf
ps -ef |grep redis
四、測試redis的sentinel(哨兵)代碼開發
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import java.util.Arrays;
import java.util.HashSet;
public class RedisDemo01 {
public static void main(String[] args) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10);
config.setMaxIdle(5);
config.setMinIdle(5);
//添加哨兵信息
HashSet<String> lists = new HashSet<>(Arrays.asList("node01:26379","node02:26379","node03:26379"));
JedisSentinelPool pool = new JedisSentinelPool("mymaster", lists, config);
// 獲取連接對象
Jedis resource = pool.getResource();
resource.set("mykey", "myvalue");
// 控制檯輸入myvalue 說明執行成功
System.out.println(resource.get("mykey"));
}
}