项目场景:
Spring Boot集成Redis集群
单节点配置:
spring.redis.database = 0
spring.redis.host = 127.0.0.1
spring.redis.password = test@redis
spring.redis.port = 8003
# 连接超时时间 单位 ms(毫秒)
spring.redis.timeout = 3000
集群配置:
#集群配置
spring.redis.cluster.nodes = 127.0.0.1:8001,127.0.0.1:8002
spring.redis.database = 0
spring.redis.password = test@redis
# 连接超时时间 单位 ms(毫秒)
spring.redis.timeout = 3000
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
问题描述
Redis exception; nested exception is io.lettuce.core.RedisException: java.lang.UnsupportedOperationException
完整错误 :
org.springframework.data.redis.RedisSystemException: Redis exception; nested exception is io.lettuce.core.RedisException: java.lang.UnsupportedOperationException
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:74)
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41)
at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)
at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:257)
at org.springframework.data.redis.connection.lettuce.LettuceKeyCommands.convertLettuceAccessException(LettuceKeyCommands.java:650)
at org.springframework.data.redis.connection.lettuce.LettuceKeyCommands.del(LettuceKeyCommands.java:98)
at org.springframework.data.redis.connection.DefaultedRedisConnection.del(DefaultedRedisConnection.java:61)
at org.springframework.data.redis.core.RedisTemplate.lambda$delete$2(RedisTemplate.java:709)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
at org.springframework.data.redis.core.RedisTemplate.delete(RedisTemplate.java:709)
原因分析:
线上部署的redis版本较高,而spring boot引入的版本较低,版本不一致导致的问题
解决方案:
lettuce排除低版本,引入高版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!-- 排除低版本,部署的redis版本较高,低版本lettuce会报错java.lang.UnsupportedOperationException -->
<exclusions>
<exclusion>
<artifactId>lettuce-core</artifactId>
<groupId>io.lettuce</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入高版本 -->
<dependency>
<artifactId>lettuce-core</artifactId>
<groupId>io.lettuce</groupId>
<version>6.1.5.RELEASE</version>
</dependency>