美文网首页
springboot整合redis异常汇总

springboot整合redis异常汇总

作者: 轰鸣龙 | 来源:发表于2020-12-17 17:26 被阅读0次

项目中要用到redis,于是尝试通过springboot整合redis,redis集群采用3主3从,搭建方法参考:https://developer.aliyun.com/article/767317

springboot从2.x版本开始默认使用lettuce访问redis,所以部分配置由jedis改为lettuce

spring.redis.cluster.nodes=ip1:port1,ip2:port2,ip3:port3,ip4:port4,ip5:port5,ip6:port6

#spring.redis.sentinel.nodes=ip1:port1
#spring.redis.sentinel.master=mymaster

# Redis 数据库索引(默认为 0)
spring.redis.database=0
# Redis 服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1  
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8  
# 连接池中的最小空闲连接 
spring.redis.lettuce.pool.min-idle=0    

# 连接超时时间(毫秒)
#spring.redis.timeout=0  这条配置有问题,需要注释掉或者改为500ms

异常1: Cannot retrieve initial cluster partitions from initial URIs [RedisURI [host='192.168.1.1', port=6379]]
这个问题绕了很多弯路,根据网上的很多方案,以为是redis集群配置成了主从模式,但是springboot连接使用的是cluster模式,最后发现问题出在spring.redis.timeout=0这条配置上。
spring.redis.timeout,在1.0中,时间相关的配置参数类型为int,默认单位为毫秒,而且设置为0意味着不超时,2.x版本中参数类型为Duration,需要添加单位,如:500ms

异常2: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
添加依赖即可
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.8.0</version>
</dependency>

异常3:Caused by: io.lettuce.core.RedisCommandExecutionException: CLUSTERDOWN The cluster is down
1.如果在配置文件中给redis设置了密码,还需要在redis-cli终端设置一下
config set requirepass xxxx(密码)
2.redis-cli 检查cluster的状态
cluster info
3.配置文件中cluster-require-full-coverage设置为no,重启redis服务,可参考:https://stackoverflow.com/questions/53594257/clusterdown-the-cluster-is-down-in-redis

相关文章

网友评论

      本文标题:springboot整合redis异常汇总

      本文链接:https://www.haomeiwen.com/subject/wqbzgktx.html