当前位置: 首页 > news >正文

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

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

http://www.cnnetsun.cn/news/168068.html

相关文章:

  • AI工具实战测评技术
  • 创意AI应用开发大赛技术
  • 全球股市估值与海洋微生物能源技术的关系
  • 基于python的同城宠物照看数据可视化分析系统的设计与实现_34cl0po8--论文
  • 【路径规划】基于RRT快速探索随机树的图像地图路径规划实现3附matlab代码
  • Quartz 工作模式,是“堵塞排队”还是“并发狂奔”?
  • 【FFNN负荷预测】基于人工神经网络的空压机负荷预测(Matlab代码实现)
  • 【C2000系列DSP的反向灌电流】为什么热插拔的时候I2C总线电平会被拉低?
  • Gemini Inc靶场练习(包含suid提权,文件包含漏洞,ssh免密登录)
  • 软件解耦与扩展:插件式开发方式(基于 C++ 与 C# 的实现)
  • 免费降AI率的工具红黑榜:认准这2个免费降AI率工具,亲测有效!
  • 霍华德·马克斯的市场周期定位技巧
  • 1500字免费降AIGC率的额度,2026年毕业论文查重必备!
  • 1500字免费降AIGC率的额度,2026年毕业论文查重必备!(附每天5次aigc查重)
  • 别再焦虑了!6款实测有效的降ai工具推荐,学姐手把手教你降低ai率!
  • 国外软件,安装即时专业版!
  • 防控近视你需要知道的这些科普常识!
  • 抽奖机随机号码生成:3 种算法实现 + 测试全解析(附完整代码)
  • LLM入门指南:预训练、SFT和强化学习三步构建ChatGPT式大模型
  • LangChain v1.0 Runtime深度解析:构建可测试、可复用的大模型智能体
  • 信息与关系:涌现的三大核心原则
  • c++狼人杀
  • 50天50个小项目 (React19 + Tailwindcss V4) ✨ | DrawingApp(画板组件)
  • 使用自定义注解校验请求参数
  • 敢不敢用一年时间读完这12本书,模型入门必看的12本书!建议收藏!!
  • 对比:Qwen-VL与传统的CNN在图像处理应用
  • 【硬件设计】DC12V输入的防护+滤波设计
  • 快!太快了!一键生成!一键导出!微信自动统计数据报表来了!
  • 智能决策系统日志系统设计:AI架构师的调试与分析技巧
  • 力扣 11.盛最多水的容器 简单的双指针算法 题解