Windows下Redis集群安装

准备工作

需要4个部件:Redis、Ruby语言运行环境、Redis的Ruby驱动redis-xxxx.gem、创建Redis集群的工具redis-trib.rb。使用redis-trib.rb工具来创建Redis集群,由于该文件是用ruby语言写的,所以需要安装Ruby开发环境,以及驱动redis-xxxx.gem。

  1. 下载Redis安装文件:https://github.com/MSOpenTech/redis/releases/,Redis提供msi和zip格式的下载文件,这里下载zip格式Redis-x64-3.2.100版本。
  2. 下载Ruby安装文件:http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.2.4-x64.exe
  3. 下载Ruby环境下Redis的驱动:https://rubygems.org/gems/redis/versions/3.2.2,考虑到兼容性,这里下载的是3.2.2版本,注意:下载在页面右下角相关连接一项中。
  4. 下载Redis官方提供的创建Redis集群的ruby脚本文件redis-trib.rb,路径如下:https://raw.githubusercontent.com/MSOpenTech/redis/3.0/src/redis-trib.rb

安装Redis

将下载到的Redis-x64-3.2.100.zip解压即可。

安装Redis,并运行3个实例(Redis集群需要至少3个以上节点,低于3个无法创建)。我们可以通过配置文件来启动6个不同的Redis实例,由于Redis默认端口为6379,所以这里使用了6380、6381、6382、6383、6384、6385来运行6个Redis实例。

注意:

  • 为了避免不必要的错误,配置文件尽量保存为utf8格式,并且不要包含注释;
  • 配置文件中以下两种保存日志的方式(保存在文件中、保存到System Log中)请根据需求选择其中一种即可:
1
2
3
4
loglevel notice                                                     #日志的记录级别,notice是适合生产环境的
logfile "D:/Redis-Cluster/Redis-x64-3.2.100/Logs/redis6380_log.txt" #指定log的保持路径,默认是创建在Redis安装目录下,如果有子目录需要手动创建,如此处的Logs目录
syslog-enabled yes #是否使用系统日志   
syslog-ident redis6380 #在系统日志的标识名

这里使用了保存在文件中的方式,所以先在Redis目录下新建Logs文件夹。

配置文件

在Redis安装根目录下,创建编码格式为utf-8的配置文件:redis.6380.conf、redis.6381.conf、redis.6382.conf、redis.6383.conf、redis.6384.conf、redis.6385.conf。

1
2
3
4
5
6
7
8
9
10
11
port 6380      
loglevel notice
logfile "D:/software/Redis-x64-3.2.100/Logs/redis6380_log.txt"
appendonly yes
appendfilename "appendonly.6380.aof"
cluster-enabled yes
cluster-config-file nodes.6380.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
1
2
3
4
5
6
7
8
9
10
11
port 6381       
loglevel notice
logfile "D:/software/Redis-x64-3.2.100/Logs/redis6381_log.txt"
appendonly yes
appendfilename "appendonly.6381.aof"
cluster-enabled yes
cluster-config-file nodes.6381.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
1
2
3
4
5
6
7
8
9
10
11
port 6382       
loglevel notice
logfile "D:/software/Redis-x64-3.2.100/Logs/redis6382_log.txt"
appendonly yes
appendfilename "appendonly.6382.aof"
cluster-enabled yes
cluster-config-file nodes.6382.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
1
2
3
4
5
6
7
8
9
10
11
port 6383       
loglevel notice
logfile "D:/software/Redis-x64-3.2.100/Logs/redis6383_log.txt"
appendonly yes
appendfilename "appendonly.6383.aof"
cluster-enabled yes
cluster-config-file nodes.6383.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
1
2
3
4
5
6
7
8
9
10
11
port 6384       
loglevel notice
logfile "D:/software/Redis-x64-3.2.100/Logs/redis6384_log.txt"
appendonly yes
appendfilename "appendonly.6384.aof"
cluster-enabled yes
cluster-config-file nodes.6384.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
1
2
3
4
5
6
7
8
9
10
11
port 6385       
loglevel notice
logfile "D:/software/Redis-x64-3.2.100/Logs/redis6385_log.txt"
appendonly yes
appendfilename "appendonly.6385.aof"
cluster-enabled yes
cluster-config-file nodes.6385.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes

安装Redis服务

将上述配置文件保存到Redis目录下,并使用这些配置文件安装6个redis服务,命令如下:

