首页 文章

JedisCluster无法使用密码连接到redis群集

提问于
浏览
0

我的redis集群正在侦听所有接口,并允许任何主机 . telnet到redis是可以的:

-NOAUTH Authentication required.
auth xxxx
+OK
get a
$1
a

iptables是空的:

[centos ]# iptables -L
        Chain INPUT (policy ACCEPT)
        target     prot opt source               destination         

        Chain FORWARD (policy ACCEPT)
        target     prot opt source               destination         

        Chain OUTPUT (policy ACCEPT)
        target     prot opt source               destination

Redis群集已创建:

127.0.0.1:5067> 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:1
cluster_stats_messages_ping_sent:242525
cluster_stats_messages_pong_sent:243749
cluster_stats_messages_sent:486274
cluster_stats_messages_ping_received:243744
cluster_stats_messages_pong_received:242525
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:486274
127.0.0.1:5067> cluster nodes
4661d0d49a0fbdcdcef62a5fb6f9efc94c27360a 127.0.0.1:5567@15567 slave a7042494018a4baa941ffbf6e058e851af3d9c9a 0 1518311378509 6 connected
a7042494018a4baa941ffbf6e058e851af3d9c9a 127.0.0.1:5267@15267 master - 0 1518311378000 3 connected 10923-16383
f9e7c80f5272966fdeeb8b53d5388cd1e9efbbcd 127.0.0.1:5067@15067 myself,master - 0 1518311376000 1 connected 0-5460
f331f32c285550673ec5895a3de29ab4e1aec800 127.0.0.1:5367@15367 slave f9e7c80f5272966fdeeb8b53d5388cd1e9efbbcd 0 1518311378910 4 connected
61ba6718c859bc35d14ad8a4f6ae8fe1a0a45673 127.0.0.1:5167@15167 master - 0 1518311378408 2 connected 5461-10922
fc46d8ff080c253711a20a559ac6d95c6ec6325f 127.0.0.1:5467@15467 slave 61ba6718c859bc35d14ad8a4f6ae8fe1a0a45673 0 1518311378000 5 connected

但是当我使用JedisCluster时:

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Caused by: java.net.ConnectException: Connection refused: connect

我来告诉你代码:

//it's successful to connect to all master nodes and get the keys' value
    @Test
    public void testOneByOne() {
    String[] keys = new String[] {
            "b",
            "c",
            "a"

    };
    for (int i = 0; i < 3; i++) {

        Jedis jedis = new Jedis("{myIp}", 5067 + i * 100);
        jedis.auth("{myPassword}");
        String a = jedis.get(keys[i]);
        System.out.println(a); //ok
    }
}

//all the masters and slaves can be connected, so network is ok.
@Test
public void testConnectFromSourceCode() throws Exception {
    for (int i = 0; i < 6; i++) {
        int port = 5067 + i * 100;
        Socket socket = new Socket();
        socket.setReuseAddress(true);
        socket.setKeepAlive(true);
        socket.setTcpNoDelay(true);
        socket.setSoLinger(true, 0);
        socket.connect(new InetSocketAddress("{myIp}", port), 2000);
        socket.setSoTimeout(2000);
        System.out.println("port=" + port + "," + socket.isConnected());//isConnected=true
    }


}

//# error:
// redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
//Caused by: java.net.ConnectException: Connection refused: connect
@Test
public void testJedisCluster() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(10);
    jedisPoolConfig.setMaxIdle(5);
    jedisPoolConfig.setMinIdle(5);
    jedisPoolConfig.setMaxWaitMillis(300000);
    jedisPoolConfig.setTestOnBorrow(true);
    jedisPoolConfig.setTestOnReturn(true);
    jedisPoolConfig.setTestWhileIdle(true);

    HostAndPort hostAndPort = new HostAndPort("{myIp}", 5067);
    JedisCluster jc=new JedisCluster(hostAndPort, 2000,2000,5, "{myPassword}", jedisPoolConfig);
    String a = jc.get("a");
    System.out.println(a);
}

六个机构(3个主机和3个从机)位于一个主机中,监听端口来自5067/5167/5267/5367/5467/5567 redis集群安装在TenCent Cloud Virtual Machine

我在自己的电脑上运行我的代码 . 该网络是中国电信全家宽带 .

我正在使用:

redis 4.0.8

jedis 2.9.0

谢谢!

1 回答

  • 0

    cluster-announce-ip解决了我的问题

相关问题