Solve the problem of redisCluster connection failure

After using redis3.2.8 to establish a cluster, it is found that the connection failure is often reported when connecting to redis. However, when a node is connected alone, no error will be reported. After analysis, the reason is finally found.
The reason is this: when I set up the cluster, I used the command: ./redis-trib.rb create --replicas 1 192.168.6.24:7000 192.168.6.24:7001 to
bind 127.0.0.1 192.168.6.24 in the configuration file redis.conf
192.168.6.24:7002 192.168.6.24:7003 192.168.6.24:7004 192.168.6.24:7005
Set up the cluster. After the cluster is established, connect to redis and find that there is a problem, and then change the rdis.conf to: bind 192.168.6.24

so the client Sometimes 127.0.0.1: **** is connected, so an error is reported.

How to solve this problem:
1. Delete the three files nodes-700*.conf , appendonly.aof , dump.rdb
2. Start the service
[root@yunboce redis]# ./redis-server 7000/redis.conf
[root @yunboce redis]# ./redis-server 7001/redis.conf
[root@yunboce redis]# ./redis-server 7002/redis.conf
[root@yunboce redis]# ./redis-server 7003/redis.conf
[root@yunboce redis]# ./redis-server 7004/redis.conf
[root@yunboce redis]# ./redis-server 7005/redis.conf

3.执行建群命令:
./redis-trib.rb  create  --replicas  1  192.168.6.24:7000 192.168.6.24:7001  192.168.6.24:7002 192.168.6.24:7003  192.168.6.24:7004  192.168.6.24:7005


4.测试:


import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

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

public class JedisClusterTest {


JedisCluster jedisCluster = null;

private String nameKey="test1";

    /**
     * Because it is a test, no singleton is written here
     */
   
    public void before(){
    //Note: the timeout time here should not be too short, he will have a timeout test mechanism. And other RPC frameworks like httpclient, dubbo should also pay attention to this
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
// config.setMaxIdle(200);
// config.setMaxTotal(300);
// config.setTestOnReturn(true);
        Set <HostAndPort> nodes = new HashSet<HostAndPort>();   
        nodes.add(new HostAndPort("192.168.6.24",7000));
        nodes.add(new HostAndPort("192.168.6.24",7001)); 
        nodes.add (new HostAndPort("192.168.6.24",7002));
        nodes.add(new HostAndPort("192.168.6.24",7003));
       nodes.add(new HostAndPort("192.168.6.24",7005));
       nodes.add(new HostAndPort("192.168.6.24",7004));
       
       
        jedisCluster = new JedisCluster(nodes, 10000, 1000, 1,
        config);

        System.out.println(jedisCluster+"===");
        for(int i=0;i<1;i++){
        System.out.println(jedisCluster.set("test1"+i, "张三"+i));
        System.out.println(jedisCluster.get("test1"+i));
        }
       

    }

      
    public TreeSet<String> keys(String pattern){ 
      //  logger.debug("Start getting keys..."); 
        TreeSet<String>keys = new TreeSet<String>(); 
        Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes(); 
        for(String k : clusterNodes.keySet()){
       
            System.out.println("Getting keys from: {}"+k); 
            JedisPool jp = clusterNodes.get(k); 
            Jedis connection = jp.getResource(); 
            try { 
            Set<String> str = connection.keys(pattern);
            System.out.println(str.toString());
                keys.addAll(str); 
            } catch(Exception e){ 
               // logger.error("Getting keys error: {}", e); 
            } finally{ 
               System.out.println("Connection closed."); 
               connection.close();//Be sure to close this link when you are done! !
            } 
        } 
          
       
       
        return keys; 
    } 
   
   
    public static void main(String[] args) {

    JedisClusterTest test = new JedisClusterTest();
    test.before();
    TreeSet<String> t = test.keys("*");
}


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326270334&siteId=291194637