近期为了复习redis的知识点,于是动手在本地环境搭建了一套主从架构。
端口号如下
master | 127.0.0.1:6379 |
slave0 | 127.0.0.1:6380 |
slave1 | 127.0.0.1:6381 |
sentinelA | 127.0.0.1:16379 |
sentinelB | 127.0.0.1:16380 |
sentinelC | 127.0.0.1:16381 |
由于windows版本的redis,官方已停止支持,所以只能从github上面下载,地址为 https://github.com/tporadowski/redis/releases
安装的过程如下:
1. 将下载的压缩包分别解压至三个目录 c:\devtools\redis\Redis6379,c:\devtools\redis\Redis6380,c:\devtools\redis\Redis6381,在这几个目录下面分别创建data目录
2. 修改三个路径下面redis的配置文件,改动的地方如下:
master ----
port 6379
logfile "C:/devtools/redis/Redis6379/redis.log" #可以不指定,将直接在控制台输出log
dir C:\devtools\redis\Redis6379\data # 不指定将直接在当前路径下面保存数据文件 rdb/aof
masterauth abcd # salve连接master需要的密码
requirepass abcd #client连接redis需要的密码,此密码和上面的含义不同
slave0 ----
port 6380
logfile "C:/devtools/redis/Redis6380/redis.log" #可以不指定,将直接在控制台输出log
dir C:\devtools\redis\Redis6380\data # 不指定将直接在当前路径下面保存数据文件 rdb/aof
masterauth abcd # salve连接master需要的密码
requirepass abcd #client连接redis需要的密码,此密码和上面的含义不同
replicaof 127.0.0.1 6379 #在新的版本中使用replicaof而不是slaveof
slave1 ----
改动同slave0,端口和路径中的6380改成6381,不赘述
3. sentinel配置(sentinel无需单独下载,本身redis已经集成了这部分的功能)
## sentinelA ----
port 16379 bind 127.0.0.1 #不加这个启动不起来 sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 15000 sentinel auth-pass mymaster abcd
## sentinelB ----
port 16380 bind 127.0.0.1 # sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 15000 sentinel auth-pass mymaster abcd
## sentinelC ----
port 16381 bind 127.0.0.1 # sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 15000 sentinel auth-pass mymaster abcd
4. 启动redis
切换至master和slave路径下面分别执行,log将会打印到对应的log文件里面,控制台无其他输出
redis-server.exe redis.windows.conf
5. 检查redis状态
c:\devtools\redis\Redis6379>redis-cli.exe -a abcd -p 6379 #host默认为127.0.0.1,不用加 -h 127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=22570,lag=1 slave1:ip=127.0.0.1,port=6381,state=online,offset=22570,lag=1 master_replid:4fc3f06bc24da1f0ea199088d1de587bc8ddb65c master_replid2:0000000000000000000000000000000000000000 master_repl_offset:22570 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:22570
6. 启动sentinel
c:\devtools\redis\Redis6379>redis-server.exe redis-sentinel.conf --sentinel
c:\devtools\redis\Redis6379>redis-server.exe redis-sentinel.conf --sentinel [23364] 27 Mar 21:49:14.167 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo [23364] 27 Mar 21:49:14.167 # Redis version=5.0.14.1, bits=64, commit=ec77f72d, modified=0, pid=23364, just started [23364] 27 Mar 21:49:14.168 # Configuration loaded _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 5.0.14.1 (ec77f72d/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 16379 | `-._ `._ / _.-' | PID: 23364 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [23364] 27 Mar 21:49:14.182 # Sentinel ID is 1d0a81f07e988ed9f0a9392e82ea388083c68606 [23364] 27 Mar 21:49:14.183 # +monitor master mymaster 127.0.0.1 6379 quorum 2 [23364] 27 Mar 21:49:14.187 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379 [23364] 27 Mar 21:49:14.190 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379 [23364] 27 Mar 21:51:30.448 * +sentinel sentinel c94dde54d0f6aa124356d8a58c6be0a8c4ae8058 127.0.0.1 16380 @ mymaster 127.0.0.1 6379 [23364] 27 Mar 21:51:50.048 * +sentinel sentinel 4ffdae22c366d20bf3383b564496f25da76ced28 127.0.0.1 16381 @ mymaster 127.0.0.1 6379 [23364] 28 Mar 07:11:42.854 # +tilt #tilt mode entered [23364] 28 Mar 07:12:12.893 # -tilt #tilt mode exited
7. 检查sentinel状态
c:\devtools\redis\Redis6379>redis-cli.exe -p 16380 127.0.0.1:16380> info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
8. 查看sentinel的可靠性,尝试关掉master