1
2
3
4
5
6
D:/software/Redis-x64-3.2.100/redis-server.exe --service-install D:/software/Redis-x64-3.2.100/redis.6380.conf --service-name redis6380
D:/software/Redis-x64-3.2.100/redis-server.exe --service-install D:/software/Redis-x64-3.2.100/redis.6381.conf --service-name redis6381
D:/software/Redis-x64-3.2.100/redis-server.exe --service-install D:/software/Redis-x64-3.2.100/redis.6382.conf --service-name redis6382
D:/software/Redis-x64-3.2.100/redis-server.exe --service-install D:/software/Redis-x64-3.2.100/redis.6383.conf --service-name redis6383
D:/software/Redis-x64-3.2.100/redis-server.exe --service-install D:/software/Redis-x64-3.2.100/redis.6384.conf --service-name redis6384
D:/software/Redis-x64-3.2.100/redis-server.exe --service-install D:/software/Redis-x64-3.2.100/redis.6385.conf --service-name redis6385

卸载命令为:

1
2
3
4
5
6
D:/software/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/software/Redis-x64-3.2.100/redis.6380.conf --service-name redis6380
D:/software/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/software/Redis-x64-3.2.100/redis.6381.conf --service-name redis6381
D:/software/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/software/Redis-x64-3.2.100/redis.6382.conf --service-name redis6382
D:/software/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/software/Redis-x64-3.2.100/redis.6383.conf --service-name redis6383
D:/software/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/software/Redis-x64-3.2.100/redis.6384.conf --service-name redis6384
D:/software/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/software/Redis-x64-3.2.100/redis.6385.conf --service-name redis6385

启动这6个服务,命令如下:

1
2
3
4
5
6
D:/software/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6380
D:/software/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6381
D:/software/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6382
D:/software/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6383
D:/software/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6384
D:/software/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6385

安装ruby

Ruby环境安装

安装时不要忘记把其加到环境变量PATH中。这样在cmd命令中能直接使用ruby的命令。

安装Ruby环境下Redis的驱动

我们将下载的”Ruby环境下Redis的驱动文件(redis-3.2.2.gem)”拷贝到Ruby安装根目录下。

然后执行安装命令如下:

1
gem install --local path_to_gem/filename.gem

例如:

1
gem install --local D:\software\Ruby22-x64\redis-3.2.2.gem

创建Redis集群

将下载的“创建Redis集群的ruby脚本文件redis-trib.rb”文件拷贝到Redis安装根目录下。

使用redis-trib.rb来创建Redis集群

CMD下切换到Redis目录,执行:

1
redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
>>> Creating cluster
Connecting to node 127.0.0.1:6380: OK
Connecting to node 127.0.0.1:6381: OK
Connecting to node 127.0.0.1:6382: OK
Connecting to node 127.0.0.1:6383: OK
Connecting to node 127.0.0.1:6384: OK
Connecting to node 127.0.0.1:6385: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6380
127.0.0.1:6381
127.0.0.1:6382
Adding replica 127.0.0.1:6383 to 127.0.0.1:6380
Adding replica 127.0.0.1:6384 to 127.0.0.1:6381
Adding replica 127.0.0.1:6385 to 127.0.0.1:6382
M: 96393938b8e08794cd17b80259e9941270364271 127.0.0.1:6380 slots:0-5460 (5461 slots) master
M: ae68a8526152f488f1a773648bcb26d37f0987be 127.0.0.1:6381 slots:5461-10922 (5462 slots) master
M: ffb77b9e44d3b0db2eaa1b33df1c481b3f73b276 127.0.0.1:6382 slots:10923-16383 (5461 slots) master
S: 8184ff02aac8b0bcfd55a73d8c002be59be00d0b 127.0.0.1:6383 replicates 96393938b8e08794cd17b80259e9941270364271
S: 03ac3942b93a2acc8cb4f2c9b16dcd12e64b45ed 127.0.0.1:6384 replicates ae68a8526152f488f1a773648bcb26d37f0987be
S: cbdb40830cdbcde0bfb084450de98a3674c2c712 127.0.0.1:6385 replicates ffb77b9e44d3b0db2eaa1b33df1c481b3f73b276
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:6380)
M: 96393938b8e08794cd17b80259e9941270364271 127.0.0.1:6380 slots:0-5460 (5461 slots) master
M: ae68a8526152f488f1a773648bcb26d37f0987be 127.0.0.1:6381 slots:5461-10922 (5462 slots) master
M: ffb77b9e44d3b0db2eaa1b33df1c481b3f73b276 127.0.0.1:6382 slots:10923-16383 (5461 slots) master
M: 8184ff02aac8b0bcfd55a73d8c002be59be00d0b 127.0.0.1:6383 slots: (0 slots) master replicates 96393938b8e08794cd17b80259e9941270364271
M: 03ac3942b93a2acc8cb4f2c9b16dcd12e64b45ed 127.0.0.1:6384 slots: (0 slots) master replicates ae68a8526152f488f1a773648bcb26d37f0987be
M: cbdb40830cdbcde0bfb084450de98a3674c2c712 127.0.0.1:6385 slots: (0 slots) master replicates ffb77b9e44d3b0db2eaa1b33df1c481b3f73b276
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

