0
点赞
收藏
分享

微信扫一扫

Redis 哨兵模式(windows)

何以至千里 2024-05-02 阅读 20

1、以文章为基础:主从模式

2、分别在三个redis目录下新增  sentinel.conf  文件:

port 26379sentinel monitor mymaster 127.0.0.1 6379 1sentinel down-after-milliseconds mymaster 5000sentinel config-epoch mymaster 12sentinel leader-epoch mymaster 13

port 26479sentinel monitor mymaster 127.0.0.1 6379 1sentinel down-after-milliseconds mymaster 5000sentinel config-epoch mymaster 12sentinel leader-epoch mymaster 13

port 26579sentinel monitor mymaster 127.0.0.1 6379 1sentinel down-after-milliseconds mymaster 5000sentinel config-epoch mymaster 12sentinel leader-epoch mymaster 13

3、增加哨兵启动脚本  startup_s.bat(其他类似)关键字是必须--sentinel

title slaver_6381_sredis-server.exe sentinel.conf --sentinel

4、总启动脚本:

cd master_6379start "master_6379" "F:\MiddleWares\master_6379\startup.bat"start "master_6379_s" "F:\MiddleWares\master_6379\startup_s.bat" cd ../cd slaver_6380start "slaver_6380" "F:\MiddleWares\slaver_6380\startup.bat"start "slaver_6380_s" "F:\MiddleWares\slaver_6380\startup_s.bat" cd ../cd slaver_6381start "slaver_6381" "F:\MiddleWares\slaver_6381\startup.bat"start "slaver_6381_s" "F:\MiddleWares\slaver_6381\startup_s.bat"

5、启动结果:

启动后  sentinel.conf 文件的变化(6379~6381):

port 26379sentinel monitor mymaster 127.0.0.1 6379 1sentinel down-after-milliseconds mymaster 5000sentinel config-epoch mymaster 12sentinel leader-epoch mymaster 13# Generated by CONFIG REWRITEdir "F:\\MiddleWares\\master_6379"sentinel known-slave mymaster 127.0.0.1 6381sentinel known-slave mymaster 127.0.0.1 6380sentinel known-sentinel mymaster 127.0.0.1 26579 81435f5e909f96330cf51e33db89f0908a9a669esentinel known-sentinel mymaster 127.0.0.1 26479 9a146d63a04fceb6630b95fd1eab6d2492011531sentinel current-epoch 12 port 26479sentinel monitor mymaster 127.0.0.1 6379 1sentinel down-after-milliseconds mymaster 5000sentinel config-epoch mymaster 12sentinel leader-epoch mymaster 13# Generated by CONFIG REWRITEdir "F:\\MiddleWares\\slaver_6380"sentinel known-slave mymaster 127.0.0.1 6381sentinel known-slave mymaster 127.0.0.1 6380sentinel known-sentinel mymaster 127.0.0.1 26579 81435f5e909f96330cf51e33db89f0908a9a669esentinel known-sentinel mymaster 127.0.0.1 26379 ff5e921d1d156ff53664ec4963930a1681306c7dsentinel current-epoch 12 port 26579sentinel monitor mymaster 127.0.0.1 6379 1sentinel down-after-milliseconds mymaster 5000sentinel config-epoch mymaster 12sentinel leader-epoch mymaster 13# Generated by CONFIG REWRITEdir "F:\\MiddleWares\\slaver_6381"sentinel known-slave mymaster 127.0.0.1 6380sentinel known-slave mymaster 127.0.0.1 6381sentinel known-sentinel mymaster 127.0.0.1 26479 9a146d63a04fceb6630b95fd1eab6d2492011531sentinel known-sentinel mymaster 127.0.0.1 26379 ff5e921d1d156ff53664ec4963930a1681306c7dsentinel current-epoch 12

6、连接测试:

Jedis jedis = new Jedis("localhost", 6379);System.out.println(jedis.info("Replication"));jedis = new Jedis("localhost", 6380);System.out.println(jedis.info("Replication"));jedis = new Jedis("localhost", 6381);System.out.println(jedis.info("Replication"));jedis = new Jedis("localhost", 26379);System.out.println(jedis.info("sentinel"));jedis = new Jedis("localhost", 26479);System.out.println(jedis.info("sentinel"));jedis = new Jedis("localhost", 26579);System.out.println(jedis.info("sentinel"));

