安装Redis

下载Windows版本Redis

下载地址: https://github.com/MicrosoftArchive/redis/releases

我下载了当前最新版本:Redis-x64-3.0.504.zip

下载后,将文件解压到D:\Program_Files\redis\redis-cluster目录(目录可以自定义,路径上不要有空格

创建Redis集群节点

Redis集群至少需要6个节点,3主3从。主节点崩溃,从节点可以替补。

配置节点

在Windows环境下搭建Redis集群_集群

将redis.windows.conf文件拷贝6份(也可以拷贝redis.windows-server.conf),修改以下配置:

port 6380
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
appendonly yes
cluster-enabled yes
appendfilename "appendonly.6380.aof"
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes

将节点安装成服务

执行以下命令:

redis-server --service-install conf/redis.windows.6380.conf --service-name redis6380 --loglevel verbose
redis-server --service-install conf/redis.windows.6381.conf --service-name redis6381 --loglevel verbose
redis-server --service-install conf/redis.windows.6382.conf --service-name redis6382 --loglevel verbose
redis-server --service-install conf/redis.windows.6383.conf --service-name redis6383 --loglevel verbose
redis-server --service-install conf/redis.windows.6384.conf --service-name redis6384 --loglevel verbose
redis-server --service-install conf/redis.windows.6385.conf --service-name redis6385 --loglevel verbose

服务停启命令:

redis-server --service-start --service-name redis6379
redis-server --service-stop --service-name redis6379

已完成服务注册:

在Windows环境下搭建Redis集群_redis_02

安装Ruby环境

下载Ruby

下载地址:https://rubyinstaller.org/downloads/

在Windows环境下搭建Redis集群_集群_03

双击文件,安装向导安装。

下载Ruby环境下Redis驱动

执行命令安装

gem install redis

手动下载后安装

下载链接:https://rubygems.org/gems/redis/versions

下载后执行以下命令

gem install --local path\to\redis-5.0.6.gem

两种方式,任选一种。

创建集群环境

下载redis-trib.rb脚本

下载地址:https://github.com/beebol/redis-trib.rb

将redis-trib.rb文件拷贝到redis目录。

在redis目录下执行命令:

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

--replicas 1表示每个master有一个slave节点。

创建集群前,不能设置redis密码

>>> Creating cluster
>>> 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: 93b13a53b9620fabe492d8f5c05364ab0b192484 127.0.0.1:6380
   slots:0-5460 (5461 slots) master
M: 950330db4bf66f5b01681bada6ab1c908b19c406 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
M: fb9c13ba71dce2bc7666abbb27b0c1fb24ee72b9 127.0.0.1:6382
   slots:10923-16383 (5461 slots) master
S: e817a18f604f5f235154f5ed8d7e74d0e855063d 127.0.0.1:6383
   replicates 93b13a53b9620fabe492d8f5c05364ab0b192484
S: 6a3f1b3ef5dd60dbdf74272d0bdc6cfd9758d2c4 127.0.0.1:6384
   replicates 950330db4bf66f5b01681bada6ab1c908b19c406
S: 0b9f327cba1640b90cbb8c08c3246807f6c01dc1 127.0.0.1:6385
   replicates fb9c13ba71dce2bc7666abbb27b0c1fb24ee72b9
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: 93b13a53b9620fabe492d8f5c05364ab0b192484 127.0.0.1:6380
   slots:0-5460 (5461 slots) master
M: 950330db4bf66f5b01681bada6ab1c908b19c406 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
M: fb9c13ba71dce2bc7666abbb27b0c1fb24ee72b9 127.0.0.1:6382
   slots:10923-16383 (5461 slots) master
M: e817a18f604f5f235154f5ed8d7e74d0e855063d 127.0.0.1:6383
   slots: (0 slots) master
   replicates 93b13a53b9620fabe492d8f5c05364ab0b192484
M: 6a3f1b3ef5dd60dbdf74272d0bdc6cfd9758d2c4 127.0.0.1:6384
   slots: (0 slots) master
   replicates 950330db4bf66f5b01681bada6ab1c908b19c406
M: 0b9f327cba1640b90cbb8c08c3246807f6c01dc1 127.0.0.1:6385
   slots: (0 slots) master
   replicates fb9c13ba71dce2bc7666abbb27b0c1fb24ee72b9
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

主节点

从节点

槽位

6380

6383

0-5460

6381

6384

5461-10922

6382

6385

10923-16383

验证集群状态

D:\Program_Files\redis\redis-cluster\Redis-x64-3.0.504-6380>redis-cli.exe -c -p 6380
127.0.0.1:6380> cluster nodes
e817a18f604f5f235154f5ed8d7e74d0e855063d 127.0.0.1:6383 slave 93b13a53b9620fabe492d8f5c05364ab0b192484 0 1681382274070 4 connected
6a3f1b3ef5dd60dbdf74272d0bdc6cfd9758d2c4 127.0.0.1:6384 slave 950330db4bf66f5b01681bada6ab1c908b19c406 0 1681382276257 2 connected
0b9f327cba1640b90cbb8c08c3246807f6c01dc1 127.0.0.1:6385 slave fb9c13ba71dce2bc7666abbb27b0c1fb24ee72b9 0 1681382270794 6 connected
950330db4bf66f5b01681bada6ab1c908b19c406 127.0.0.1:6381 master - 0 1681382272981 2 connected 5461-10922
93b13a53b9620fabe492d8f5c05364ab0b192484 127.0.0.1:6380 myself,master - 0 0 1 connected 0-5460
fb9c13ba71dce2bc7666abbb27b0c1fb24ee72b9 127.0.0.1:6382 master - 0 1681382275165 3 connected 10923-16383

设置redis密码

停止全部redis节点,修改全部配置文件

requirepass 12345678
masterauth  12345678

完成后启动全部节点。