使用Spring Integration创建ZookeeperMetadataStore Bean报错如何解决
你定义的MetadataStore Bean代码本身没有语法和逻辑问题,报错完全由依赖版本冲突导致。
问题根因
该NoSuchMethodError是Apache Curator依赖版本不兼容引发的:
- Spring Integration Zookeeper 5.5.2版本默认适配Curator 4.x,该版本中
PathChildrenCache.getListenable()方法仍然存在 - Spring Cloud Zookeeper新版本默认引入Curator 5.x,
PathChildrenCache在Curator 5.x中被重构为接口,原getListenable()方法被移除,你还手动排除了curator-recipes依赖,进一步加剧了版本不一致问题
可行解决方案
方案1:升级Spring Integration Zookeeper版本(推荐)
Spring Integration Zookeeper 6.0及以上版本已经完成Curator 5.x的适配,替换了所有废弃API的调用。你只需要修改spring-integration-zookeeper的依赖版本到和你当前Spring Boot/Spring Cloud版本匹配的6.x+版本即可,同时删除spring-cloud-starter-zookeeper-config中对curator-recipes的排除配置。
示例依赖修改:
<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-zookeeper</artifactId> <!-- 适配Spring Boot 3.x可使用6.2.x系列稳定版 --> <version>6.2.10</version> </dependency> <!-- 移除原有curator-recipes排除配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-config</artifactId> </dependency>
方案2:锁定Curator兼容版本(适用于必须保留Spring Integration Zookeeper 5.5.2的场景)
如果你无法升级Spring Integration版本,可以手动锁定Curator版本为4.3.0(该版本是Spring Integration Zookeeper 5.5.2的官方适配版本),同时删除对curator-recipes的排除:
<!-- 在pom文件的dependencyManagement节点中统一锁定curator版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.3.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.3.0</version> </dependency> </dependencies> </dependencyManagement>
注意:请根据你实际部署的Zookeeper服务端版本选择对应兼容的Curator版本,避免出现服务端客户端通信异常:Curator 4.x兼容Zookeeper 3.5.x、3.6.x;Curator 5.x兼容Zookeeper 3.6.x、3.7.x、3.8.x
内容的提问来源于stack exchange,提问作者Karanjot Singh
相关产品推荐
相关产品推荐

