news 2026/5/12 23:41:57

RedisConnectionMonitor.java

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
RedisConnectionMonitor.java

RedisConnectionMonitor.java
用于监控 Redis 连接状态,特别是在服务器端定时关闭连接时进行诊断。

package further.common.conf.redis; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * Redis 连接健康检查和监控 * <p> * 用于监控 Redis 连接状态,特别是在服务器端定时关闭连接时进行诊断。 * </p> * * @author ZengWenFeng * @date 2025.12.20 * @mobile 13805029595 * @email 117791303@QQ.com */ @Component public class RedisConnectionMonitor { private static final Logger logger = LoggerFactory.getLogger(RedisConnectionMonitor.class); @Autowired private RedisTemplate<Object, Object> redisTemplate; /** * 每5分钟检查一次 Redis 连接健康状态 */ @Scheduled(fixedRate = 300000) // 5分钟 = 300000毫秒 public void checkConnectionHealth() { try { // 执行 PING 命令检查连接 String result = redisTemplate.getConnectionFactory().getConnection().ping(); if ("PONG".equals(result)) { logger.debug("Redis 连接健康检查通过: {}", result); } else { logger.warn("Redis 连接健康检查异常,返回: {}", result); } } catch (Exception e) { logger.error("Redis 连接健康检查失败,可能连接已断开: {}", e.getMessage()); // Lettuce 会自动重连,这里只记录日志 } } /** * 每天 4:55 执行一次连接检查(在 5:01 断开前) * 用于诊断是否在 5 点有定时任务 */ @Scheduled(cron = "0 55 4 * * ?") public void preDisconnectCheck() { logger.info("========== Redis 连接预检查(5点前) =========="); try { String result = redisTemplate.getConnectionFactory().getConnection().ping(); logger.info("Redis 连接正常: {}", result); } catch (Exception e) { logger.error("Redis 连接异常: {}", e.getMessage(), e); } logger.info("============================================="); } /** * 每天 5:05 执行一次连接检查(在 5:01 断开后) * 用于确认连接是否已自动重连 */ @Scheduled(cron = "0 5 5 * * ?") public void postDisconnectCheck() { logger.info("========== Redis 连接后检查(5点后) =========="); try { String result = redisTemplate.getConnectionFactory().getConnection().ping(); logger.info("Redis 连接已恢复: {}", result); } catch (Exception e) { logger.error("Redis 连接仍未恢复: {}", e.getMessage(), e); } logger.info("============================================="); } }

2025-12-20 05:01:12.121 [lettuce-nioEventLoop-16-1] INFO io.lettuce.core.protocol.CommandHandler - null Unexpected exception during request: java.io.IOException: 远程主机强迫关闭了一个现有的连接。 java.io.IOException: 远程主机强迫关闭了一个现有的连接。 at sun.nio.ch.SocketDispatcher.read0(Native Method) at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43) at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) at sun.nio.ch.IOUtil.read(IOUtil.java:192) at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:378) at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253) at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1134) at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:750) The method setMaxWait(int, TimeUnit) is undefined for the type GenericObjectPoolConfig<capture#4-of ?> The method setTimeBetweenEvictionRuns(int, TimeUnit) is undefined for the type GenericObjectPoolConfig<capture#7-of ?> The method commandTimeout(Duration) in the type LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder is not applicable for the arguments (int, TimeUnit) The final field RedisConfig.FastJson2JsonRedisSerializer<T>.objectMapper cannot be assigned

登飞来峰 北宋·王安石 飞来山上千寻塔,闻说鸡鸣见日升.不畏浮云遮望眼,只缘身在最高层。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/12 7:32:06

NVIDIA设置常见问题分类

驱动安装与更新问题游戏性能异常&#xff08;卡顿、帧率低&#xff09;多显示器配置冲突显卡温度过高或风扇异常光线追踪/DLSS功能失效驱动问题排查与解决使用DDU工具彻底卸载旧驱动&#xff08;安全模式操作流程&#xff09;手动下载官方驱动避免第三方软件干扰检查Windows系统…

作者头像 李华
网站建设 2026/5/11 0:22:56

转换成小写字母

一、问题描述&#xff1a;简单却实用的字符串转换需求​LeetCode 709 题要求我们实现一个函数&#xff0c;将输入字符串中的所有大写英文字母转换为小写&#xff0c;其他字符保持不变。这是一个日常开发中高频出现的场景 —— 比如用户输入规范化、数据格式统一等场景都可能用到…

作者头像 李华
网站建设 2026/5/8 11:24:39

切木棍最小成本方法

一、核心解题思路1. 问题转化与预处理- 排序切割点&#xff1a;切割点的顺序不影响最终切割成本&#xff0c;先对切割点升序排序&#xff0c;保证后续区间处理的有序性。- 补全切割点&#xff1a;在切割点数组首尾分别添加 0 &#xff08;木棍起点&#xff09;和 n &#xff08…

作者头像 李华
网站建设 2026/4/30 7:53:59

我发现Zstd压缩级太高内存涨 后来调level参数优化平衡

&#x1f493; 博客主页&#xff1a;瑕疵的CSDN主页 &#x1f4dd; Gitee主页&#xff1a;瑕疵的gitee主页 ⏩ 文章专栏&#xff1a;《热点资讯》 目录《我在Node.js坑里摔了三年&#xff0c;终于摸到点门道了》 一、为什么我要和Node.js杠上&#xff1f; 二、Node.js能干啥&a…

作者头像 李华