springboot整合redis报以下异常
org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 20 second(s)
springboot版本:2.1.1.RELEASE
redis版本:3.2.100
springboot中redis配置文件如下
# redis 配置
redis:
# 地址
host: 127.0.0.1
# 端口,默认为6379
port: 6379
# 密码
#password: 7fdc99e19da868f1536d870de3f2f944
# 连接超时时间
timeout: 20s
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 0
# 连接池中的最大空闲连接
max-idle: 8
# 连接池的最大数据库连接数
max-active: 8
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
查找网上的解决方法为修改配置文件中的timeout值,无效。
后来发现应该修改方法调用中的timeout值,使其小于配置文件中的timeout值。如下
AcsConfigurer acsConfig= (AcsConfigurer) redisCache.brpop(ipStr,10, TimeUnit.SECONDS);
方法内层为
/**
* 取值 - <brpop:阻塞式> - 推荐使用
* @param key 键
* @param timeout 超时时间
* @param timeUnit 给定单元粒度的时间段
* TimeUnit.DAYS //天
* TimeUnit.HOURS //小时
* TimeUnit.MINUTES //分钟
* TimeUnit.SECONDS //秒
* TimeUnit.MILLISECONDS //毫秒
* @return
*/
public Object brpop(String key, long timeout, TimeUnit timeUnit) {
try {
return redisTemplate.opsForList().rightPop(key, timeout, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}