简介
哨兵(seninel)是一个分布式系统,用于对主从结构中的每台服务器进行监控,当出现故障时通过投票机制选择master并将所有slave连接到新的master中
作用
监控:不断检查master和slave是否正常运行。
通知:当被监控的服务器出现问题时,向其他(哨兵,客户端)发送通知
自动故障转移:断开master与slave连接,选取一个slave作为master,将其它slave连接到新的master,并告知客户端新的服务器地址
注意:哨兵也是redis服务器,不提供数据服务,通常配置为单数
配置
1.配置一主双从
2.配置三个哨兵(sentinel.conf)
3.启动哨兵
redis-sentinel sentinel-端口号.conf
配置步骤:
106.55.146.154(master)
39.106.142.155(slave)
1.master配置/etc/redis.conf,将bind 127.0.0.1变为 bind 0.0.0.0
2.master和slave配置/etc/redis-sentinel.conf改为
sentinel monitor mymaster 106.55.146.154 6379 2
3.修改slave的配置/etc/redis.conf
slaveof 106.55.146.154 6379
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
masterauth xxx(master的密码)
4.启动master和slave,通过service redis restart
5.启动master和slave的哨兵,通过redis-sentinel /etc/redis-sentinel.conf
6.启动redis客户端redis-cli -p 26379,输入info命令,可看到2个哨兵,1个从机
哨兵工作原理
监控阶段(用于同步各个节点的状态信息)
- 获取sentinel的状态(是否在线)
- 获取master状态(master属性:runid,role;slave的详细信息)
- 获取slave状态(slave属性)
通知阶段
故障转移阶段
1.判断mater下线
2.竞选哪个sentinel安排master下线
3.从服务器列表中挑选备选master
- 在线的
- 响应快的
- 与原master断开时间短
- 优先原则(优先级,offset, runid)
与springboot集成
spring:
redis:
#Redis 哨兵模式
password: larry123456
sentinel:
master: larry-master
nodes: 192.168.127.130:26379,192.168.127.128:26379,192.168.127.129:26379
#Redis 单机模式
# host: 192.168.127.128
# port: 6379
# password: larry123456