【解决】io.lettuce.core.RedisCommandTimeoutException: Command timed out

SpringBoot Redis超时异常解决
本文详细解析了SpringBoot项目中使用Redis时遇到的超时异常问题,提供了具体的错误堆栈信息,并给出了合理的解决方案,即调整连接超时时间设置。

springboot(spring也一样的)整个redis 的时候报如下错误:

org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out
	at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:70) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:257) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.data.redis.connection.lettuce.LettuceConnection.pSubscribe(LettuceConnection.java:747) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.data.redis.listener.RedisMessageListenerContainer$SubscriptionTask.eventuallyPerformSubscription(RedisMessageListenerContainer.java:783) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.data.redis.listener.RedisMessageListenerContainer$SubscriptionTask.run(RedisMessageListenerContainer.java:750) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]
 
Caused by: io.lettuce.core.RedisCommandTimeoutException: Command timed out
	at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114) ~[lettuce-core-5.0.2.RELEASE.jar:na]
	at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:62) ~[lettuce-core-5.0.2.RELEASE.jar:na]
	at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80) ~[lettuce-core-5.0.2.RELEASE.jar:na]
	at com.sun.proxy.$Proxy70.psubscribe(Unknown Source) ~[na:na]
	at org.springframework.data.redis.connection.lettuce.LettuceSubscription.doPsubscribe(LettuceSubscription.java:54) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.data.redis.connection.util.AbstractSubscription.pSubscribe(AbstractSubscription.java:130) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.springframework.data.redis.connection.lettuce.LettuceConnection.pSubscribe(LettuceConnection.java:745) ~[spring-data-redis-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	... 3 common frames omitted

错误原因:连接超时时间设置的过于短暂(我这边设置成了0),修改为5000左右即可

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.30.103
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000

 

<think> 我们正在解决io.lettuce.core.RedisCommandTimeoutException: Command timed out after 500 millisecond(s)错误。根据引用,我们可以从以下几个方面入手: 1. **调整超时时间**:将默认的500毫秒超时时间增加,比如增加到2000毫秒(2秒)或更高。这可以通过在Spring Boot的配置文件中设置`spring.redis.timeout`属性来实现(引用[1]和[2]都提到了超时时间设置的重要性)。例如: ```properties spring.redis.timeout=2000 ``` 2. **检查连接池配置**:确保使用了连接池,并且配置了合适的参数(引用[3]和[4]提到连接池配置)。如果使用Lettuce,需要添加commons-pool2依赖,并配置连接池参数。例如: ```properties spring.redis.lettuce.pool.max-active=100 spring.redis.lettuce.pool.max-idle=50 spring.redis.lettuce.pool.min-idle=10 spring.redis.lettuce.pool.max-wait=-1 ``` 同时,在pom.xml中添加依赖: ```xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> ``` 3. **切换客户端**:如果Lettuce客户端在某些环境下存在问题,可以考虑切换到Jedis(引用[3]提到)。在pom.xml中排除Lettuce并引入Jedis: ```xml <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> <version>3.3.0</version> </dependency> ``` 然后,在配置文件中配置Jedis连接池(引用[4]): ```properties spring.redis.jedis.pool.max-wait=2000 spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-idle=9 spring.redis.jedis.pool.min-idle=0 ``` 4. **检查服务器性能和网络**:超时可能是由于Redis服务器负载过高或网络延迟引起(引用[1]和[2])。需要监控服务器资源使用情况(CPU、内存、网络)和Redis服务器性能指标。 5. **代码优化**:避免在Redis操作中执行耗时过长的操作,如大键的删除或复杂查询。考虑使用异步操作或批量操作来减少阻塞时间。 综上所述,我们首先尝试增加超时时间,并配置连接池。如果问题仍然存在,可以尝试切换客户端到Jedis。同时,确保服务器和网络状况良好。 §§相关问题§§ 1. 如何监控Redis服务器的性能? 2. Lettuce和Jedis客户端的主要区别是什么? 3. 如何优化Redis命令以减少超时? </think> ### 解决`io.lettuce.core.RedisCommandTimeoutException: Command timed out after 500 millisecond(s)`错误的方法 #### 1. **调整超时时间(最直接方案) - **原因**:默认500ms超时在高负载或网络波动时不足 - **解决方案**:在配置文件中增加超时时间(单位:毫秒) ```properties # application.properties spring.redis.timeout=2000 # 建议2-5秒 ``` - **验证方法**:逐步增加超时值(如1000ms→2000ms)观察异常是否消失[^1][^2] #### 2. **优化连接池配置(关键步骤) - **原因**:连接池不足导致所有连接被占用 - **解决方案**: ```properties # Lettuce连接池配置 spring.redis.lettuce.pool.max-active=100 # 最大连接数 spring.redis.lettuce.pool.max-idle=30 # 最大空闲连接 spring.redis.lettuce.pool.min-idle=10 # 最小空闲连接 spring.redis.lettuce.pool.max-wait=3000 # 获取连接最大等待时间(ms) ``` - **必须添加依赖**(Maven): ```xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> ``` 缺少此依赖会导致连接池失效[^3] #### 3. **切换Redis客户端(备选方案) - **原因**:Lettuce在某些场景存在已知bug[^3] - **解决方案**:改用Jedis客户端 ```xml <!-- pom.xml --> <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> <version>3.7.0</version> </dependency> ``` Jedis配置示例[^4]: ```properties spring.redis.jedis.pool.max-active=50 spring.redis.jedis.pool.max-wait=2000 ``` #### 4. **服务器与网络优化 - **诊断工具**: - `redis-cli --latency` 检测Redis响应延迟 - `ping`命令测试网络往返时间 - **优化建议**: - 确保Redis服务器与客户端在同一可用区 - 升级Redis服务器配置(特别是CPU核心数)[^1] - 检查防火墙/安全组规则是否限制连接 #### 5. **代码级优化 - **避免阻塞操作**: - 将耗时命令(如`KEYS*`)替换为`SCAN` - 大value分片存储 - **批量操作**:使用`pipeline`减少网络往返次数 ```java List<Object> results = redisTemplate.executePipelined(...) ``` > **建议实施顺序**: > 1. 先增加超时时间 → 2. 配置连接池 → 3. 检查服务器负载 → 4. 切换客户端(若前3步无效)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值