使用阻塞操作后,redis连接超时

本文探讨了在SpringBoot项目中使用Redis的brpop操作时遇到的超时异常问题,详细分析了错误原因,并提供了两种有效的解决方案,帮助开发者避免因配置不当导致的系统阻塞。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目是springboot,在之前使用redis时一切正常(配置连接超时为1500ms),后来为了实现分布式队列,增加了redis list的brpop操作,阻塞时间为30s,就报了这个错

Caused by: io.lettuce.core.RedisCommandTimeoutException: Command timed out after 5 second(s)
	at io.lettuce.core.ExceptionFactory.createTimeoutException(ExceptionFactory.java:51)
	at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114)
	at io.lettuce.core.AbstractRedisAsyncCommands.select(AbstractRedisAsyncCommands.java:1194)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:57)
	at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80)
	at com.sun.proxy.$Proxy146.select(Unknown Source)
	at org.springframework.data.redis.connection.lettuce.LettuceConnection.getDedicatedConnection(LettuceConnection.java:956)
	at org.springframework.data.redis.connection.lettuce.LettuceListCommands.bRPop(LettuceListCommands.java:407)
	... 12 more

上述问题的解决方法有两个:

1. 去掉配置的超时时间:  #spring.redis.timeout=1500  #方便,但不推荐

2. 将其设置为超过阻塞操作的等待时间, 比如我这里设置的阻塞30s,可以将等待时间设置为31s, spring.redis.timeout=3100

总结:
  redis的操作需要时间,如果超过这个时长,将会报timeout的错。 
  一般情况下 推荐配置一下这个时长 它可以在开发人员无意中做了耗时操作(如 新手做了keys *)时自动断开连接,同时停止阻塞所在的jvm线程。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值