Spring boot redis java.io.IOException: 远程主机强迫关闭了一个现有的连接

现象

在测试环境部署的若依版本,经常会报如下的错误。而我在开发环境基本没有遇到过。

16:38:01.040 [lettuce-nioEventLoop-4-2] INFO  i.l.c.p.CommandHandler - [log,217] - 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(Unknown Source)
	at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
	at sun.nio.ch.IOUtil.read(Unknown Source)
	at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
	at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253)
	at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1133)
	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:714)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
	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(Unknown Source)
16:38:01.104 [lettuce-eventExecutorLoop-1-3] INFO  i.l.c.p.ConnectionWatchdog - [log,171] - Reconnecting, last destination was localhost/127.0.0.1:6379
16:38:01.110 [lettuce-nioEventLoop-4-3] INFO  i.l.c.p.ReconnectionHandler - [lambda$null$3,177] - Reconnected to localhost:6379

原因分析

从这个报错日志上来看,有几点是确定的。

  • 这是一个IO异常
java.io.IOException: 远程主机强迫关闭了一个现有的连接。
  • 这个异常应该与redis服务有关
Reconnecting, last destination was localhost/127.0.0.1:6379

因为redis服务器是部署在本地的,所以网络连接肯定是畅通无阻的。出现IO异常的原因应该就是在redis服务了,也许是可以redis服务中做了个会话连接时长的限制。

解决办法

springboot 使用 lettuce做redis 的连接池,lettuce 调用netty
与redis服务器通讯。redis服务器的连接超时缺省是65,改为0后不再报上面的错。

操作详解:
编辑你的redis.conf配置文件,比如:

客户端空闲 N 秒后关闭连接(0 表示禁用)
timeout 0

参考

https://gitee.com/y_project/RuoYi-Vue/issues/I3DP6H?_from=gitee_search

猜你喜欢

转载自blog.csdn.net/lxyoucan/article/details/124816647