# Replicationrole:masterconnected_slaves:2slave0:ip=127.0.0.1,port=6380,state=online,offset=265271,lag=0slave1:ip=127.0.0.1,port=6381,state=online,offset=265420,lag=0master_repl_offset:265555repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:265554 # Replicationrole:slavemaster_host:127.0.0.1master_port:6379master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_repl_offset:265555slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0 # Replicationrole:slavemaster_host:127.0.0.1master_port:6379master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_repl_offset:265555slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0 # Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3 # Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3 # Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3

7、同样,从机是只读

8、关掉master之后的连接测试(6379已经connect refused):

Jedis jedis = new Jedis("localhost", 6380);System.out.println(jedis.info("Replication"));jedis = new Jedis("localhost", 6381);System.out.println(jedis.info("Replication"));jedis = new Jedis("localhost", 26379);System.out.println(jedis.info("sentinel"));jedis = new Jedis("localhost", 26479);System.out.println(jedis.info("sentinel"));jedis = new Jedis("localhost", 26579);System.out.println(jedis.info("sentinel"));

# Replicationrole:masterconnected_slaves:1slave0:ip=127.0.0.1,port=6381,state=online,offset=25451,lag=1master_repl_offset:25586repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:25585 # Replicationrole:slavemaster_host:127.0.0.1master_port:6380master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_repl_offset:25586slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0 # Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=3 # Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=3 # Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=3

sentinel.conf  配置文件的变化(6379~6381):

port 26379sentinel monitor mymaster 127.0.0.1 6380 1sentinel down-after-milliseconds mymaster 5000sentinel config-epoch mymaster 14sentinel leader-epoch mymaster 14# Generated by CONFIG REWRITEdir "F:\\MiddleWares\\master_6379"sentinel known-slave mymaster 127.0.0.1 6381sentinel known-slave mymaster 127.0.0.1 6379sentinel known-sentinel mymaster 127.0.0.1 26579 81435f5e909f96330cf51e33db89f0908a9a669esentinel known-sentinel mymaster 127.0.0.1 26479 9a146d63a04fceb6630b95fd1eab6d2492011531sentinel current-epoch 14 port 26479sentinel monitor mymaster 127.0.0.1 6380 1sentinel down-after-milliseconds mymaster 5000sentinel config-epoch mymaster 14sentinel leader-epoch mymaster 14# Generated by CONFIG REWRITEdir "F:\\MiddleWares\\slaver_6380"sentinel known-slave mymaster 127.0.0.1 6381sentinel known-slave mymaster 127.0.0.1 6379sentinel known-sentinel mymaster 127.0.0.1 26579 81435f5e909f96330cf51e33db89f0908a9a669esentinel known-sentinel mymaster 127.0.0.1 26379 ff5e921d1d156ff53664ec4963930a1681306c7dsentinel current-epoch 14 port 26579sentinel monitor mymaster 127.0.0.1 6380 1sentinel down-after-milliseconds mymaster 5000sentinel config-epoch mymaster 14sentinel leader-epoch mymaster 14# Generated by CONFIG REWRITEdir "F:\\MiddleWares\\slaver_6381"sentinel known-slave mymaster 127.0.0.1 6379sentinel known-slave mymaster 127.0.0.1 6381sentinel known-sentinel mymaster 127.0.0.1 26479 9a146d63a04fceb6630b95fd1eab6d2492011531sentinel known-sentinel mymaster 127.0.0.1 26379 ff5e921d1d156ff53664ec4963930a1681306c7dsentinel current-epoch 14

6380 的 redis.windows.conf  配置文件中的  "slaveof 127.0.0.1 6379"  被删除6381 的 redis.windows.conf  配置文件中的  "slaveof 127.0.0.1 6379"  被改为 "slaveof 127.0.0.1 6380"也就是说6380成为了主节点,并且拥有之前6379主节点的功能

9、如果关掉了master 的 sentinel 之后:

连接测试中查看 26479  和  26579 ,sentinel的值仍然显示为3:

master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3

并且再关闭master之后,6380 节点仍然可以变更为主节点,只是原有的 6379节点中的 sentinel.conf  文件不会被修改(6380和6381的会);

10、关于 sentinel.conf 配置文件的解释:

#当前Sentinel服务运行的端口port 26379#Sentinel去监视一个名为mymaster的主redis实例,这个主实例的IP地址为本机地址127.0.0.1,端口号为6379,#而将这个主实例判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行sentinel monitor mymaster 127.0.0.1 6379 1#指定了Sentinel认为Redis实例已经失效所需的毫秒数。当 实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。#只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行sentinel down-after-milliseconds mymaster 5000#指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长sentinel config-epoch mymaster 12#如果在该时间(ms)内未能完成failover操作,则认为该failover失败sentinel leader-epoch mymaster 13

举报

相关推荐

0 条评论