开发者社区> 问答> 正文

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

自己在阿里云的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"));

展开
收起
cononyc 2017-09-16 13:05:42 12283 0
15 条回答
写回答
取消 提交回答
  • ESC使用公网IP创建一直在>>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join....................

    2020-01-10 12:24:15
    赞同 展开评论 打赏
  • 有解决的方法吗?跪求。。。

    2019-10-24 23:42:37
    赞同 展开评论 打赏
  • 有人解决了么?公司另外一台服务器可以,但是我的就是不行,天呐

    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方法。

    2019-07-17 21:35:52
    赞同 1 展开评论 打赏
  • 除了修改文件还有什么其他方法吗

    2019-07-17 21:35:52
    赞同 展开评论 打赏
  • 我解决了 老铁 你解决没,没解决 可以发邮件 3130290751@qq.com 给我 ,我把我源码发你

    2019-07-17 21:35:52
    赞同 展开评论 打赏
  • 遇到同样的问题 ,无法解决

    2019-07-17 21:35:52
    赞同 展开评论 打赏
  • 2019-07-17 21:35:51
    赞同 展开评论 打赏
  • 首先是确认jedis的配置有没有问题,这个网上文章很多,可以确认下
    然后使用telnet测试下端口是否可达,排除网络问题

    2019-07-17 21:35:51
    赞同 展开评论 打赏
  • 我也遇见了同样的问题,好几台服务器,有的可以有的不可以,很奇怪


    问题已经解决,直接修改cluster-nodes对应文件中的对应节点的IP,因为本楼主的nodes文件在对应的节点下是内网IP,直接修改成外网IP,并且同时要设置密码,就可以了

    2019-07-17 21:35:51
    赞同 展开评论 打赏
  • 你好,我也遇到了同样的问题,百度了好多都没能解决。。请问这个解决了没有

    2019-07-17 21:35:51
    赞同 展开评论 打赏
  • 巧了2333集群启动的时候用内网ip可以的创建成功.但是外网访问访问是可以通..但是要set他节点好像不能内部跳转。。。我用公网ip创建集群他就一直给我在哪里waiting for the cluster to join....... 老哥解决了嘛

    2019-07-17 21:35:51
    赞同 展开评论 打赏
  • 修改 Redis.conf 配置 cluster-config-file的指定文件,把私有ip更改为公网ip即可

    2019-07-17 21:35:51
    赞同 1 展开评论 打赏
  • 这个有没有解决啊,我也碰到了。。。。

    2019-07-17 21:35:51
    赞同 展开评论 打赏
  • 备注下redis版本是3.2.9,jedis用的是2.9,然后ruby是2.3,gem中的redis是3.0.0(换成4.0一样的)

    2019-07-17 21:35:51
    赞同 展开评论 打赏
滑动查看更多
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS快储存加密技术 立即下载