请教一个 redis 长时间不使用再使用会报 command timeout 错误的问题

2020-07-28 15:33:29 +08:00
 nekoneko

下面是报错信息

Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out
	at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:70)
	at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41)
	at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)
	at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)
	at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:257)
	at org.springframework.data.redis.connection.lettuce.LettuceKeyCommands.convertLettuceAccessException(LettuceKeyCommands.java:650)
	at org.springframework.data.redis.connection.lettuce.LettuceKeyCommands.keys(LettuceKeyCommands.java:148)
	at org.springframework.data.redis.connection.DefaultedRedisConnection.keys(DefaultedRedisConnection.java:75)
	at org.springframework.data.redis.core.RedisTemplate.lambda$keys$10(RedisTemplate.java:840)
	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224)
	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
	at org.springframework.data.redis.core.RedisTemplate.keys(RedisTemplate.java:840)

早网上找了找没找到什么有用的解决方案,特来请教

11125 次点击
所在节点    Redis
14 条回复
ShutTheFu2kUP
2020-07-28 15:47:59 +08:00
mark 一下,也有同样问题
purensong
2020-07-28 15:49:19 +08:00
好像是 lettuce 的 bug,换驱动吧,jedis 不香吗
securityCoding
2020-07-28 16:53:10 +08:00
跟源码, 发现问题->解决问题 , 水平就是这样提高的
kidtest
2020-07-28 16:59:34 +08:00
一般这种都是 redis server 中配置了连接的超时时间,如果一个 client 连接在这段时间内没有操作就会被断开。所以首先需要确定 server 的超时时间,这个可以在配置文件中看到,然后一般的 redis 连接池都可以配置客户端的超时时间,将这个超时间配的短于 server 的超时时间即可。
或者笨方法就是自己手动加个判断,如果上次命令执行时间超过了超时时间,自己再创建一个新连接去执行命令
killergun
2020-07-28 17:03:34 +08:00
应该是连接断开了,客户端没用重新连接,而是用旧的连接
xuanbg
2020-07-28 17:30:52 +08:00
连接被 redis 断开了,客户端还在使用原先的连接就这样了。我遇到过的情况是 windows 平台的微软 redis 服务会断开客户端,Linux 上的 docker 装的 redis 就不会。
blueorange
2020-07-28 18:06:41 +08:00
顶一下 , 同样问题
nekoneko
2020-07-28 22:14:00 +08:00
@kidtest #4
@killergun #5
@xuanbg #6

redis 的配置
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
说明服务端不会去主动断开空闲连接,那么就是 lettuce 的锅了
在 github 上作者说 4.x 的版本会有这个问题,但是 springboot 用的是 5.x 的

又进行了几次复现,一段时间不用,再调用是会出现 command timeout,如果继续调用会一直出现这个异常,
又过了一段时间,调用,成功.....
kidtest
2020-07-29 10:54:38 +08:00
@nekoneko

那这就有点奇怪了。。应该是库哪里有问题。。
wakzz
2020-07-29 14:51:52 +08:00
开启客户端心跳包吧
nekoneko
2020-07-30 16:44:13 +08:00
@wakzz #10 lettuce 没有配置心跳的地方,我只能自己写个定时任务,每分钟 get 一次
ljw930824
2020-08-17 14:09:04 +08:00
lettuce 超时问题有俩种解决办法
1 、Redis 服务端 通过 tcp-keepalive 发送 ack 达到超活检测效果 官方建议设置 60s 时间
2 、升级 lettuce-core 至 5.3.0+
(具体看此 Fix infinite command timeout #1260 https://github.com/lettuce-io/lettuce-core/issues/1260
ljw930824
2020-08-17 14:19:53 +08:00
@nekoneko 抱歉,实际 这个更贴近 lettuce 中描述的 Bug 被 Block https://github.com/lettuce-io/lettuce-core/issues/1269
nekoneko
2020-08-18 09:33:19 +08:00
@ljw930824 #12 好吧,现在用的是 5.1.3 的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/693771

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX