V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nekoneko
V2EX  ›  Redis

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

  •  1
     
  •   nekoneko · 2020-07-28 15:33:29 +08:00 · 11074 次点击
    这是一个创建于 1339 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • 环境是 springboot2.0,连接池是 springboot 带的 lettuce
    • 配置使用的是默认配置
    • 现象就是 一段时间不是用 redis, 再去使用的时候就会报 command timeout exception

    下面是报错信息

    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)
    
    

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

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

    那这就有点奇怪了。。应该是库哪里有问题。。
    wakzz
        10
    wakzz  
       2020-07-29 14:51:52 +08:00
    开启客户端心跳包吧
    nekoneko
        11
    nekoneko  
    OP
       2020-07-30 16:44:13 +08:00
    @wakzz #10 lettuce 没有配置心跳的地方,我只能自己写个定时任务,每分钟 get 一次
    ljw930824
        12
    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
        13
    ljw930824  
       2020-08-17 14:19:53 +08:00
    @nekoneko 抱歉,实际 这个更贴近 lettuce 中描述的 Bug 被 Block https://github.com/lettuce-io/lettuce-core/issues/1269
    nekoneko
        14
    nekoneko  
    OP
       2020-08-18 09:33:19 +08:00
    @ljw930824 #12 好吧,现在用的是 5.1.3 的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3188 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 14:27 · PVG 22:27 · LAX 07:27 · JFK 10:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.