jedis 操作cluster以及故障演示

来源:互联网 发布:周立功单片机笔试题目 编辑:程序博客网 时间:2024/06/05 17:09

代码如下所示

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 successfully2500: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:80002500:S 18 Oct 22:17:54.235 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:17:54.235 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:17:55.254 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:17:55.255 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:17:55.255 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:17:56.262 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:17:56.262 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:17:56.263 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:17:57.309 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:17:57.309 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:17:57.310 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:17:58.330 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:17:58.331 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:17:58.331 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:17:59.340 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:17:59.341 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:17:59.341 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:18:00.385 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:18:00.385 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:18:00.385 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:18:01.396 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:18:01.396 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:18:01.396 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:18:02.407 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:18:02.408 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:18:02.408 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:18:03.417 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:18:03.417 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:18:03.417 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:18:04.433 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:18:04.433 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:18:04.433 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:18:05.444 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:18:05.444 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:18:05.444 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:18:06.454 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:18:06.454 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:18:06.454 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:18:07.463 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:18:07.463 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:18:07.463 # Error condition on socket for SYNC: Connection refused2500:S 18 Oct 22:18:08.473 * Connecting to MASTER 192.168.138.128:80002500:S 18 Oct 22:18:08.473 * MASTER <-> SLAVE sync started2500: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: fail2500: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:80002500:S 18 Oct 22:18:09.484 * MASTER <-> SLAVE sync started2500:S 18 Oct 22:18:09.484 # Error condition on socket for SYNC: Connection refused2500: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 failover2500:M 18 Oct 22:18:09.892 * Discarding previously cached master state.2500:M 18 Oct 22:18:09.892 # Cluster state changed: ok8003成为了新的主机

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

[root@localhost redis-data]# redis-cli -h 192.168.138.128 -p 8005192.168.138.128:8005> cluster nodes30c231a603cf446ab3e1394b12220999933f8c1c 192.168.138.128:8000 slave edc6cf38e050a646bacb9f3c8135484afd450815 0 1476800584476 6 connected77d38886a59f9cc79b01e0c755118fde18bc355f 192.168.138.128:8004 slave 41ff2c811fa237c423934f4fb60566530d65bcd1 0 1476800583468 4 connected41ff2c811fa237c423934f4fb60566530d65bcd1 192.168.138.128:8001 master - 0 1476800588526 4 connected 5461-109221fd4b3e6bf501700524df08ba6780968e2581976 192.168.138.128:8005 myself,slave 2e43cbd8495bba9bf0d0d5bba6668391fc0c053b 0 0 5 connectededc6cf38e050a646bacb9f3c8135484afd450815 192.168.138.128:8003 master - 0 1476800587518 6 connected 0-54602e43cbd8495bba9bf0d0d5bba6668391fc0c053b 192.168.138.128:8002 master - 0 1476800586504 3 connected 10923-16383 8000成为了从机
1 0
原创粉丝点击