今天在用jedis连接远程的redis时,抛出连接超时异常:connect timed out。具体信息如下:
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out at redis.clients.jedis.Connection.connect(Connection.java:142) at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:75) at redis.clients.jedis.Connection.sendCommand(Connection.java:83) at redis.clients.jedis.BinaryClient.set(BinaryClient.java:92) at redis.clients.jedis.Client.set(Client.java:23) at redis.clients.jedis.Jedis.set(Jedis.java:53) at com.ado.test.redis.RedisTest.main(RedisTest.java:10) Caused by: java.net.SocketTimeoutException: connect timed out at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at redis.clients.jedis.Connection.connect(Connection.java:137) ... 6 more
经过确认,排除了几种可能:
1、服务器地址配置错误。(经确认,配置无误。)
2、redis连接数量过多。(经确认,没有任何连接在redis服务器上。)
查看jedis连接源码,也没发现有什么异常,于是怀疑是redis主机的问题。使用telnet命令进行测试,发现也是连接不上。于是怀疑是redis服务器防火墙没关闭。
查看服务器防火墙状态:
services iptables status
关闭防火墙:
services iptables stop
然后再进行连接,这次就很快连接成功了。问题解决。
原文地址:http://blog.csdn.net/ado1986/article/details/40117515