SpringBoot整合Redis:java.io.IOException: 远程主机强迫关闭了一个现有的连接。或者控制台报连接超时异常

场景:
项目启动过后 Redis 连接过一会就会断开,报如下问题:

问题1:

2022-12-05 23:05:18.287 ERROR 10752 --- [sson-netty-1-11] o.redisson.client.handler.CommandsQueue  :139 - Exception occured. Channel: [id: 0x7b1e0211, L:/xxx:60851 - R:/xxx:6379]

java.io.IOException: 远程主机强迫关闭了一个现有的连接。
	at java.base/sun.nio.ch.SocketDispatcher.read0(Native Method)
	at java.base/sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
	at java.base/sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:276)
	at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:233)
	at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:223)
	at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:358)
	at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253)
	at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132)
	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.base/java.lang.Thread.run(Thread.java:834)

问题2:

Redis server response timeout (3000 ms) occured for command: (EVAL) with params: [if (redis.call('exists', KEYS[1]) == 0) then redis.call('publish', KEYS[2], ARGV[1]); return 1; end;..., 2, a:TestParamDtoOne(id=null, code=, userName=, email=), redisson_lock__channel:{a:TestParamDtoOne(id=null, code=, userName=, email=)}, 0, 30000, 033aee3e-7de4-415f-b627-69b3d7e17566:119] channel: [id: 0x9700eaf4, L:/xxx:60842 - R:/xxx:6379]

问题3:

nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out

一、解决

1、设置连接空闲超过 N(秒或毫秒)后关闭,0为禁用:redis.timeout: 60s(这里设置和tcp-keepalive的值一致)

2、设置 redis 服务端的配置文件 redis.conf 中 tcp-keepalive 的时间为60(单位秒)(TCP连接存活时间):tcp-keepalive 60

二、总结注意项

1、tcp-keepalive 超时时间默认是300,应该把这个存活时间改成小于或等于超时时间 redis.timeout

2、如果 tcp-keepalive 存活时间大于超时时间,存活时间还没有结束(连接还没关闭)就又创建新的连接,创建的连接多了就会远程主机强迫关闭现有连接

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值