https://blog.csdn.net/weixin_41846320/article/details/83753182
1、以文章为基础:主从模式
2、分别在三个redis目录下新增 sentinel.conf 文件:
port 26379
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 12
sentinel leader-epoch mymaster 13
port 26479
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 12
sentinel leader-epoch mymaster 13
port 26579
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 12
sentinel leader-epoch mymaster 13
3、增加哨兵启动脚本 startup_s.bat(其他类似)关键字是必须--sentinel:
title slaver_6381_s
redis-server.exe sentinel.conf --sentinel
4、总启动脚本:
cd master_6379
start "master_6379" "F:\MiddleWares\master_6379\startup.bat"
start "master_6379_s" "F:\MiddleWares\master_6379\startup_s.bat"
cd ../
cd slaver_6380
start "slaver_6380" "F:\MiddleWares\slaver_6380\startup.bat"
start "slaver_6380_s" "F:\MiddleWares\slaver_6380\startup_s.bat"
cd ../
cd slaver_6381
start "slaver_6381" "F:\MiddleWares\slaver_6381\startup.bat"
start "slaver_6381_s" "F:\MiddleWares\slaver_6381\startup_s.bat"
启动后 sentinel.conf 文件的变化(6379~6381):
port 26379
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 12
sentinel leader-epoch mymaster 13
# Generated by CONFIG REWRITE
dir "F:\\MiddleWares\\master_6379"
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-slave mymaster 127.0.0.1 6380
sentinel known-sentinel mymaster 127.0.0.1 26579 81435f5e909f96330cf51e33db89f0908a9a669e
sentinel known-sentinel mymaster 127.0.0.1 26479 9a146d63a04fceb6630b95fd1eab6d2492011531
sentinel current-epoch 12
port 26479
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 12
sentinel leader-epoch mymaster 13
# Generated by CONFIG REWRITE
dir "F:\\MiddleWares\\slaver_6380"
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-slave mymaster 127.0.0.1 6380
sentinel known-sentinel mymaster 127.0.0.1 26579 81435f5e909f96330cf51e33db89f0908a9a669e
sentinel known-sentinel mymaster 127.0.0.1 26379 ff5e921d1d156ff53664ec4963930a1681306c7d
sentinel current-epoch 12
port 26579
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 12
sentinel leader-epoch mymaster 13
# Generated by CONFIG REWRITE
dir "F:\\MiddleWares\\slaver_6381"
sentinel known-slave mymaster 127.0.0.1 6380
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-sentinel mymaster 127.0.0.1 26479 9a146d63a04fceb6630b95fd1eab6d2492011531
sentinel known-sentinel mymaster 127.0.0.1 26379 ff5e921d1d156ff53664ec4963930a1681306c7d
sentinel 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"));
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=265271,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=265420,lag=0
master_repl_offset:265555
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:265554
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:265555
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:265555
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0: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"));
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=25451,lag=1
master_repl_offset:25586
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:25585
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:25586
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=3
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=3
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=3
sentinel.conf 配置文件的变化(6379~6381):
port 26379
sentinel monitor mymaster 127.0.0.1 6380 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 14
sentinel leader-epoch mymaster 14
# Generated by CONFIG REWRITE
dir "F:\\MiddleWares\\master_6379"
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-slave mymaster 127.0.0.1 6379
sentinel known-sentinel mymaster 127.0.0.1 26579 81435f5e909f96330cf51e33db89f0908a9a669e
sentinel known-sentinel mymaster 127.0.0.1 26479 9a146d63a04fceb6630b95fd1eab6d2492011531
sentinel current-epoch 14
port 26479
sentinel monitor mymaster 127.0.0.1 6380 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 14
sentinel leader-epoch mymaster 14
# Generated by CONFIG REWRITE
dir "F:\\MiddleWares\\slaver_6380"
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-slave mymaster 127.0.0.1 6379
sentinel known-sentinel mymaster 127.0.0.1 26579 81435f5e909f96330cf51e33db89f0908a9a669e
sentinel known-sentinel mymaster 127.0.0.1 26379 ff5e921d1d156ff53664ec4963930a1681306c7d
sentinel current-epoch 14
port 26579
sentinel monitor mymaster 127.0.0.1 6380 1
sentinel down-after-milliseconds mymaster 5000
sentinel config-epoch mymaster 14
sentinel leader-epoch mymaster 14
# Generated by CONFIG REWRITE
dir "F:\\MiddleWares\\slaver_6381"
sentinel known-slave mymaster 127.0.0.1 6379
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-sentinel mymaster 127.0.0.1 26479 9a146d63a04fceb6630b95fd1eab6d2492011531
sentinel known-sentinel mymaster 127.0.0.1 26379 ff5e921d1d156ff53664ec4963930a1681306c7d
sentinel 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