io.lettuce.core.RedisCommandTimeoutException: Command timed out

本文记录了一次SpringBoot项目中使用Redis遇到的连接超时异常,并详细展示了异常堆栈跟踪,最后给出了通过调整连接超时时间来解决该问题的方法。

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

springboot整合redis

2018-07-15 22:52:48.105 ERROR 5644 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out] with root cause

io.lettuce.core.RedisCommandTimeoutException: Command timed out
    at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114) ~[lettuce-core-5.0.4.RELEASE.jar:na]
    at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:62) ~[lettuce-core-5.0.4.RELEASE.jar:na]
    at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80) ~[lettuce-core-5.0.4.RELEASE.jar:na]
    at com.sun.proxy.$Proxy75.hvals(Unknown Source) ~[na:na]
    at org.springframework.data.redis.connection.lettuce.LettuceHashCommands.hVals(LettuceHashCommands.java:368) ~[spring-data-redis-2.0.8.RELEASE.jar:2.0.8.RELEASE]
    at org.springframework.data.redis.connection.DefaultedRedisConnection.hVals(DefaultedRedisConnection.java:909) ~[spring-data-redis-2.0.8.RELEASE.jar:2.0.8.RELEASE]
    at org.springframework.data.redis.core.DefaultHashOperations.lambda$values$10(DefaultHashOperations.java:205) ~[spring-data-redis-2.0.8.RELEASE.jar:2.0.8.RELEASE]
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224) ~[spring-data-redis-2.0.8.RELEASE.jar:2.0.8.RELEASE]
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184) ~[spring-data-redis-2.0.8.RELEASE.jar:2.0.8.RELEASE]
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95) ~[spring-data-redis-2.0.8.RELEASE.jar:2.0.8.RELEASE]
    at org.springframework.data.redis.core.DefaultHashOperations.values(DefaultHashOperations.java:205) ~[spring-data-redis-2.0.8.RELEASE.jar:2.0.8.RELEASE]
    at com.example.demo.service.IRedisService.getAll(IRedisService.java:65) ~[classes/:na]
    at com.example.demo.controller.TestController.getAll(TestController.java:85) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_112]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_112]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.31.jar:8.5.31]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_112]

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

spring:
  redis:
    # Redis数据库索引(默认为0)
    database: 0
    # Redis服务器地址,单机
    host: 127.0.0.1
    # Redis服务器连接端口,单机
    port: 6379
    # Redis服务器连接密码(默认为空)
    password:
    # 连接超时时间(毫秒)
    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步无效)
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值