备注:

  • --replicas:指定集群中每个主节点配备几个从节点,这里设置为1。
  • redis-trib.rb工具的使用
    • create:创建集群
    • check:检查集群
    • info:查看集群信息
    • fix:修复集群
    • reshard:在线迁移slot
    • rebalance:平衡集群节点slot数量
    • add-node:将新节点加入集群
    • del-node:从集群中删除节点
    • set-timeout:设置集群节点间心跳连接的超时时间
    • call:在集群全部节点上执行命令
    • import:将外部redis数据导入集群

检验是否真的创建成功

输入以下命令:

1
redis-trib.rb check 127.0.0.1:6380

如果现实信息如下,则说明创建的Redis集群是没问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Connecting to node 127.0.0.1:6380: OK
Connecting to node 127.0.0.1:6383: OK
Connecting to node 127.0.0.1:6381: OK
Connecting to node 127.0.0.1:6382: OK
Connecting to node 127.0.0.1:6384: OK
Connecting to node 127.0.0.1:6385: OK
>>> Performing Cluster Check (using node 127.0.0.1:6380)
M: 96393938b8e08794cd17b80259e9941270364271 127.0.0.1:6380 slots:0-5460 (5461 slots) master 1 additional replica(s)
S: 8184ff02aac8b0bcfd55a73d8c002be59be00d0b 127.0.0.1:6383 slots: (0 slots) slave replicates 96393938b8e08794cd17b80259e9941270364271
M: ae68a8526152f488f1a773648bcb26d37f0987be 127.0.0.1:6381 slots:5461-10922 (5462 slots) master 1 additional replica(s)
M: ffb77b9e44d3b0db2eaa1b33df1c481b3f73b276 127.0.0.1:6382 slots:10923-16383 (5461 slots) master 1 additional replica(s)
S: 03ac3942b93a2acc8cb4f2c9b16dcd12e64b45ed 127.0.0.1:6384 slots: (0 slots) slave replicates ae68a8526152f488f1a773648bcb26d37f0987be
S: cbdb40830cdbcde0bfb084450de98a3674c2c712 127.0.0.1:6385 slots: (0 slots) slave replicates ffb77b9e44d3b0db2eaa1b33df1c481b3f73b276
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

信息查询

使用Redis客户端Redis-cli.exe来查看数据记录数,以及集群相关信息。

在Redis根目录下执行以下命令进入redis-cli:

1
redis-cli.exe -h 127.0.0.1 -p 6380

然后执行:

1
2
3
4
5
6
7
8
9
127.0.0.1:6380> cluster nodes
8184ff02aac8b0bcfd55a73d8c002be59be00d0b 127.0.0.1:6383 slave 96393938b8e08794cd17b80259e9941270364271 0 1599988100443 4 connected
ae68a8526152f488f1a773648bcb26d37f0987be 127.0.0.1:6381 master - 0 1599988101450 2 connected 5461-10922
ffb77b9e44d3b0db2eaa1b33df1c481b3f73b276 127.0.0.1:6382 master - 0 1599988103461 3 connected 10923-16383
03ac3942b93a2acc8cb4f2c9b16dcd12e64b45ed 127.0.0.1:6384 slave ae68a8526152f488f1a773648bcb26d37f0987be 0 1599988099439 5 connected
96393938b8e08794cd17b80259e9941270364271 127.0.0.1:6380 myself,master - 0 0 1 connected 0-5460
cbdb40830cdbcde0bfb084450de98a3674c2c712 127.0.0.1:6385 slave ffb77b9e44d3b0db2eaa1b33df1c481b3f73b276 0 1599988102456 6 connected
127.0.0.1:6380> dbsize
(integer) 0

本文转载于:在Windows系统下搭建Redis集群


----------- 本文结束啦感谢您阅读 -----------

赞赏一杯咖啡

欢迎关注我的其它发布渠道