spring-data-redis报错NoSuchMethodError:org.springframework.util.Assert.isTrue

本文解决了一个关于使用Spring框架启动Redis时报错的问题,错误源于版本不兼容。通过将spring-data-redis版本从2.1.0.RELEASE降至1.8.4.RELEASE,问题得以解决。文章还探讨了版本更新可能导致的方法签名变化。

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

使用Redis启动报错:

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'redisTemplate' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'jedisConnFactory' while setting bean property 'connectionFactory'; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'jedisConnFactory' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: 
Constructor threw exception; 
nested exception is java.lang.NoSuchMethodError: org.springframework.util.Assert.isTrue(ZLjava/util/function/Supplier;)V

当前pom配置的版本:

 <jedis-version>2.9.0</jedis-version>
 <spring-data-redis-version>2.1.0.RELEASE</spring-data-redis-version>

spring-data-redis-version换到1.8.4.RELEASE。问题解决。

如何快读定位是版本的兼容问题,确实是靠经验哇。
看了下,org.springframework.util.Assert.isTrue()方法至少在2.5就有了,可能版本更新,调整了下。高版本调用的是新增方法。
问题解决来源

2025-03-17 05:17:30.641 ERROR 15424 --- [nio-3004-exec-1] c.bishe.product.GlobalExceptionHandler : 全局异常捕获: URL=/product/list, Error=Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: READONLY You can't write against a read only replica. org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: READONLY You can't write against a read only replica. at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:54) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:52) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:273) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.convertLettuceAccessException(LettuceStringCommands.java:799) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.set(LettuceStringCommands.java:180) ~[spring-data-redis-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.data.redis.connection.DefaultedRedisConnection.set(DefaultedRedisConn
03-18
<think>好的,我现在需要解决用户在Spring Data Redis中遇到的“READONLY You can't write against a read only replica”错误。首先,我需要理解这个错误的根本原因。 根据用户提供的错误信息,问题似乎发生在尝试向Redis的只读副本执行写操作时。Redis的主从架构中,从节点默认是只读的,不能接受写命令。如果应用程序错误地将写请求发送到了从节点而不是主节点,就会触发这个错误。 接下来,我需要参考用户提供的引用内容。引用[2]提到权限问题,但这里的问题更可能与节点的角色有关。引用[4]中的解决方法是关于配置密码后未重启Redis的情况,但当前问题可能涉及配置错误的主从连接。 可能的解决方案包括: 1. 检查Redis配置,确保写操作发送到主节点,而不是从节点。 2.Spring Boot的配置中正确指定主节点的地址和端口。 3. 如果确实需要向从节点写入,修改从节点的配置,但这不是推荐的做法。 4. 确保应用程序的连接池配置正确,区分读写操作。 还需要验证用户的应用配置,例如检查`application.yml`中的`spring.redis.host`和`spring.redis.port`是否指向主节点。如果使用了集群或哨兵模式,可能需要调整配置方式。 另外,Lettuce客户端在Spring Boot中默认使用的主从连接可能需要明确配置,避免自动路由写请求到从节点。可能需要设置`read-only`为false,或者在连接字符串中指定正确的角色。 最后,可能需要指导用户如何检查Redis节点的角色,使用`INFO replication`命令确认主从状态,并确保应用程序连接的是主节点。</think>### 解决 Redis 只读副本导致的 "READONLY You can’t write against a read only replica" 错误 #### 问题原因 该错误表示应用程序尝试向 Redis 的**只读副本(从节点)执行写操作**。在 Redis 主从架构中,默认配置下从节点仅支持读操作,无法处理写命令(如`SET`, `HSET`, `DEL`等)。若 Spring Data Redis 误将写请求发送到从节点,则会触发此异常[^2][^4]。 --- #### 解决方案 ##### 1. **检查 Redis 连接配置** 确保 Spring Boot 的配置文件中**写操作指向主节点**(Master): ```yaml spring: redis: host: 127.0.0.1 # 主节点地址 port: 6379 # 主节点端口 password: your_password # 若使用哨兵或集群模式,需额外配置 ``` *若使用哨兵模式*,需明确指定主节点名称: ```yaml spring: redis: sentinel: master: mymaster nodes: 192.168.1.1:26379,192.168.1.2:26379 ``` ##### 2. **强制禁用从节点写权限** 在 Redis 从节点配置文件中添加: ```conf replica-read-only yes # 确保从节点为只读模式(默认已开启) ``` 重启从节点使配置生效。 ##### 3. **代码层读写分离** 通过注解或代码显式区分读写操作: ```java @Autowired private RedisTemplate<String, String> redisTemplate; // 写操作强制路由到主节点 public void writeData(String key, String value) { redisTemplate.opsForValue().set(key, value); } // 读操作允许使用从节点 @ReadOnly // Lettuce 支持该注解 public String readData(String key) { return redisTemplate.opsForValue().get(key); } ``` ##### 4. **调整 Lettuce 客户端配置** 在 `application.yml` 中显式声明节点角色: ```yaml spring: redis: lettuce: pool: nodes: master-host:6379,replica-host:6380 read-only: false # 禁止向从节点发送写请求 ``` --- #### 验证步骤 1. **检查 Redis 节点角色**: ```bash redis-cli -h <host> -p <port> INFO replication ``` 输出中若包含 `role:slave`,则为从节点。 2. **监控 Spring Boot 连接目标**: 在日志中启用调试模式,观察连接地址: ```yaml logging: level: org.springframework.data.redis: DEBUG ``` ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值