一. 主从复制中出现的问题
1)需要手动故障转移
2)写能力和存储能力受限
二. Redis sentinel 架构和故障转移
1)Redis sentinel架构
a. redis sentinel采用多节点保证其高可用性
b. redis sentinel负责监控每一个节点的状态
c. redis sentinel负责连接客户端,并将客户端的请求转给redis或将redis结果返回给客户端
2 )redis故障转移
故障转移总共有6步:
1.多个sentinel发现并确认master有问题
2.选举出一个sentinel作为领导
3.选举出一个slave作为master
4.通知其余slave成为新的master的slave
5.通知客户端主从变化
6.等待老的master复活成为新的master的slave
三. Redis Sentinel 安装。
1)服务列表:3个Redis Sentinel + 3个redis服务器(1个master + 2个slave)
2)配置3个redis服务器并启动
a.master配置
port 6380
daemonize yes
pidfile /var/run/redis_6380.pid
logfile "6380.log"
dbfilename dump-6380.rdb
dir /opt/soft/redis/test/redis-6380/data
b.slave1配置
port 6381
daemonize yes
pidfile /var/run/redis_6381.pid
logfile "6381.log"
dbfilename dump-6381.rdb
dir /opt/soft/redis/test/redis-6381/data
slaveof 127.0.0.1 6380
c.slave2配置
port 6382
daemonize yes
pidfile /var/run/redis_6382.pid
logfile "6382.log"
dbfilename dump-6382.rdb
dir /opt/soft/redis/test/redis-6382/data
slaveof 127.0.0.1 6380
d.启动redis服务器
./bin/redis-server ./conf/redis-6380.conf
./bin/redis-server ./conf/redis-6381.conf
./bin/redis-server ./conf/redis-6382.conf
3)配置3个redis sentinel服务器并启动
a.sentinel1配置
daemonize yes
port 26379
dir /opt/soft/redis/test/sentinal-26379/data
logfile "26379.log"
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 30000 #设置故障down机30秒之后执行故障自动转移
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
b.sentinel2配置
daemonize yes
port 26380
dir /opt/soft/redis/test/sentinal-26380/data
logfile "26380.log"
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
c.sentinel3配置
daemonize yes
port 26381
dir /opt/soft/redis/test/sentinal-26381/data
logfile "26381.log"
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
d.启动redis-sentinel服务器
./bin/redis-sentinel ./conf/sentinel-26379.conf
./bin/redis-sentinel ./conf/sentinel-26380.conf
./bin/redis-sentinel ./conf/sentinel-26381.conf
四. Java客户端连接redis sentinel
a.客户端借入redis sentinel流程
1.获取Sentinel地址集合
2.根据masterName获取master信息
3.注意这个不是代理模式,因为它不是每次都要先跟Sentinel请求才能获得master
b.使用Jedis连接redis sentinel方法
五. 故障转移
故障转移时间:down-after-milliseconds
down-after-milliseconds 用来配置用来设置故障转移时间,如果主节点 master down机,sentinel 会在 down-after-milliseconds 时间内一直尝试重连,若 down-after-milliseconds 时间后后仍未连接成功,则 sentinel 会自动进行故障转移,但在 down-after-milliseconds 时间内redis服务是 down掉的,无法提供服务
六. Redis Sentinel 故障转移原理:三个定时任务
1. 每 10 秒每个 sentinel 对 master 和 slave 执行 info,这样可以发现 slave 节点和确认主从关系
2. 每 2 秒每个 sentinel 通过 master 节点的 channel 交换信息(publish/subscribe),主要通过 _sentinel_:hello 频道交互,达到交互对节点“看法”和自身信息
3. 每 1 秒每个 sentinel 对其他 sentinel 和 redis 执行 ping 心跳检测,用作失败判定依据
七. Redis Sentinel 故障转移原理:主观下线和客观下线
监视命令:monitor <masterName> <ip> <port> <quorum>
主观下线:每个sentinel节点对Redis节点失败的“偏见“
客观下线:所有sentinel节点对Redis节点失败“达成共识”(超过quorum个统一)
八. Redis Sentinel 故障转移原理:sentinel 领导者选举过程
九. Redis Sentinel 故障转移原理:故障转移过程
原文:https://www.cnblogs.com/programmlover/p/11645069.html