jedis 操作cluster以及故障演示

代码如下所示

package com;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.log4j.WriterAppender;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class RedisCluster {
    private Logger logger=LoggerFactory.getLogger(RedisCluster.class);
    private static JedisCluster jCluster;
    private static String host="192.168.138.128";
    @BeforeClass
    public static void setUp(){
        //节点集合
        Set<HostAndPort>nodeList=new HashSet<HostAndPort>();
        for (int i = 0; i < 6; i++) {
            nodeList.add(new HostAndPort(host, 8000+i));
        }
        GenericObjectPoolConfig gConfig=new GenericObjectPoolConfig();
        int timeout=2000;
        jCluster=new JedisCluster(nodeList,timeout,gConfig);
    }
    @Test
    public void testWrite(){
        for (int i = 0; i < 100; i++) {
            if (i%10==0) {
                System.out.println(i);
                logger.info("execute {}"+i);
            }
            String key="user"+i;
            String value=String.valueOf(i);
            jCluster.set(key, value);
        }
    }
    @Test
    public void testRead(){
        while(true){
            try {
                TimeUnit.MILLISECONDS.sleep(100);
                String key="user"+(new Random().nextInt(100)+1);
                System.out.println("key:"+key+"value:"+jCluster.get(key));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                logger.error(e.getMessage());
                e.printStackTrace();
            }
        }
    }
}

结果查询所示

[root@localhost ~]# redis-cli -c -h 192.168.138.128 -p 8000 dbsize
(integer) 31
[root@localhost ~]# redis-cli -c -h 192.168.138.128 -p 8001 dbsize
(integer) 33
[root@localhost ~]# redis-cli -c -h 192.168.138.128 -p 8002 dbsize
(integer) 38
[root@localhost ~]# redis-cli -c -h 192.168.138.128 -p 8003 dbsize
(integer) 31
[root@localhost ~]# redis-cli -c -h 192.168.138.128 -p 8004 dbsize
(integer) 33
[root@localhost ~]# redis-cli -c -h 192.168.138.128 -p 8005 dbsize
(integer) 38
[root@localhost ~]# 

kill 8000这个进程查看8003.log以及与8000断开连接

2500:S 18 Oct 21:55:10.298 * Background AOF rewrite finished successfully
2500:S 18 Oct 22:17:53.899 # Connection with master lost.
2500:S 18 Oct 22:17:53.900 * Caching the disconnected master state.
2500:S 18 Oct 22:17:54.234 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:17:54.235 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:17:54.235 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:17:55.254 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:17:55.255 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:17:55.255 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:17:56.262 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:17:56.262 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:17:56.263 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:17:57.309 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:17:57.309 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:17:57.310 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:17:58.330 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:17:58.331 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:17:58.331 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:17:59.340 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:17:59.341 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:17:59.341 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:18:00.385 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:18:00.385 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:18:00.385 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:18:01.396 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:18:01.396 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:18:01.396 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:18:02.407 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:18:02.408 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:18:02.408 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:18:03.417 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:18:03.417 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:18:03.417 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:18:04.433 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:18:04.433 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:18:04.433 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:18:05.444 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:18:05.444 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:18:05.444 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:18:06.454 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:18:06.454 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:18:06.454 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:18:07.463 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:18:07.463 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:18:07.463 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:18:08.473 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:18:08.473 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:18:08.473 # Error condition on socket for SYNC: Connection refused

接下来继续查看日志

2500:S 18 Oct 22:18:09.184 # Cluster state changed: fail
2500:S 18 Oct 22:18:09.282 # Start of election delayed for 581 milliseconds (rank #0, offset 3868).
2500:S 18 Oct 22:18:09.484 * Connecting to MASTER 192.168.138.128:8000
2500:S 18 Oct 22:18:09.484 * MASTER <-> SLAVE sync started
2500:S 18 Oct 22:18:09.484 # Error condition on socket for SYNC: Connection refused
2500:S 18 Oct 22:18:09.887 # Starting a failover election for epoch 6.
2500:S 18 Oct 22:18:09.892 # Failover election won: I'm the new master.
2500:S 18 Oct 22:18:09.892 # configEpoch set to 6 after successful failover
2500:M 18 Oct 22:18:09.892 * Discarding previously cached master state.
2500:M 18 Oct 22:18:09.892 # Cluster state changed: ok

8003成为了新的主机

启动8000并且查看8000节点信息

[root@localhost redis-data]# redis-cli -h 192.168.138.128 -p 8005
192.168.138.128:8005> cluster nodes
30c231a603cf446ab3e1394b12220999933f8c1c 192.168.138.128:8000 slave edc6cf38e050a646bacb9f3c8135484afd450815 0 1476800584476 6 connected
77d38886a59f9cc79b01e0c755118fde18bc355f 192.168.138.128:8004 slave 41ff2c811fa237c423934f4fb60566530d65bcd1 0 1476800583468 4 connected
41ff2c811fa237c423934f4fb60566530d65bcd1 192.168.138.128:8001 master - 0 1476800588526 4 connected 5461-10922
1fd4b3e6bf501700524df08ba6780968e2581976 192.168.138.128:8005 myself,slave 2e43cbd8495bba9bf0d0d5bba6668391fc0c053b 0 0 5 connected
edc6cf38e050a646bacb9f3c8135484afd450815 192.168.138.128:8003 master - 0 1476800587518 6 connected 0-5460
2e43cbd8495bba9bf0d0d5bba6668391fc0c053b 192.168.138.128:8002 master - 0 1476800586504 3 connected 10923-16383

 8000成为了从机
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值