今天在用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、server地址配置错误。(经确认。配置无误。)
2、redis连接数量过多。(经确认,没有不论什么连接在redisserver上。)
查看jedis连接源代码。也没发现有什么异常。于是怀疑是redis主机的问题。使用telnet命令进行測试,发现也是连接不上。于是怀疑是redisserver防火墙没关闭。
查看server防火墙状态:
service iptables status
关闭防火墙:
service iptables stop
然后再进行连接,这次就非常快连接成功了。问题解决。