redis服务器是windows版的。
项目使用spring集成redis,偶尔会出现read timed out的情况,刚开始以为是网络不稳定引起的。后面发现影响业务测试的准确性,才认真去看这个问题,实际是could not get a resource from the pool,连接池不够用。
网上看了很多博客都大同小异:
1. 说redis连接池配置不正确,最大连接数配置太小
尝试调整maxTotal为500、1000,依然出现异常
2. 说使用redisTemplate时未关闭连接,需要手动关闭
查看了源码,如果是非事务性调用不存在这个问题,每一次的调用都会自动关闭
在redis客户端层面的调整都无法解决这个异常,只能转向服务端了。
既然短连接不够用,考虑是否可以换成长连接,于是在配置文件中找到了一个参数
# 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) Take the connection alive from the point of view of network
# equipment in the middle.
#
# 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 60 seconds.
tcp-keepalive 0
tcp-keepalive默认是0,但上面推荐了一个合理值,设置为60。
重启redis,异常不见了,妥妥的。
具体原因还不清楚,表象是服务端提供的资源不能满足客户端的需求,是否有什么工具可以检测供求情况,用了Redis Desktop Manager 没看出什么苗头来,各位大佬懂的可以指教。
上面的方案仅供参考,还要根据实际情况而定。因为生产环境使用的某云的Redis服务,未发现该问题。