使用连接工具RedisDesktopManager连接设置密码的redis。另外启动了Java项目建立连接后,工具端就掉线了,让后再工具再连接,此时Java项目调用redis后报错:“远程主机强迫关闭了一个现有的连接”。请问怎么解决?
1.检查连接数
查看redis最大连接数
config get maxclients
查看当前连接数
info clients
当连接达到最大连接数,导致后面的连接都连接失败,一般报错redis.clients.jedis.exceptions.JedisDataException: ERR max number of clients reached
2.检查超时时间(主要看这个)
redis.conf中
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Force network equipment in the middle to consider the connection to be
# alive.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
tcp-keepalive 300
程序端配置的超时时间(jedis默认2000毫秒)应该大于tcp-keepalive,否则tcp连接alive,spring已经超时,再创建连接,强制之前的连接关闭