先看完這個(gè),然后很簡(jiǎn)單的弄一個(gè)master和一個(gè)slave。然后按順序啟動(dòng)。
https://redis.io/topics/replication
其實(shí)就是配置slaveof,然后分別配置requirepass和master-auth。
再看完這個(gè),
https://redis.io/topics/sentinel
然后啟動(dòng)(最佳實(shí)踐是3或者更多的奇數(shù)個(gè))哨兵服務(wù),可能用到的配置文件如下。
Sentinel 配置
bind 0.0.0.0
port 5000 #Sentinel 服務(wù)的端口
daemonize yes
logfile "/var/log/redis/sentinel.log"
sentinel monitor mymaster 127.0.0.1 6379 1 # Quorum取比總數(shù)/2大的最小整數(shù)即可,這里是1
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster toor # Redis服務(wù)的密碼
然后通過(guò)tailf /var/log/redis/sentinel.log來(lái)觀察服務(wù)的主備切換和各種狀態(tài)之類的。
應(yīng)用方面,SpringBoot 的配置如下。
spring.redis.database=0
spring.redis.password=toor
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=127.0.0.1:5000 # 把需要連接的Sentinel全列在這里即可
然后就正常的用RedisTemplate來(lái)操作就行了。
當(dāng)Redis_6379(主服務(wù))掛掉的時(shí)候,sentinel會(huì)將Redis_6380作為Master啟動(dòng),這個(gè)可以從sentinel的日志中觀察到。然后SpringBoot方面,會(huì)看到類似于Created JedisPool to master之類的信息重新連到新的Master上面,然后一次failover的切換就完成了。
后續(xù)
通過(guò)CLI登錄到6380服務(wù)上,把slaveof的指令取消(畢竟這時(shí)候已經(jīng)是master了嘛),然后將6379作為slave重新啟動(dòng)即可,這樣子下次6380掛了的話,6379就會(huì)繼續(xù)作為Master來(lái)提供服務(wù)了。