问题出现:

org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: io.lettuce.core.RedisCommandTimeoutExcep                                                                                                  tion: Command timed out after 3 second(s)

项目刚启动时,redis可用,当过了一段时间后,redis就报这个错误

通过现象可以看出,我们的应用应该是拿一个已经断开的Redis连接,所以一直会超时,而且Lettuce也没有去自动重连

解决方案:
1.放弃使用lettuce,使用Jedis
2.加入定时心跳,保活

@Configuration
@EnableScheduling
@Sl4j
public class RedisScheduleTask {


    @Resource
    private StringRedisTemplate dupShowMasterRedisTemplate;

    // 1 minutes
    @Scheduled(fixedRate = 60000)
    private void configureTasks() {
        log.debug("ping redis");
        dupShowMasterRedisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(@NotNull RedisConnection connection) throws DataAccessException {
                return connection.ping();
            }
        });
    }

}

参考链接:
https://www.cnblogs.com/songjiyang/p/16809694.html
https://www.cnblogs.com/wingcode/p/14527107.html
https://juejin.cn/post/7199643079284179003

更多推荐