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 存活时间大于超时时间,存活时间还没有结束(连接还没关闭)就又创建新的连接,创建的连接多了就会远程主机强迫关闭现有连接

你可能感兴趣的:(Redis和SpCache,异常和开发经验总结,redis)