redis异常:

redis.clients.jedis.exceptions.JedisConnectionException: It seems like server has closed the connection.at redis.clients.util.RedisInputStream.readLine(RedisInputStream.java:90) ~\[jedis-2.1.0.jar:na\]at redis.clients.jedis.Protocol.processInteger(Protocol.java:110) ~\[jedis-2.1.0.jar:na\]at redis.clients.jedis.Protocol.process(Protocol.java:70) ~\[jedis-2.1.0.jar:na\]at redis.clients.jedis.Protocol.read(Protocol.java:131) ~\[jedis-2.1.0.jar:na\]at redis.clients.jedis.Connection.getIntegerReply(Connection.java:188) ~\[jedis-2.1.0.jar:na\]at redis.clients.jedis.Jedis.sismember(Jedis.java:1266) ~\[jedis-2.1.0.jar:na\]

看提示,应该是服务端主动关闭了连接。查看线上redis服务器的配置的timout选项:

# Close the connection after a client is idle for N seconds (0 to disable)timeout 30

该配置指的是客户端连接空闲超过多少秒后,服务端主动关闭连接,默认值0表示服务端永远不主动关闭。客户端使用了一个连接池管理访问redis的所有连接,这些连接是长连接,当业务量较小时,客户端部分连接使用率较低,当两次使用之间的间隔超过30秒时,redis服务端就主动关闭了这个连接,而等客户端下次再使用这个连接对象时,发现服务端已经关闭了连接,进而报错。

连接池setTimeBetweenEvictionRunsMillis(60000);// 失效检查线程运行时间间隔,如果小于等于0,不会启动检查线程 每隔1分钟检查一下连接池建立的连接。

解决方案:把timeout值大于60s即可。改为300,问题解决。

本文固定链接: [http://www.chepoo.com/jedis-connection-exception-it-seems-like-server-has-closed-the-connection.html IT技术精华网](http://www.chepoo.com/jedis-connection-exception-it-seems-like-server-has-closed-the-connection.html “JedisConnectionException: It seems like server has closed the connection”)