升级至Spring Boot 3.0.6+Jedis后单元测试失败排查求助
Spring Boot 3.0.6 + Java 17 下Jedis单元测试失败问题排查方案
1. 优先确认版本兼容性
Spring Boot 3.0.x的spring-boot-starter-data-redis默认依赖Jedis 4.x系列,和Spring Boot 2.x依赖的Jedis 3.x存在API差异:
- 删掉
pom.xml中手动指定的Jedis版本,直接用Spring Boot依赖管理的版本,避免版本不匹配。 - 执行
mvn dependency:tree查看最终依赖的Jedis版本,确认是Spring Boot 3.0.6对应的官方兼容版本(通常为4.3.x左右)。
2. 解决DNS解析连接失败问题
底层报错“Failed to connect to any host resolved for DNS name”和Java 17网络栈变更有关:
- 把配置里的
localhost换成127.0.0.1,强制用IPv4连接,规避IPv6解析失败的问题。 - 给单元测试的JVM启动参数加
-Djava.net.preferIPv4Stack=true,强制优先使用IPv4网络栈。
3. 适配RedisConfig配置类
Spring Boot 3.x对Redis配置API有调整,旧的配置代码可能失效:
- 检查配置类里是否用了Spring Boot 2.x的废弃API,比如
JedisConnectionFactory的旧构造器或配置方法。 - 参考适配后的集群配置示例:
@Configuration public class RedisConfig { @Bean public JedisCluster jedisCluster(RedisProperties properties) { Set<HostAndPort> nodes = properties.getCluster().getNodes().stream() .map(node -> { String[] parts = node.split(":"); return new HostAndPort(parts[0], Integer.parseInt(parts[1])); }) .collect(Collectors.toSet()); JedisPoolConfig poolConfig = new JedisPoolConfig(); // 按需配置连接池参数 poolConfig.setMaxTotal(properties.getJedis().getPool().getMaxActive()); poolConfig.setMaxIdle(properties.getJedis().getPool().getMaxIdle()); poolConfig.setMinIdle(properties.getJedis().getPool().getMinIdle()); return new JedisCluster(nodes, poolConfig); } }
- 确保配置正确读取Spring Boot 3.x的
RedisProperties,避免配置项读取错误导致连接失败。
4. 检查本地Redis集群状态
- 确认本地测试用Redis集群正常运行,
localhost:6379节点属于集群成员,且插槽信息正常。 - 用
redis-cli连接集群,执行cluster slots命令,验证插槽信息能正常获取,排除Redis集群本身的问题。
5. 处理Java 17模块权限限制
Java 17模块系统可能限制Jedis的网络访问:
- 如果单元测试用了
module-info.java模块化配置,需要添加requires java.net.http;、requires java.net;等依赖,或者在启动参数里加--add-opens java.base/java.net=ALL-UNNAMED解除权限限制。
内容的提问来源于stack exchange,提问作者Raul Mureșan
相关产品推荐
相关产品推荐

