redis io.lettuce.core.RedisCommandTimeoutException: Command timed out after

redis报错远程主机强迫关闭了一个现有的连接以及超时问题

原创 2019-10-11 16:37 阅读(841)次

问题说明:

spring boot2.x+redis开发时,总是时不是发生redis超时,时不时报:远程主机强迫关闭了一个现有的连接以及超时问题。这个问题总是偶有出现,烦人。

spring boot2.x版本默认redis连接池为lettuce,以前在非spring boot项目中使用jedis连接redis时,好像没有过这种烦人的问题,搞的现在对redis有阴影。。。我的项目也是使用默认的lettuce连接redis。

我们先看超时问题的报错:

io.lettuce.core.RedisCommandTimeoutException: Command timed out after 5 second(s)

再看看远程主机强迫关闭了一个现有的连接报错:

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)

真的烦。

 

这两个问题我暂时的解决办法(有待观察):

将spring boot redis配置设置超时时间比redis.conf中的timeout要小,比如spring boot redis中设置超时30秒,那么服务器中redis timeout设置为40秒;另外将redis.conf中的tcp-keepalive改成10:

 

# Unix socket.
#
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /tmp/redis.sock
# unixsocketperm 700

# Close the connection after a client is idle for N seconds (0 to disable)
timeout 40

# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Take the connection alive from the point of view of network
#    equipment in the middle.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
tcp-keepalive 10


 

 

这样好像就不会报Command timed out after xxx second(s)了,但是对于“远程主机强迫关闭了一个现有的连接”这个问题貌似只能把lettuce连接池切回jedis了,我今天实在是忍受不了lettuce了。把配置文件改成如下:

 

spring:
  redis:
    host: xxxxx
    port: 6379
    password: xxxxxx
    timeout: 30s
#    lettuce:
#      pool:
#        min-idle: 10
#        max-idle: 20
#        max-active: 200
#        max-wait: -1ms
    jedis:
      pool:
        min-idle: 10
        max-idle: 20
        max-wait: -1ms
        max-active: 200

 

Maven pom.xml:

 

		<!-- redis缓存 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
			<exclusions>
				<exclusion>
					<groupId>redis.clients</groupId>
					<artifactId>jedis</artifactId>
				</exclusion>
				<exclusion>
					<groupId>io.lettuce</groupId>
					<artifactId>lettuce-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.6.2</version>
		</dependency>

改完后,我在本地观察了一会,好像不会出现以上两个问题。不过上到生产可能还要观察一段时间。唉,烦

(观察了几天还真没有再发现问题了)

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这个错误表示在执行 Redis 命令时超时了。RedisCommandTimeoutExceptionLettuce 客户端库中的异常类,它指示 Redis 命令执行超时。 在默认情况下,Lettuce 客户端库设置了一个命令执行的超时时间,如果 Redis 服务器在该时间内没有返回结果,则会抛出这个异常。在你的情况下,命令执行时间超过了 100 秒。 有几个可能的原因导致命令执行超时: 1. Redis 服务器过载:如果 Redis 服务器处理大量请求或者数据量过大,可能导致命令执行时间增长。可以通过监控 Redis 服务器的负载情况来验证这一点。 2. 网络延迟:如果 Redis 服务器和 Lettuce 客户端之间的网络延迟增加,命令执行时间会增加。可以通过检查网络连接和延迟来排除这个问题。 3. 命令复杂性:某些复杂的 Redis 命令(如聚合操作或大规模数据操作)可能需要更长的执行时间。可以检查你正在执行的命令是否包含复杂操作。 为了解决这个问题,你可以尝试以下几个步骤: 1. 检查 Redis 服务器的性能和负载情况,确保它没有过载。 2. 检查网络连接和延迟,确保网络通畅。 3. 优化你的 Redis 命令,尽量减少复杂操作或者将其拆分成多个较小的操作。 4. 调整 Lettuce 客户端的命令执行超时时间,可以根据你的实际需求增加超时时间。 如果问题仍然存在,你可能需要进一步调查和诊断,可能需要检查日志以获取更多细节,或者考虑升级 Redis 服务器的硬件配置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值