news 2026/3/22 6:36:54

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/3/20 16:45:57

【PostgreSQL】常用SQL

一、数据库操作1. 查询服务器版本-- 1.1 查询详细版本信息 select version();-- 1.2 查看版本信息&#xff08;简洁版&#xff09; show server_version;-- 1.3 查看数字版本信息&#xff08;含小版本号&#xff09; show server_version_num; -- 或 select current_setting(se…

作者头像 李华
网站建设 2026/3/16 1:52:49

【PostgreSQL】日常总结

一、下载 https://www.enterprisedb.com/downloads/postgres-postgresql-downloadshttps://get.enterprisedb.com/postgresql/postgresql-11.2-1-windows-x64.exe 二、安装 安装完成&#xff0c;服务启动 三、使用 psql.exe -U postgres -p 5432 四、Navicat连接 五、常用SQL …

作者头像 李华
网站建设 2026/3/20 10:49:59

【图像增强】基于波长补偿和去雾水下图像增强附Matlab代码

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。 &#x1f34e; 往期回顾关注个人主页&#xff1a;Matlab科研工作室 &#x1f34a;个人信条&#xff1a;格物致知,完整Matlab代码获取及仿…

作者头像 李华
网站建设 2026/3/12 18:23:22

VMware技术文章

技术融合背景云原生与VMware的协同价值&#xff1a;探讨传统虚拟化与容器化、微服务的互补性&#xff0c;分析企业混合云场景下的技术需求。核心技术与架构VMware Tanzu产品线解析&#xff1a;包括Tanzu Kubernetes Grid&#xff08;TKG&#xff09;、Tanzu Application Servic…

作者头像 李华
网站建设 2026/3/20 5:25:58

解析 React 的 ‘Keyed Fragment’:为什么在 Fragment 上也需要 Key?

各位同学&#xff0c;大家好&#xff01;今天我们将深入探讨 React 中一个看似简单却蕴含深意的特性——Fragment&#xff0c;尤其是当它与 Key 结合时所展现出的强大能力与必要性。我们将聚焦于一个核心问题&#xff1a;为什么在 Fragment 上也需要 Key&#xff1f;这个问题常…

作者头像 李华
网站建设 2026/3/14 2:44:48

idea多模块项目运行设置

以该项目为例&#xff0c;backend内是后端部分&#xff0c;frontend内是前端部分1.添加后端模块&#xff1a;File → Project Structure → Modules点击 → Import Module选 C:\javacode\audio\backend\pom.xml 或 build.gradle选择 "Create module groups"&#xf…

作者头像 李华