先看完這個,然后很簡單的弄一個master和一個slave绪励。然后按順序啟動。
https://redis.io/topics/replication
其實就是配置slaveof唠粥,然后分別配置requirepass和master-auth疏魏。
再看完這個,
https://redis.io/topics/sentinel
然后啟動(最佳實踐是3或者更多的奇數(shù)個)哨兵服務(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ù)的密碼
然后通過tailf /var/log/redis/sentinel.log
來觀察服務(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
來操作就行了舅巷。
當Redis_6379(主服務(wù))掛掉的時候羔味,sentinel會將Redis_6380作為Master啟動,這個可以從sentinel的日志中觀察到钠右。然后SpringBoot方面赋元,會看到類似于Created JedisPool to master
之類的信息重新連到新的Master上面,然后一次failover的切換就完成了飒房。
后續(xù)
通過CLI登錄到6380服務(wù)上搁凸,把slaveof的指令取消(畢竟這時候已經(jīng)是master了嘛),然后將6379作為slave重新啟動即可狠毯,這樣子下次6380掛了的話护糖,6379就會繼續(xù)作為Master來提供服務(wù)了。