前言:redis
緩存對應實時性要求比較高的業務需求可謂十分重要,但緩存中的實時數據一旦丟失,將直接影響整個業務功能。考慮到各場景的業務需求,redis推出了高可用的方案,即:主從+哨兵的HA機制。 緩存
準備spa
"redis-4.0.6.tar.gz" 下載地址(https://redis.io/download )debug
$ tar -zxvf redis-4.0.6.tar.gz 3d
$ ln -s redis-4.0.6 redis調試
1. 背景
升級以前:
單實例 Jedis Instance
-------------------------------------
HA升級方案:日誌
2. 實現步驟
step1:主節點test-zk02增長以下服務啓動:code
test-zk02 (master)
src/redis-sentinel.confserver
配置文件的內容見「附1」blog
test-zk03 (slave)
src/redis-server redis6379.conf
複製主節點的配置後在末尾加上一行"slaveof test-zk02 6379"便可
step2:代碼層修改以下:
Set<String> sentinels = new HashSet<String>(); sentinels.add("192.168.10.80:26379"); JedisSentinelPool jedisPool = new JedisSentinelPool("mymaster", sentinels);
3. 注意
1).服務升級用戶爲root
2).配置哨兵日誌 sentinel26379.log
3).修改代碼層以後需及時同步redis配置
4).升級HA後觀察鏈接數變更 " echo 'info clients' | /opt/redis-cli -h test-zk02 -p 6379"
附1
-------------------------------
sentinel.conf配置內容
bind 127.0.0.1 test-zk02
protected-mode no
daemonize yes
logfile "/opt/redis/s26379.log"
port 26379
dir /tmp
sentinel monitor test-master test-zk02 6379 1
sentinel down-after-milliseconds test-master 30000
sentinel parallel-syncs test-master 1
sentinel failover-timeout test-master 180000
附2
-------------------------------
主從切換調試命令
/opt/redis-cli -h localhost -p 9998 debug sleep 20
主從切換日誌
55794:X 01 Jun 15:25:55.813 # +sdown master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.813 # +odown master test-master 192.168.10.80 9999 #quorum 1/1
55794:X 01 Jun 15:25:55.813 # +new-epoch 3
55794:X 01 Jun 15:25:55.813 # +try-failover master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.828 # +vote-for-leader 35e9cce79ec5d7a2f1cd9473d089166b533c4690 3
55794:X 01 Jun 15:25:55.828 # +elected-leader master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.828 # +failover-state-select-slave master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.886 # +selected-slave slave 192.168.10.80:9998 192.168.10.80 9998 @ test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.886 * +failover-state-send-slaveof-noone slave 192.168.10.80:9998 192.168.10.80 9998 @ test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.945 * +failover-state-wait-promotion slave 192.168.10.80:9998 192.168.10.80 9998 @ test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:56.894 # +promoted-slave slave 192.168.10.80:9998 192.168.10.80 9998 @ test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:56.894 # +failover-state-reconf-slaves master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:56.969 # +failover-end master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:56.969 # +switch-master test-master 192.168.10.80 9999 192.168.10.80 9998
55794:X 01 Jun 15:25:56.969 * +slave slave 192.168.10.80:9999 192.168.10.80 9999 @ test-master 192.168.10.80 9998
55794:X 01 Jun 15:26:25.531 * +convert-to-slave slave 192.168.10.80:9999 192.168.10.80 9999 @ test-master 192.168.10.80 9998
參考資料
https://redis.io/topics/sentinel