如何解决Command timed out after 1 minute(s) 问题

org.springframework.dao.QueryTimeoutException: Redis command timed out; 
nested exception is io.lettuce.core.RedisCommandTimeoutException:
 Command timed out after 1 minute(s)

今天上班刚打开项目 发现项目突然报这个错 检查自己的代码 也没有问题 

于是我就开始找解决方法:

问题原因:

1.可能是redis长时间处于高并发情况 或服务异常断链

2.可能是redis配置里面阻塞为负数导致连接通道关闭了

3.可能是springboot2.x,springBoot默认使用的Redis客户端是lettuce,而不是jedis,lettuce连接池

科普一下知识:

       spring-data-redis内置了两款驱动,jedis和lettuce 。springboot1.X版本默认jedis实现,                   springboot2.X默认lettuce实现 。lettuce只要断掉连接后,自己并没有去重连的机制,最后导致time out 。
       lettuce:基于netty实现,线程安全,但默认只有一个实例 。

大部分人说设置springBoot Redis配置设置超时时间 :timeout

 其实不管你设置多久多会超时  因为是连接通道被关闭了 无法重连 其次max-wait 设置负数(永久等待)导致无法生成连接 

解决方法:

调整 POM,排除 lettuce,使用 jedis

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

调整 YML 参数

  redis:
    # 连接超时 毫秒
    connectTimeout: 1800
    # 连接超时时间
    timeout: 60s
    host: 127.0.0.1
    port: 6379
    database: 1
    password:
    jedis:
      pool:
        # 连接池中的最小空闲连接
        min-idle: 0
        # 连接池中的最大空闲连接
        max-idle: 8
        # 连接池的最大数据库连接数
        max-active: 8
        # #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: 3600ms

以上均为个人解决方案 以此记录 如有不对请各位指正!

猜你喜欢

转载自blog.csdn.net/yzs2022/article/details/131810222