学习淘淘商城第三十二课(Redis集群测试)
学习淘淘商城第三十二课(Redis集群测试)
原创 2017年04月22日 16:36:48
- 1057
上节课我们一起学习了怎样搭建一个六台设备的Redis集群,这节我们一起学习下如何测试集群。
首先启动Redis集群使用命令/usr/local/redis/bin/redis-trib.rb create --replicas 1 192.168.156.15:6379 192.168.156.16:6379 192.168.156.17:6379 192.168.156.18:6379 192.168.156.19:6379 192.168.156.20:6379进行启动,如下所示。可以看到192.168.156.18、192.168.156.19、192.168.156.20这三个节点是主节点,另外三个节点是从节点。槽号为0-5460的卡槽被分配到了192.168.156.20上,5461-10922被分配到了192.168.156.19上,10923-16383被分配到了192.168.156.18上。
- [root@redis1 ~]# /usr/local/redis/bin/redis-trib.rb create --replicas 1 192.168.156.15:6379 192.168.156.16:6379 192.168.156.17:6379 192.168.156.18:6379 192.168.156.19:6379 192.168.156.20:6379
- >>> Creating cluster
- Connecting to node 192.168.156.15:6379: OK
- Connecting to node 192.168.156.16:6379: OK
- Connecting to node 192.168.156.17:6379: OK
- Connecting to node 192.168.156.18:6379: OK
- Connecting to node 192.168.156.19:6379: OK
- Connecting to node 192.168.156.20:6379: OK
- >>> Performing hash slots allocation on 6 nodes...
- Using 3 masters:
- 192.168.156.20:6379
- 192.168.156.19:6379
- 192.168.156.18:6379
- Adding replica 192.168.156.17:6379 to 192.168.156.20:6379
- Adding replica 192.168.156.16:6379 to 192.168.156.19:6379
- Adding replica 192.168.156.15:6379 to 192.168.156.18:6379
- S: 0cbe6fc270afa701156c5dc9c47dba522dd62737 192.168.156.15:6379
- replicates 423f334654c3dfdacd39927e0528a95c38aa933f
- S: 4eecf66148e88917b4ecf4b36e794a6e83e4fe85 192.168.156.16:6379
- replicates b69de3b87aeefac1d4711dd644a8dd5815c56bec
- S: 149c9e9a0e0febd581193df17d509ce4970e4163 192.168.156.17:6379
- replicates 6808fa38709cac3a6ec4b232fd1e45980cbb0c96
- M: 423f334654c3dfdacd39927e0528a95c38aa933f 192.168.156.18:6379
- slots:10923-16383 (5461 slots) master
- M: b69de3b87aeefac1d4711dd644a8dd5815c56bec 192.168.156.19:6379
- slots:5461-10922 (5462 slots) master
- M: 6808fa38709cac3a6ec4b232fd1e45980cbb0c96 192.168.156.20:6379
- slots:0-5460 (5461 slots) master
- 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.156.15:6379)
- M: 0cbe6fc270afa701156c5dc9c47dba522dd62737 192.168.156.15:6379
- slots: (0 slots) master
- replicates 423f334654c3dfdacd39927e0528a95c38aa933f
- M: 4eecf66148e88917b4ecf4b36e794a6e83e4fe85 192.168.156.16:6379
- slots: (0 slots) master
- replicates b69de3b87aeefac1d4711dd644a8dd5815c56bec
- M: 149c9e9a0e0febd581193df17d509ce4970e4163 192.168.156.17:6379
- slots: (0 slots) master
- replicates 6808fa38709cac3a6ec4b232fd1e45980cbb0c96
- M: 423f334654c3dfdacd39927e0528a95c38aa933f 192.168.156.18:6379
- slots:10923-16383 (5461 slots) master
- M: b69de3b87aeefac1d4711dd644a8dd5815c56bec 192.168.156.19:6379
- slots:5461-10922 (5462 slots) master
- M: 6808fa38709cac3a6ec4b232fd1e45980cbb0c96 192.168.156.20:6379
- slots:0-5460 (5461 slots) master
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
既然集群已经启动好了,我们使用redis客户端随意连接一台设备进行测试,比如我们连接192.168.156.16这台设备(这是个从节点,从属于192.168.156.19),如下所示(注意:一定要加上参数"-c"否则没办法重定向 到其它节点)。
[html] view plain copy
- [root@redis2 bin]# ./redis-cli -h 192.168.156.16 -p 6379 -c
- 192.168.156.16:6379>
连接上之后,我们先来保存key1,它的值是123,如下所示,可以看到,这个key1经过算法并对16384进行求余之后的数字是9189,这个槽号是在192.168.156.19上,因此key1被保存到了192.168.156.19上。
- 192.168.156.16:6379> set key1 123
- -> Redirected to slot [9189] located at 192.168.156.19:6379
- OK
- 192.168.156.19:6379>
我们再接着保存key2、key3、key4,如下所示,可以看到key2所对应的槽号是4998,显然是在192.168.156.20上,key3所对应的槽号也在0-5640,因此set key2的时候根本就没有重定向,还是停留在192.168.156.20上。key4所对应的槽号是13120,显然是在192.168.156.18上因此被保存到了192.168.156.18上。
[html] view plain copy
- 192.168.156.19:6379> set key2 111
- -> Redirected to slot [4998] located at 192.168.156.20:6379
- OK
- 192.168.156.20:6379> set key3 aaa
- OK
- 192.168.156.20:6379> set key4 bbb
- -> Redirected to slot [13120] located at 192.168.156.18:6379
- OK
- 192.168.156.18:6379>
我们可以使用命令cluster info查看集群的某些信息,如下所示,可以看到当前集群的状态是OK的,所有的槽号都已分配完毕,没有失败的节点。所有的节点数是6,当前提供服务的有3台
- 192.168.156.18:6379> cluster info
- cluster_state:ok
- cluster_slots_assigned:16384
- cluster_slots_ok:16384
- cluster_slots_pfail:0
- cluster_slots_fail:0
- cluster_known_nodes:6
- cluster_size:3
- cluster_current_epoch:6
- cluster_my_epoch:4
- cluster_stats_messages_sent:10748
- cluster_stats_messages_received:10748
- 192.168.156.18:6379>
还可以通过命令cluster nodes来查看节点信息。
- 192.168.156.18:6379> cluster nodes
- 423f334654c3dfdacd39927e0528a95c38aa933f 192.168.156.18:6379 myself,master - 0 0 4 connected 10923-16383
- 4eecf66148e88917b4ecf4b36e794a6e83e4fe85 192.168.156.16:6379 slave b69de3b87aeefac1d4711dd644a8dd5815c56bec 0 1492869406355 5 connected
- 0cbe6fc270afa701156c5dc9c47dba522dd62737 192.168.156.15:6379 slave 423f334654c3dfdacd39927e0528a95c38aa933f 0 1492869405340 4 connected
- 149c9e9a0e0febd581193df17d509ce4970e4163 192.168.156.17:6379 slave 6808fa38709cac3a6ec4b232fd1e45980cbb0c96 0 1492869402275 6 connected
- 6808fa38709cac3a6ec4b232fd1e45980cbb0c96 192.168.156.20:6379 master - 0 1492869408383 6 connected 0-5460
- b69de3b87aeefac1d4711dd644a8dd5815c56bec 192.168.156.19:6379 master - 0 1492869407370 5 connected 5461-10922
- 192.168.156.18:6379>
下面使用JedisCluster来测试集群,我们在taotao-content-service工程的测试类中再添加一个测试方法(与第三十课结合学习),如下所示。运行,发现是可以正常输出"hello jedis cluster"的。
- @Test
- public void testJedisCluster(){
- //创建构造参数Set类型,集合中每个元素是HostAndPort类型
nodes = new HashSet<>(); - //向集合中添加节点
- nodes.add(new HostAndPort("192.168.156.15", 6379));
- nodes.add(new HostAndPort("192.168.156.16", 6379));
- nodes.add(new HostAndPort("192.168.156.17", 6379));
- nodes.add(new HostAndPort("192.168.156.18", 6379));
- nodes.add(new HostAndPort("192.168.156.19", 6379));
- nodes.add(new HostAndPort("192.168.156.20", 6379));
- //创建JedisCluster对象
- jedisCluster = new JedisCluster(nodes);
- //直接使用jedisCluster,自带连接池,jedisCluster可以是单例的
- jedisCluster.set("jedis-cluster", "hello jedis cluster");
- result = jedisCluster.get("jedis-cluster");
- System.out.println(result);
- //系统关闭前关闭jedisCluster
- jedisCluster.close();
- }