尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

RedisConnectionMonitor.java

RedisConnectionMonitor.java
📅 发布时间:2026/6/19 1:46:18

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

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

相关新闻

  • 代码重构艺术
  • Java线程池全解:工作原理、参数调优
  • MP4 Downloader Pro(MP4下载器)

最新新闻

  • 供应链规则引擎应用:JVS-Rules实现动态供应商评分
  • 嵌入式高精度低功耗ADC选型与应用:Sigma-Delta架构与TC3405实战
  • VS2019使用Microsoft Web Browser控件获取网页源码
  • 2026玉林防水补漏靠谱服务商盘点:屋面/厨卫/外墙/地下室渗水维修详解,适配桂东南盆地回南天防潮暴雨甄选指南 - 宅安选房屋修缮
  • Django毕设项目:基于 Django+Vue 的电信业务资费结算管理系统的设计与实现 基于 Django+Vue 的移动通信资费后台管控平台 (源码+文档,讲解、调试运行,定制等)
  • RE46C109低功耗报警驱动芯片:集成LDO与升压驱动的设计实战

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号