Redis command timed out nested exception is io.lettuce.core.RedisCommandTimeoutException

矫情吗;* 2024-03-26 22:22 126阅读 0赞

报错如下: ERROR org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 500 millisecond(s) at org…

报错如下:

  1. ERROR org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 500 millisecond(s)
  2. at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:70)
  3. at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41)
  4. at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)
  5. at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)
  6. at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:268)
  7. at org.springframework.data.redis.connection.lettuce.LettuceListCommands.convertLettuceAccessException(LettuceListCommands.java:490)
  8. at org.springframework.data.redis.connection.lettuce.LettuceListCommands.lLen(LettuceListCommands.java:159)
  9. at org.springframework.data.redis.connection.DefaultedRedisConnection.lLen(DefaultedRedisConnection.java:465)
  10. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  11. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  12. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  13. at java.lang.reflect.Method.invoke(Method.java:498)
  14. ****
  15. Caused by: io.lettuce.core.RedisCommandTimeoutException: Command timed out after 500 millisecond(s)
  16. at io.lettuce.core.ExceptionFactory.createTimeoutException(ExceptionFactory.java:51)
  17. at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114)
  18. at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:69)
  19. at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80)
  20. at com.sun.proxy.$Proxy196.llen(Unknown Source)
  21. at org.springframework.data.redis.connection.lettuce.LettuceListCommands.lLen(LettuceListCommands.java:157)
  22. ... 134 more

原因分析:这个情况是我在将SpringBoot从1.X升级到2.X的时候产生的问题,所以大致可以猜到是版本问题。

解决办法:

  • 设置超时时间

    redis:

    1. port: 6379
    2. jedis:
    3. pool:
    4. max-active: 50
    5. max-wait: 5000
    6. max-idle: 50
    7. min-idle: 0
    8. timeout: 500
    9. host: 47.100.21.110

这种方法无效,解决不了这个问题,我从0改到500还是报错。但是网上绝大多数都是这种方式,实际上我刚更改过来的时候有效过一阵,之后就又不行了。

  • 更改依赖


    org.springframework.boot
    spring-boot-starter-data-redis

在SpringBoot 1.X版本的jar包依赖中,点击进去依赖项如下:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.data</groupId>
  8. <artifactId>spring-data-redis</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>redis.clients</groupId>
  12. <artifactId>jedis</artifactId>
  13. </dependency>
  14. </dependencies>

升级到SpringBoot 2.X版本之后依赖如下:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter</artifactId>
  5. <version>2.1.2.RELEASE</version>
  6. <scope>compile</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.springframework.data</groupId>
  10. <artifactId>spring-data-redis</artifactId>
  11. <version>2.1.4.RELEASE</version>
  12. <scope>compile</scope>
  13. <exclusions>
  14. <exclusion>
  15. <artifactId>jcl-over-slf4j</artifactId>
  16. <groupId>org.slf4j</groupId>
  17. </exclusion>
  18. </exclusions>
  19. </dependency>
  20. <dependency>
  21. <groupId>io.lettuce</groupId>
  22. <artifactId>lettuce-core</artifactId>
  23. <version>5.1.3.RELEASE</version>
  24. <scope>compile</scope>
  25. </dependency>
  26. </dependencies>

可以看到变化是从jedis依赖变成了lettuce-core依赖,而上面的报错io.lettuce.core.RedisCommandTimeoutException也是lettuce-core的异常,所以我的解决方式是先将依赖换回jedis:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>io.lettuce</groupId>
  7. <artifactId>lettuce-core</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  11. <dependency>
  12. <groupId>redis.clients</groupId>
  13. <artifactId>jedis</artifactId>
  14. </dependency>

如上,配置文件不变,Redis连接成功,不再报错。

补充(深入一点点,个人理解,勿喷)

因为上文是我在版本升级的过程中遇到的,所以当时为了稳定还是继续使用的jedis连接池,但是实际上升级之后应该转向lettuce连接池,毕竟lettuce更好。
修改依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  5. <!-- 如果使用Lettuce作为连接池,需要引入commons-pool2包,否则会报错bean注入失败 -->
  6. <dependency>
  7. <groupId>org.apache.commons</groupId>
  8. <artifactId>commons-pool2</artifactId>
  9. </dependency>

修改配置文件

  1. spring:
  2. redis:
  3. port: 6379
  4. lettuce:
  5. pool:
  6. max-active: 500
  7. max-wait: 500
  8. max-idle: 500
  9. min-idle: 0
  10. timeout: 500
  11. host: 100.10.10.10
  12. password: password
分析
  1. @Configuration
  2. @ConditionalOnClass(RedisOperations.class)
  3. @EnableConfigurationProperties(RedisProperties.class)
  4. @Import({
  5. LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
  6. public class RedisAutoConfiguration {

首先是加载顺序。

在这里插入图片描述

同时我在@ConditionalOnClass(RedisClient.class)@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class })类里面打断点,发现仅RedisClient执行了,这让我明白为啥LettuceConnectionConfiguration配置生效,但是我没明白RedisClient为啥执行。。。。项目刚启动就进来这里,再上一层我实在没找到,希望哪位研究到这里可以给个反馈。

我想要抛出LettuceConnectionConfiguration配置时失效,有异常信息。

此文仅做记录,尚未分析原因,后续补充。

发表评论

表情:
评论列表 (有 0 条评论,126人围观)

还没有评论,来说两句吧...

相关阅读