踩坑-springboot 集成redis(Lettuce 客户端超时问题)

原先配置是使用默认Lettuce。后来在生产环境发现会经常的超时,导致缓存不可用,而且一超时就不恢复。

后来查了些资料,网上都有这个问题,无解,都建议使用jedis。

 

先看下原来的配置,maven配置(项目其他配置已省略)

 
  1. <parent>

  2.       <groupId>org.springframework.boot</groupId>

  3.       <artifactId>spring-boot-starter-parent</artifactId>

  4.       <version>2.0.0.RELEASE</version>

  5. </parent>

  6.  
  7. <dependency>

  8. <groupId>org.springframework.boot</groupId>

  9. <artifactId>spring-boot-starter-redis</artifactId>

  10. <version>2.0.0.RELEASE</version>

  11. </dependency>

  12. <dependency>

  13. <groupId>org.springframework.boot</groupId>

  14. <artifactId>spring-boot-starter-data-redis</artifactId>

  15. <version>2.0.0.RELEASE</version>

  16. <exclusions>

  17. <exclusion>

  18. <groupId>redis.clients</groupId>

  19. <artifactId>jedis</artifactId>

  20. </exclusion>

  21. </exclusions>

  22. </dependency>

application.yml配置:

 
  1. spring:

  2. redis:

  3. database: 15 # Redis数据库索引(默认为0)

  4. host: 10.10.10.43 # Redis服务器地址

  5. port: 6379 # Redis服务器连接端口

  6. password: # Redis服务器连接密码(默认为空)

  7. timeout: 5000 # 连接超时时间(毫秒)

  8. pool:

  9. max-active: 8 # 连接池最大连接数(使用负值表示没有限制)

  10. max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)

  11. max-idle: 8 # 连接池中的最大空闲连接

  12. min-idle: 0 # 连接池中的最小空闲连接

会一直报错org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 500 millisecond(s)。

后来改配置,maven如下:

 
  1. <parent>

  2.       <groupId>org.springframework.boot</groupId>

  3.       <artifactId>spring-boot-starter-parent</artifactId>

  4.       <version>2.0.0.RELEASE</version>

  5. </parent>

  6.  
  7. <dependency>

  8. <groupId>org.springframework.boot</groupId>

  9. <artifactId>spring-boot-starter-redis</artifactId>

  10. <version>2.0.0.RELEASE</version>

  11. </dependency>

  12. <dependency>

  13. <groupId>org.springframework.boot</groupId>

  14. <artifactId>spring-boot-starter-data-redis</artifactId>

  15. <version>2.0.0.RELEASE</version>

  16. <exclusions>

  17. <!-- <exclusion>

  18. <groupId>redis.clients</groupId>

  19. <artifactId>jedis</artifactId>

  20. </exclusion> -->

  21. <exclusion>

  22. <groupId>io.lettuce</groupId>

  23. <artifactId>lettuce-core</artifactId>

  24. </exclusion>

  25. </exclusions>

  26. </dependency>

  27. <dependency>

  28. <groupId>redis.clients</groupId>

  29. <artifactId>jedis</artifactId>

  30. <version>2.9.0</version>

  31. </dependency>

application.yml配置:

 
  1. spring:

  2. redis:

  3. database: 15 # Redis数据库索引(默认为0)

  4. host: 10.10.10.43 # Redis服务器地址

  5. port: 6379 # Redis服务器连接端口

  6. password: # Redis服务器连接密码(默认为空)

  7. timeout: 5000 # 连接超时时间(毫秒)

  8. jedis:

  9. pool:

  10. max-active: 300

  11. max-idle: 100

  12. min-idle: 0

  13. max-wait: -1ms

 

总的来说是放弃了Lettuce ,使用jedis,问题能够得到解决。