本示例是伪集群模式,是指在一台机器中多个redis服务配置为集群,性能上肯不如真实集群但配置步骤相同。
linux安装请参考 http://blog.csdn.net/tangyaliang11/article/details/78901615
linux集群请参考 http://blog.csdn.net/tangyaliang11/article/details/78902750
系统环境:win10企业版
服务程序:redis-3.2x64
windows版下载地址:https://github.com/MicrosoftArchive/redis/releases(官方没有windows版,只有微软维护的64位版,最新版本是3.2版)
管理工具:rubyinstaller-2.3.3-x64
下载地址:https://rubyinstaller.org/downloads/
集群管理命令工具:redis-trib.rb
这个文件需要到linux版本的redis中src下找到并copy到windows的redis目录下。
1:redis我使用的是zip解压版,直接解压即可。
2:rubyinstaller安装选中复选框然后一路next即可。
3:安装ruby与redis接口,在cmd窗口运行:gem install redis
1、在redis安装目录新建6个文件夹名称分别为7001、7002、7003、7004、7005、7006。这六个文件夹模拟6个redis服务并代表对外开放的端口号。
2、将redis.windows.conf文件复制到新建的6个文件夹中,每个文件夹中复制一个。
3、修改每个文件夹中的redis.windows.conf配置文件,将端口号、文件所在位置与所在文件夹名称修改相同,以下是最少选项的配置:
port 7001
cluster-enabled yes
appendonly yes
dir ./7001/
1、启动:
cmd进入redis目录,使用命令redis-server.exe 7001/redis.windows.conf启动端口号为7001的redis服务,如下图表示启动成功。
接下来在将7002-7006全部启动。
redis-server.exe 7002/redis.windows.conf
redis-server.exe 7003/redis.windows.conf
redis-server.exe 7004/redis.windows.conf
redis-server.exe 7005/redis.windows.conf
redis-server.exe 7006/redis.windows.conf
前面是命令,后面是使用的资源文件。
2、配置集群:cmd进入redis目录,执行命令ruby redis-trib.rb create --replicas 1 192.168.1.104:7001 192.168.1.104:7002 192.168.1.104:7003 192.168.1.104:7004 192.168.1.104:7005 192.168.1.104:7006
输入命令后会显示主从信息并询问您是否进行配置,输入yes继续。
D:\worksoft\Redis-x64-3.2.100>ruby redis-trib.rb create --replicas 1 192.168.1.104:7001 192.168.1.104:7002 192.168.1.104:7003 192.168.1.104:7004 192.168.1.104:7005 192.168.1.104:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:显示三个主节点
192.168.1.104:7001
192.168.1.104:7002
192.168.1.104:7003
Adding replica 192.168.1.104:7004 to 192.168.1.104:7001三个从节点
Adding replica 192.168.1.104:7005 to 192.168.1.104:7002
Adding replica 192.168.1.104:7006 to 192.168.1.104:7003
M: 278b6affeb3f8d86bcfc2f6af60daa57c6426794 192.168.1.104:7001
slots:0-5460 (5461 slots) master
M: 53adba5f0949c0912e999bc1a9e0e46c5c6b6ad0 192.168.1.104:7002
slots:5461-10922 (5462 slots) master
M: b6b94a867af3bec92d4aef7ba2be3217ab570bfa 192.168.1.104:7003
slots:10923-16383 (5461 slots) master
S: d5df0e8affb346654b9584244d4b76f9b01605dd 192.168.1.104:7004
replicates 278b6affeb3f8d86bcfc2f6af60daa57c6426794
S: 366479eed17c02a3a1472acda718e0b70deb9e75 192.168.1.104:7005
replicates 53adba5f0949c0912e999bc1a9e0e46c5c6b6ad0
S: f7f475a323765eabd912fd4d959b4f810fca865a 192.168.1.104:7006
replicates b6b94a867af3bec92d4aef7ba2be3217ab570bfa
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 192.168.1.104:7001)
M: 278b6affeb3f8d86bcfc2f6af60daa57c6426794 192.168.1.104:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: b6b94a867af3bec92d4aef7ba2be3217ab570bfa 192.168.1.104:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: d5df0e8affb346654b9584244d4b76f9b01605dd 192.168.1.104:7004
slots: (0 slots) slave
replicates 278b6affeb3f8d86bcfc2f6af60daa57c6426794
S: 366479eed17c02a3a1472acda718e0b70deb9e75 192.168.1.104:7005
slots: (0 slots) slave
replicates 53adba5f0949c0912e999bc1a9e0e46c5c6b6ad0
S: f7f475a323765eabd912fd4d959b4f810fca865a 192.168.1.104:7006
slots: (0 slots) slave
replicates b6b94a867af3bec92d4aef7ba2be3217ab570bfa
M: 53adba5f0949c0912e999bc1a9e0e46c5c6b6ad0 192.168.1.104:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
使用命令redis-cli -c -h 192.168.1.104 -p 7001 连接集群。
-c表示集群
-h表示IP地址
-p表示端口
测试在7001set一个字符串,显示ok。
客户端连接自动转换的到7002,这是因为集群把这个值分配在了7002的插槽中,而且在7002中也可以get到刚设置的值。
再次测试连接7006,同样可以get到刚刚设置的值。
redis-trib.rb add-node --slave --master-id 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e 192.168.1.104:7007 192.168.1.104:7001 向集群中定向的主节点添加从节点
第一个参数:新添加节点的地址以及端口192.168.1.104:7007
第二个参数:现有集群中任意一个服务端地址以及端口192.168.1.104:7001
D:\worksoft\Redis-x64-3.2.100>redis-trib add-node 192.168.1.5:7007 192.168.1.5:7001
>>> Adding node 192.168.1.5:7007 to cluster 192.168.1.5:7001
>>> Performing Cluster Check (using node 192.168.1.5:7001)
M: c6672c636b0a0b698551a1dd9db6d0880b17f58b 192.168.1.5:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 9d10e68c68d03f5f034619d2a75633b5850dc59f 192.168.1.5:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: d74f5f96feb31fb6bb2cbc131b2f6bff03ec18a2 192.168.1.5:7005
slots: (0 slots) slave
replicates 9d10e68c68d03f5f034619d2a75633b5850dc59f
S: 2e0e64bb0b1c19f88c1fd9f5de2ba59fa17f8e5c 192.168.1.5:7004
slots: (0 slots) slave
replicates c6672c636b0a0b698551a1dd9db6d0880b17f58b
S: 79169f90d4d351a8104aa43ff4225075e0d2da32 192.168.1.5:7006
slots: (0 slots) slave
replicates e57fead54adfd5a03c7fec0d1a2904aa587ce412
M: e57fead54adfd5a03c7fec0d1a2904aa587ce412 192.168.1.5:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.1.5:7007 to make it join the cluster.
[OK] New node added correctly.
D:\worksoft\Redis-x64-3.2.100>redis-cli.exe -c -h 192.168.1.5 -p 7001
192.168.1.5:7001> cluster nodes
ab853f5e95f1e32e0ee40543a9687d60fc3bd941 192.168.1.5:7007 master - 0 1512013773107 0 connected
9d10e68c68d03f5f034619d2a75633b5850dc59f 192.168.1.5:7002 master - 0 1512013775504 2 connected 5461-10922
d74f5f96feb31fb6bb2cbc131b2f6bff03ec18a2 192.168.1.5:7005 slave 9d10e68c68d03f5f034619d2a75633b5850dc59f 0 1512013777801 5 connected
2e0e64bb0b1c19f88c1fd9f5de2ba59fa17f8e5c 192.168.1.5:7004 slave c6672c636b0a0b698551a1dd9db6d0880b17f58b 0 1512013775613 4 connected
c6672c636b0a0b698551a1dd9db6d0880b17f58b 192.168.1.5:7001 myself,master - 0 0 1 connected 0-5460
79169f90d4d351a8104aa43ff4225075e0d2da32 192.168.1.5:7006 slave e57fead54adfd5a03c7fec0d1a2904aa587ce412 0 1512013774521 6 connected
e57fead54adfd5a03c7fec0d1a2904aa587ce412 192.168.1.5:7003 master - 0 1512013776707 3 connected 10923-16383
192.168.1.5:7001> exit
第一个参数:任意集群中现有的地址192.168.1.104:7001
第二个参数:你想移除的节点id ab853f5e95f1e32e0ee40543a9687d60fc3bd941 (该id可以在想要移除的节点nodes.conf文件中找到)
D:\worksoft\Redis-x64-3.2.100>redis-trib del-node 192.168.1.5:7001 ab853f5e95f1e32e0ee40543a9687d60fc3bd941
>>> Removing node ab853f5e95f1e32e0ee40543a9687d60fc3bd941 from cluster 192.168.1.5:7001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
D:\worksoft\Redis-x64-3.2.100>redis-cli -c -h 192.168.1.5 -p 7001 已经不存在7007节点
192.168.1.5:7001> cluster nodes
9d10e68c68d03f5f034619d2a75633b5850dc59f 192.168.1.5:7002 master - 0 1512016406937 2 connected 5461-10922
d74f5f96feb31fb6bb2cbc131b2f6bff03ec18a2 192.168.1.5:7005 slave 9d10e68c68d03f5f034619d2a75633b5850dc59f 0 1512016411312 5 connected
2e0e64bb0b1c19f88c1fd9f5de2ba59fa17f8e5c 192.168.1.5:7004 slave c6672c636b0a0b698551a1dd9db6d0880b17f58b 0 1512016412407 4 connected
c6672c636b0a0b698551a1dd9db6d0880b17f58b 192.168.1.5:7001 myself,master - 0 0 1 connected 0-5460
79169f90d4d351a8104aa43ff4225075e0d2da32 192.168.1.5:7006 slave e57fead54adfd5a03c7fec0d1a2904aa587ce412 0 1512016409125 6 connected
e57fead54adfd5a03c7fec0d1a2904aa587ce412 192.168.1.5:7003 master - 0 1512016410219 3 connected 10923-16383
192.168.1.5:7001> exit
D:\worksoft\Redis-x64-3.2.100>redis-trib reshard 192.168.1.5:7007
接下来会让你输入分配的槽数,范围在1-16384,我输入4000.
然后会询问你从哪个节点分出这些槽数,我选择all所有节点中。
How many slots do you want to move (from 1 to 16384)? 4000
最后会询问你是否执行这个分槽计划,输入yes开始执行计划。
Do you want to proceed with the proposed reshard plan (yes/no)? yes
一、在配置集群时出现 [ERR] Sorry, can't connect to node 错误,原因之一是redis.windows.conf的bind 地址使用了127.0.0.1,将地址修改为服务器的固定ip后解决了。
二、在重新启动集群是出现了CLUSTERDOWN The cluster is down 错误,检查了redis.windows.conf文件与nodes.conf发现里面的IP地址与机器IP地址不同,将配置文件中的ip修改为机器ip后解决问题。
ps:本人初学可能有不当之处,请见谅。