项目场景:
项目场景:SpringBoot 整合Redis客户端lettuce。
问题描述
整合Redis客户端lettuce出现异常:
Caused by: java.lang.NoSuchFieldError: LPOS
at org.springframework.data.redis.connection.lettuce.LettuceConnection$TypeHints.<init>(LettuceConnection.java:1173)
at org.springframework.data.redis.connection.lettuce.LettuceConnection.<clinit>(LettuceConnection.java:96)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.afterPropertiesSet(LettuceConnectionFactory.java:340)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
... 111 common frames omitted
原因分析:
最终分析定位为版本问题,spring-boot-starter-data-redis 2.2.5.RELEASE 关联子包spring-data-redis: 2.5.6.RELEASE,lettuce-core:5.2.2.RELEASE,其中spring-data-redis: 2.5.6.RELEASE版本过高,需要
降低至2.2.5.RELEASE版本。
解决方案:
修改POM.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath />
</parent>
.......
<!-- 1.SpringWeb模块,引入Web服务器undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<!-- 排除spring-data-redis高版本2.5.6.RELEASE -->
<exclusion>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 指定2.2.5.RELEASE版本 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
mvn包间关系如图:
application.yml文件配置redis:
spring:
redis:
# 地址
host: 127.0.0.1
# 端口,默认为6379
port: 6379
# 数据库索引
database: 0
# 密码
password: 123456
# 连接超时时间
timeout: 200
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0