Redis哨兵机制工作原理(心跳检测+自动故障转移)
1).当哨兵启动时,首先会监控主机,从主机中获取当前所有的节点的状态,同时哨兵开启心跳检测机制.
2).当主机发生宕机的现象时,由于哨兵有PING-PONG机制 发现主机宕机,则哨兵开始进行选举.
3).当选举成功之后,新的主机当选之后,其他的节点当新主机的从.
解决: 1.单节点并发压力问题 2.单节点内存和磁盘物理上限
搭建环境
1.在主节点上创建哨兵配置
- 在Master对应redis.conf同目录下新建sentinel.conf文件,名字绝对不能错;
2.配置哨兵,在sentinel.conf文件中填入内容:
- sentinel monitor 被监控数据库名字(自己起名字) ip port 1
3.启动哨兵模式进行测试
- redis-sentinel sentinel.conf
说明:这个后面的数字2,是指当有两个及以上的sentinel服务检测到master宕机,才会去执行主从切换的功能。
编辑监控配置
修改选举时间
2.4 启动哨兵服务
命令: redis-sentinel sentinel.conf
高可用检查:
1).先关闭主机 等待10秒检查是否进行选举.
2).启动6379. 检查是否当选新主机的从.
用jedis操作redis
springboot整合哨兵机制
1.创建配置文件存储redis信息
jedis配置哨兵
springboot整合哨兵在程序配置文件中配置
redis sentinel 配置
master书写是使用哨兵监听的那个名称
spring.redis.sentinel.master=mymaster
连接的不再是一个具体redis主机,书写的是多个哨兵节点
spring.redis.sentinel.nodes=192.168.202.206:26379
- 注意:如果连接过程中出现如下错误:RedisConnectionException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no’ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2)
- 解决方案:在哨兵的配置文件中加入bind 0.0.0.0 开启远程连接权限