JedisCluster在2.7.x中工作,但在Jedis 2.9.x中使用JedisConnectionException并且Connection拒绝

时间:2018-03-21 19:01:42

标签: java redis jedis

我在Jedis 2.7.x中使用了几个月的代码。但我发现我遇到与https://github.com/xetorthio/jedis/issues/1625相同的问题(redis服务器重启时的JedisPool.getResource块)。所以我从2.7.x切换到2.9.x.我使用相同的代码来创建JedisCluster。但它无法连接并从Jedis获取数据。

我创建了一个单独的JedisCluster实例,每次我只使用JedisCluster.get()来获取数据。我不关闭群集的连接。

更多信息:我的redis是3.2.8。我使用单个主机作为JedisCluster并且没有奴隶。我想使用JedisCluster模式而不是Jedis模式的原因是我有另一个系统需要用多主机调用JedisCluster。我想先测试当前单主机集群中的代码,然后将代码部署到该系统

cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:1
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0

相同的代码在2.7.x中工作,但不在2.9.x。

@Singleton
JedisCluster jedisCluster(){
        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
        jedisClusterNodes.add(new HostAndPort("10.xx.xx.xx", 6379));
        GenericObjectPoolConfig jedisPoolConfig = new GenericObjectPoolConfig();
        jedisPoolConfig.setMaxTotal(16);
        jedisPoolConfig.setMaxIdle(16);
        return new JedisCluster(jedisClusterNodes, 2000, 1000, 5, jedisPoolConfig);
}
//When using cluster
jedisCluster.set("foo", "bar");
String value = jedisCluster.get("foo");

以下代码适用于2.9.x

Jedis jedis = new Jedis("xx.xx.xxx.xx",6379);
System.out.println(jedis.get("foo"));

我ssh到我的Redis主机并运行cli

127.0.0.1:6379> get foo
"bar

我得到的例外

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:53) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnectionFromSlot(JedisSlotBasedConnectionHandler.java:66) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:116) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:31) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisCluster.get(JedisCluster.java:124) ~[jedis-2.9.x.jar:?]
.........
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused)
at redis.clients.jedis.Connection.connect(Connection.java:207) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106) ~[jedis-2.9.x.jar:?]
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:836) ~[commons-pool2-2.2.jar:2.2]
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:434) ~[commons-pool2-2.2.jar:2.2]
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361) ~[commons-pool2-2.2.jar:2.2]
at redis.clients.util.Pool.getResource(Pool.java:49) ~[jedis-2.9.x.jar:?]
... 45 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:1.8.0_144]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_144]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_144]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_144]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_144]
at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_144]
at redis.clients.jedis.Connection.connect(Connection.java:184) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767) ~[jedis-2.9.x.jar:?]
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106) ~[jedis-2.9.x.jar:?]
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:836) ~[commons-pool2-2.2.jar:2.2]
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:434) ~[commons-pool2-2.2.jar:2.2]
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361) ~[commons-pool2-2.2.jar:2.2]
at redis.clients.util.Pool.getResource(Pool.java:49) ~[jedis-2.9.x.jar:?]
... 45 more

登录redis方

3088:M 21 Mar 21:42:24.187 - Accepted xx.xx.xxx.xxx:36064
3088:M 21 Mar 21:42:24.197 - Accepted xx.xx.xxx.xxx:36065
3088:M 21 Mar 21:42:24.212 - Reading from client: Connection reset by peer
3088:M 21 Mar 21:42:28.481 - DB 0: 12407076 keys (0 volatile) in 16777216 slots HT.
3088:M 21 Mar 21:42:28.482 - 1 clients connected (0 slaves), 1852164712 bytes in use

https://github.com/xetorthio/jedis/issues/1792

1 个答案:

答案 0 :(得分:1)

Marcos Nils在jedis github中回答了我的问题 - https://github.com/xetorthio/jedis/issues/1792我指的是https://github.com/xetorthio/jedis/pull/1211

以前我没有在我的单个主机中创建集群,我可以直接在2.7.x中将它用作集群。我运行了以下命令解决了我的问题。

for i in {0..16383}; do bin/redis-cli -h <ipOfHost1> CLUSTER ADDSLOTS $i; done
cluster meet <ip> 6379