参考的博客
问题起因
做毕设的时候,使用到Lettuce连接redis,一段时间后不操作,再去操作redis,会报连接超时错误,在其重连后又可使用。
原因是:Lettuce 自适应拓扑刷新(Adaptive updates)与定时拓扑刷新(Periodic updates) 是默认关闭的导致问题的出现
解决的方案
1、重写连接工厂实例,更改其LettuceClientConfiguration 为开启拓扑更新
2、SpringBoot2.3.x后,可使用配置文件中开启lettuce的拓扑刷新
- lettuce:
- pool:
- max-active: 20
- max-wait: -1ms
- max-idle: 10
- min-idle: 2
- cluster:
- refresh:
- adaptive: true
- #20秒自动刷新一次
- period: 20
3、更改连接redis的连接方式,使用jedis连接
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- <exclusions>
- <exclusion>
- <groupId>io.lettuce</groupId>
- <artifactId>lettuce-core</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
-
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- </dependency>
- spring:
- redis:
- jedis:
- pool:
- max-active: ${redis.config.maxTotal:1024}
- max-idle: ${redis.config.maxIdle:50}
- min-idle: ${redis.config.minIdle:1}
- max-wait: ${redis.config.maxWaitMillis:5000}
- #lettuce:
- #pool:
- #max-active: ${redis.config.maxTotal:1024}
- #max-idle: ${redis.config.maxIdle:50}
- #min-idle: ${redis.config.minIdle:1}
- #max-wait: ${redis.config.maxWaitMillis:5000}
到此这篇关于SpringBoot整合redis使用Lettuce客户端超时问题的文章就介绍到这了,更多相关SpringBoot整合redis内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!