使用ECS自己搭建Redis集群出现JedisCluster连接超时的问题-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

使用ECS自己搭建Redis集群出现JedisCluster连接超时的问题

cononyc 2017-09-16 13:05:42 6950

自己在阿里云的ECS(配置是1G ,1核)上搭建了一个redis集群(端口7000-7005),在服务器上测试

redis-cli -c -ip -p 7000

执行set,get方法的时候能够正确的重定向slot(定向到公网ip+端口,创建的时候是用公网ip创建的)
而且自己在电脑上的虚拟机上使用,和另外一台阿里云服务器

redis-cli -c -h 我的公网ip -p 7000

都能够正常访问,而且能够正确的重定向slot
但是我用Java的jedisCluster操作的时候就老是出现下面的问题

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

    at redis.clients.util.Pool.getResource(Pool.java:53)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
    at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnectionFromSlot(JedisSlotBasedConnectionHandler.java:66)
    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:116)
    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
    at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:31)
    at redis.clients.jedis.JedisCluster.set(JedisCluster.java:103)
    at JedisTest.test(JedisTest.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
    at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
    at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
    at redis.clients.util.Pool.getResource(Pool.java:49)
    ... 38 more
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 45 more

百度了好久,试了各种方法都不能解决问题,ESC的安全组配置也开放了7000-7005端口(0.0.0.0/0)和17000-17005(0.0.0.0/0)
redis.conf的配置文件

daemonize yes
appendonly yes
protected-mode no
cluster-enabled yes
cluster-node-timeout 15000
port 7000
cluster-config-file nodes-7000.conf

是不是因为阿里云服务器的问题导致的JedisCluster连接超时,想请各位大佬帮忙解决问题,停在这里好久了。
Java测试代码

        Set<HostAndPort> hostandports = new HashSet();
        hostandports.add(new HostAndPort("公网Ip",7000));
        hostandports.add(new HostAndPort("公网Ip",7001));
        hostandports.add(new HostAndPort("公网Ip",7002));
        JedisCluster jedisCluster = new JedisCluster(hostandports);
        System.out.println(jedisCluster.set("132455","aaaaaaaaaaaaa"));;
        System.out.println(jedisCluster.get("aaaaaa"));
弹性计算 NoSQL Java Redis
分享到
取消 提交回答
全部回答(15)
  • 游客sefuuxoj6wt3c
    2020-01-10 12:24:15

    ESC使用公网IP创建一直在>>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join....................

    0 0
  • 游客6mt6p3363segs
    2019-10-24 23:42:37

    有解决的方法吗?跪求。。。

    0 0
  • 游客nwxvs4m7ntaxi
    2019-07-17 21:35:52

    有人解决了么?公司另外一台服务器可以,但是我的就是不行,天呐

    0 0
  • crh5251
    2019-07-17 21:35:52

    把下面这三行
    hostandports.add(new HostAndPort("公网Ip",7000));
    hostandports.add(new HostAndPort("公网Ip",7001));
    hostandports.add(new HostAndPort("公网Ip",7002));
    改成
    hostandports.add(new HostAndPort("slave节点ip", slave节点端口));
    就行了。
    具体你看JedisClusterConnectionHandler类的initializeSlotsCache方法。

    1 0
  • zzjmay
    2019-07-17 21:35:52

    除了修改文件还有什么其他方法吗

    0 0
滑动查看更多
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

推荐文章
相似问题