* 使用spring-boot-starter-data-redis时长时间不使用会出现连接超时问题
* 启动类添加@EnableScheduling注解
* 添加commons-pool2依赖 <dependency> <groupId>org.apache.commons</groupId> <
artifactId>commons-pool2</artifactId> </dependency>
* 开启可验证配置@Slf4j @Component public class LettuceConnectionValidConfig
implements InitializingBean { private final RedisConnectionFactory
redisConnectionFactory; public LettuceConnectionValidConfig(
RedisConnectionFactory redisConnectionFactory) { this.redisConnectionFactory =
redisConnectionFactory; } @Override public void afterPropertiesSet() throws
Exception { if(redisConnectionFactory instanceof LettuceConnectionFactory){
LettuceConnectionFactory c= (LettuceConnectionFactory)redisConnectionFactory; c.
setValidateConnection(true); } } }
* 配置定时任务@Slf4j @Component public class LettuceConnectionValidTask { private
final RedisConnectionFactory redisConnectionFactory; public
LettuceConnectionValidTask(RedisConnectionFactory redisConnectionFactory) { this
.redisConnectionFactory = redisConnectionFactory; } @Scheduled(cron="0/2 * * *
* ?") public void task() { if(redisConnectionFactory instanceof
LettuceConnectionFactory){ LettuceConnectionFactory c=(LettuceConnectionFactory)
redisConnectionFactory; c.validateConnection(); } } }

技术
今日推荐
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信