原文:http://blog.sina.com.cn/s/blog_53b45c4d0102wg10.html
1、测试存取值
客户端连接集群redis-cli需要带上 -c ,redis-cli -c -p 端口号
- [root@localhost redis01]# ./redis-cli -c -p 7001
- 127.0.0.1:7001> set name andy
- -> Redirected to slot [5798] located at 127.0.0.1:7002
- OK
- 127.0.0.1:7002> get name
- "andy"
- 127.0.0.1:7002>
根据redis-cluster的key值分配,name应该分配到节点7002[5461-10922]上,上面显示redis cluster自动从7001跳转到了7002节点。
我们可以测试一下7006从节点获取name值
- [root@localhost redis06]# ./redis-cli -c -p 7006
- 127.0.0.1:7006> get name
- -> Redirected to slot [5798] located at 127.0.0.1:7002
- "andy"
- 127.0.0.1:7002>
7006位7003的从节点,从上面也是自动跳转至7002获取值,这也是redis cluster的特点,它是去中心化,每个节点都是对等的,连接哪个节点都可以获取和设置数据。
四、集群节点选举
现在模拟将7002节点挂掉,按照redis-cluster原理会选举会将 7002的从节点7005选举为主节点。
- [root@localhost redis-cluster]# ps -ef | grep redis
- root 7950 1 0 12:50 ? 00:00:28 ./redis-server 127.0.0.1:7001 [cluster]
- root 7952 1 0 12:50 ? 00:00:29 ./redis-server 127.0.0.1:7002 [cluster]
- root 7956 1 0 12:50 ? 00:00:29 ./redis-server 127.0.0.1:7003 [cluster]
- root 7960 1 0 12:50 ? 00:00:29 ./redis-server 127.0.0.1:7004 [cluster]
- root 7964 1 0 12:50 ? 00:00:29 ./redis-server 127.0.0.1:7005 [cluster]
- root 7966 1 0 12:50 ? 00:00:29 ./redis-server 127.0.0.1:7006 [cluster]
- root 11346 10581 0 14:57 pts/2 00:00:00 grep --color=auto redis
- [root@localhost redis-cluster]# kill 7952
在查看集群中的7002节点
- [root@localhost redis-cluster]#
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7002
- [ERR] Sorry, can't connect to node 127.0.0.1:7002
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7005
- >>> Performing Cluster Check (using node 127.0.0.1:7005)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:5461-10922 (5462 slots) master
- 0 additional replica(s)
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:10923-16383 (5461 slots) master
- 1 additional replica(s)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:0-5460 (5461 slots) master
- 1 additional replica(s)
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
- [root@localhost redis-cluster]#
可以看到集群连接不了7002节点,而7005有原来的S转换为M节点,代替了原来的7002节点。我们可以获取name值:
- [root@localhost redis01]# ./redis-cli -c -p 7001
- 127.0.0.1:7001> get name
- -> Redirected to slot [5798] located at 127.0.0.1:7005
- "andy"
- 127.0.0.1:7005>
- 127.0.0.1:7005>
从7001节点连入,自动跳转到7005节点,并且获取name值。
现在我们将7002节点恢复,看是否会自动加入集群中以及充当的M还是S节点。
- [root@localhost redis-cluster]# cd redis02
- [root@localhost redis02]# ./redis-server redis.conf
- [root@localhost redis02]#
在check一下7002节点
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7002
- >>> Performing Cluster Check (using node 127.0.0.1:7002)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:10923-16383 (5461 slots) master
- 1 additional replica(s)
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- slots:5461-10922 (5462 slots) master
- 1 additional replica(s)
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates dd19221c404fb2fc4da37229de56bab755c76f2b
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots:0-5460 (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.
- [root@localhost redis-cluster]#
可以看到7002节点变成了a5db243087d8bd423b9285fa8513eddee9bb59a6 7005的从节点。