编辑/etc/ssh/sshd_config,添加配置项:

ClientAliveInterval 600      
ClientAliveCountMax 10

配置完后保存,重启服务:

service sshd restart

ClientAliveCountMax
sshd(8) 在未收到任何客户端回应前最多容许发送多少个"alive"消息。默认值是 3 。
到达这个上限后,sshd(8) 将强制断开链接、关闭会话。
须要注意的是,"alive"消息与 TCPKeepAlive 有很大差别。
"alive"消息是经过加密链接发送的,所以不会被欺骗;而 TCPKeepAlive 倒是能够被欺骗的。
若是 ClientAliveInterval 被设为 15 而且将 ClientAliveCountMax 保持为默认值,
那么无应答的客户端大约会在45秒后被强制断开。这个指令仅能够用于SSH-2协议。

ClientAliveInterval
设置一个以秒记的时长,若是超过这么长时间没有收到客户端的任何数据,
sshd(8) 将经过安全通道向客户端发送一个"alive"消息,并等候应答。
默认值 0 表示不发送"alive"消息。这个选项仅对SSH-2有效。

以下是整个经过:

本地开发是发现隔一段时间没请求redis就会报错:

2022/11/15 21:31:35 ERROR [com.SpringLearn.common.redis.JedisUtils] - Redis exception; nested exception is io.lettuce.core.RedisException: java.io.IOException: 远程主机强迫关闭了一个现有的连接。
org.springframework.data.redis.RedisSystemException: Redis exception; nested exception is io.lettuce.core.RedisException: java.io.IOException: 远程主机强迫关闭了一个现有的连接。
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:74)
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41)
at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)
at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:268)
at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.convertLettuceAccessException(LettuceStringCommands.java:799)
at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.get(LettuceStringCommands.java:68)
at org.springframework.data.redis.connection.DefaultedRedisConnection.get(DefaultedRedisConnection.java:253)
at org.springframework.data.redis.core.DefaultValueOperations$1.inRedis(DefaultValueOperations.java:57)
at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:59)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:53)
at com.SpringLearn.common.redis.JedisUtils.get(JedisUtils.java:114)
at com.SpringLearn.common.framework.securi

网上看了很多解决办法,都是timeout和tcp-keepalive的配置,多次试验后都无效,最奇怪的是putty链接服务器也是过一会就断开了,mysql出现了这种情况,于是猜测不是redis的配置问题,开启本地虚拟机测试发现没有出现断开链接的问题,查了putty自动断开链接的解决方案(文章开头),修改完后上面的问题竟然解决了,本地服务的输出日志出现了下面的日志:

2020/07/09 20:20:02 INFO  [io.lettuce.core.protocol.ConnectionWatchdog] - Reconnecting, last destination was /139.xxx.xx.xx:6666
2020/07/09 20:20:03 INFO  [io.lettuce.core.protocol.ReconnectionHandler] - Reconnected to 139.xxx.xx.xx:6666
2020/07/09 20:20:03 INFO  [io.lettuce.core.protocol.ConnectionWatchdog] - Reconnecting, last destination was /139.xxx.xxx.xx:6666
2020/07/09 20:20:03 INFO  [io.lettuce.core.protocol.ReconnectionHandler] - Reconnected to 139.xxx.xxx.xxx:6666
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