Windows版本redis高可用方案探究 (2)

4.png

启动redis

可以直接在服务中右键启动或者通过命令redis-server --service-start --service-name 服务名 启动指定服务。

通过redis-cli 连接到各个redis服务。

F:\Study\redis> redis-cli -p 28380 -a test1 127.0.0.1:28380> info Replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=28382,state=online,offset=183,lag=1 slave1:ip=127.0.0.1,port=28381,state=online,offset=183,lag=1 master_repl_offset:183 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:182

可以看到我们的主从配置已经生效了。

连接到从库28381

F:\Study\redis> redis-cli -p 28381 -a test1 127.0.0.1:28381> info Replication # Replication role:slave master_host:127.0.0.1 master_port:28380 master_link_status:up master_last_io_seconds_ago:9 master_sync_in_progress:0 slave_repl_offset:323 slave_priority:99 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

连接到从库28382

F:\Study\redis> redis-cli -p 28382 -a test1 127.0.0.1:28382> info Replication # Replication role:slave master_host:127.0.0.1 master_port:28380 master_link_status:up master_last_io_seconds_ago:8 master_sync_in_progress:0 slave_repl_offset:379 slave_priority:99 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

现在我们往主库写入数据会自动并主动同步到从库。

配置中默认配了以下配置了slave-read-only yes,从库是只读的,不允许写入。

配置哨兵

设置三个哨兵端口为 24381~24383

配置哨兵24381

dir ./ logfile "sentinel.28481.txt" port 28481 sentinel monitor master 127.0.0.1 28380 2 sentinel down-after-milliseconds master 5000 sentinel failover-timeout master 30000 sentinel auth-pass 127.0.0.1 28380 test1

sentinel monitor master 127.0.0.1 28380 2 设置主redis的别名为master,后面都通过master表示主redis。后面的2表示2个哨兵检测到主redis挂了即为挂了,需要重新选举出新的主redis,而原来的主redis降级为从redis。

sentinel down-after-milliseconds master 5000 哨兵多久连不上主redis即认为它挂了

sentinel failover-timeout master 30000 投票选举超时时间,超过时间没有选出则该轮投票失败。

sentinel auth-pass master test1 主redis的密码 。

由于哨兵只能设置主的密码,因此主从的密码应该设置为一样的。

配置哨兵24382

dir ./ logfile "sentinel.28482.txt" port 28482 sentinel monitor master 127.0.0.1 28380 2 sentinel down-after-milliseconds master 5000 sentinel failover-timeout master 30000 sentinel auth-pass 127.0.0.1 28380 test1

配置哨兵24383

dir ./ logfile "sentinel.28483.txt" port 28483 sentinel monitor master 127.0.0.1 28380 2 sentinel down-after-milliseconds master 5000 sentinel failover-timeout master 30000 sentinel auth-pass 127.0.0.1 28380 test1 启动哨兵

通过命令redis-server.exe 配置 --sentinel 启动哨兵

redis-server redis-sentinel-28481.conf --sentinel redis-server redis-sentinel-28482.conf --sentinel redis-server redis-sentinel-28483.conf --sentinel

启动哨兵28481后查看日志

[8464] 21 Nov 02:02:05.258 # +tilt #tilt mode entered [8464] 21 Nov 08:33:01.513 # +tilt #tilt mode entered [8464] 21 Nov 08:33:31.561 # -tilt #tilt mode exited _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.100 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 28481 | `-._ `._ / _.-' | PID: 22332 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [22332] 05 Jan 12:01:48.399 # Sentinel ID is 48693b2911456ef3da6ebfd14d1adf46e76fbb8b [22332] 05 Jan 12:01:48.399 # +monitor master master 127.0.0.1 28382 quorum 2 [22332] 05 Jan 12:01:53.399 # +sdown sentinel ce50397a76e2e3ca165ce407859cbc94d7caf504 127.0.0.1 28482 @ master 127.0.0.1 28382 [22332] 05 Jan 12:01:53.399 # +sdown sentinel 162c66f370550a9926b794abecfb431cf3f8bcc9 127.0.0.1 28483 @ master 127.0.0.1 28382 [22332] 05 Jan 12:02:13.404 # +sdown master master 127.0.0.1 28382

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzwjzw.html