手打不易,如果转摘,请注明出处!
注明原文: https://blog.csdn.net/q258523454/article/details/144477957
前言
Redis版本:redis 5.0.14.1
Windows版本:Windows10
本文只讲集群模式
1. 安装Redis
1.1 下载
https://github.com/tporadowski/redis/releases

1.2 解压
解压到任意目录,例如:E:\Program Files (x86)\redis5.0.14.1
2. 集群配置准备
2.1 创建集群目录
创建一个目录,名字随意,例如: cluster

由于Redis集群模式,一般需要6个节点。假设我们要创建的集群节点的端口分别为:
7001、7002、7003、7004、7005、7006.
我们先创建7001节点,如图所示先在cluster目录下,创建一个名为7001的目录.

我们把下载的redis再解压(或者拷贝)一次,本文这里下载的版本是:Redis-x64-5.0.14.1.zip,重新再这里解压一次。注意:cluster目录的上一层,已经是解压了的,只是这里在做一次同样的操作。
得到如下目录:

2.2 修改节点配置
打开目录:E:\Program Files (x86)\redis5.0.14.1\cluster\7001 下的配置文件:redis.windows.conf
修改或新增以下内容:
注意:
① requirepass 和 masterauth 是密码,可以不设置,看自己需要
② requirepass 和 masterauth 需要同时设置,否则下次启动会报错:MASTER aborted replication with an error: NOAUTH Authentication required,原因可以参考:Link
port 7001
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 15000
requirepass 123456
masterauth 123456
2.3 新增start.bat
在目录:E:\Program Files (x86)\redis5.0.14.1\cluster\7001新增bat文件,方便启动

start.bat 内容如下:
title redis-7001
redis-server.exe redis.windows.conf
此时,第一个集群节点7001,配置完成。
我们可以双击 start.bat看是否正常启动.

2.4 创建其他5个节点
我们直接拷贝7001目录,拷贝命名为7002、7003、7004、7005、7006

分别修改对应的配置文件 redis.windows.conf
注意: 下面的配置,700X,在不同目录下改成对应的7002、7003、7004、7005、7006
port 700X
cluster-enabled yes
cluster-config-file nodes-700X.conf
cluster-node-timeout 15000
requirepass 123456
masterauth 123456
分别修改对应的启动bat文件 start.bat
注意: 下面的配置,700X,在不同目录下改成对应的7002、7003、7004、7005、7006
title redis-700X
redis-server.exe redis.windows.conf
至此,6个集群节点的配置,全部准备完毕!
3. 启动集群
3.1 启动6个节点
首先,我们启动这6个节点。
方式1:对每个节点手动启动 (不推荐)
redis-server.exe redis.windows.conf
方式2:执行每个目录下的 start.bat(不推荐)
方式3:写一个批量的bat,一次性启动

例如,bat文件名为:run_cluster_7001_7006.bat
命令如下:
@echo off
setlocal
rem 获取脚本所在的目录作为父目录
set "parent_directory=%~dp0"
rem 遍历父目录下的每个子目录
for /d %%D in ("%parent_directory%*") do (
rem 检查子目录是否包含 start.bat 文件
if exist "%%~D\start.bat" (
rem 进入子目录并运行 start.bat
pushd "%%~D"
start start.bat
popd
)
)
endlocal
保存后,双击即可执行。

3.2 启动集群模式
节点都启动后,节点界面会显示:Ready to accept connections
意思是需要我们启动集群模式。
启动命令是:
注意:如果没有密码,去掉**"-a 123456**"
redis-cli.exe -a 123456 --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
假如我们用下面这个 redis-cli.exe 启动

打开CMD命令,输入 "E:",切换磁盘。注意冒号是英文格式。
E:
cd 到指定目录:
E:\Program Files (x86)\redis5.0.14.1
执行命令,注意redis-cli.exe前面没有 ./
redis-cli.exe -a 123456 --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
CMD界面执行如图所示:
bash
E:\Program Files (x86)\redis5.0.14.1>redis-cli.exe -a 123456 --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 25e19e328a37fd52b671f00c0a9d87111d514487 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
M: 5f93a0d2fb64fb182fe22b8d8a75c4af6007eec5 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
M: d6d27d2be5a257f84b393104b9eb31014be95d0d 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
S: aeca57ee8f3c1dba4f04223ebbe44701853db324 127.0.0.1:7004
replicates 5f93a0d2fb64fb182fe22b8d8a75c4af6007eec5
S: 0964ae11a4bf4e924bd52f35e0e9e9961d471236 127.0.0.1:7005
replicates d6d27d2be5a257f84b393104b9eb31014be95d0d
S: b9755b7d45c2d794743666ccb3a70f34979857de 127.0.0.1:7006
replicates 25e19e328a37fd52b671f00c0a9d87111d514487
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 25e19e328a37fd52b671f00c0a9d87111d514487 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 0964ae11a4bf4e924bd52f35e0e9e9961d471236 127.0.0.1:7005
slots: (0 slots) slave
replicates d6d27d2be5a257f84b393104b9eb31014be95d0d
M: 5f93a0d2fb64fb182fe22b8d8a75c4af6007eec5 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: d6d27d2be5a257f84b393104b9eb31014be95d0d 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: b9755b7d45c2d794743666ccb3a70f34979857de 127.0.0.1:7006
slots: (0 slots) slave
replicates 25e19e328a37fd52b671f00c0a9d87111d514487
S: aeca57ee8f3c1dba4f04223ebbe44701853db324 127.0.0.1:7004
slots: (0 slots) slave
replicates 5f93a0d2fb64fb182fe22b8d8a75c4af6007eec5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
E:\Program Files (x86)\redis5.0.14.1>
各个节点的界面如图所示:

4. 连接测试
这里用客户端:ARDM------Another Redis Desktop Manager
下载链接:
https://github.com/qishibo/AnotherRedisDesktopManager/releases
如图所示,编辑信息,点击确认。

点击进入

显示正常

5. 其他
5.1 快速停止集群模式
我们新建一个批量bat,如图所示:

stop.bat内容如下:
@echo off
set START_PORT=7001
set END_PORT=7006
echo Closing port %START_PORT%-%END_PORT%...
echo.
for /l %%i in (%START_PORT%, 1, %END_PORT%) do (
call :closeProgram %%i
)
echo.
echo Closing all
echo.
taskkill /f /fi "imagename eq cmd.exe"
echo.
echo All closed.
goto :eof
:closeProgram
set PORT=%1
echo Searching for programs using port %PORT%...
for /f "tokens=5" %%a in ('netstat -ano ^| findstr /i ":%PORT%"') do (
taskkill /f /pid %%a
echo Program with port %PORT% has been closed.
)
echo.
echo End of program list for port %PORT%.
goto :eof