Spring Boot 使用 Dubbo 3 报错: org/apache/curator/framework/CuratorFrameworkFactory

场景:Spring Boot 集成 Dubbo 3 并使用 Zookeeper 作为注册中心集成,启动的时候直接报错:

java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory
	at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:70) ~[dubbo-3.0.0.jar:3.0.0]
	at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter.createZookeeperClient(CuratorZookeeperTransporter.java:26) ~[dubbo-3.0.0.jar:3.0.0]
	at org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperTransporter.connect(AbstractZookeeperTransporter.java:71) ~[dubbo-3.0.0.jar:3.0.0]
	at org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter$Adaptive.connect(ZookeeperTransporter$Adaptive.java) ~[dubbo-3.0.0.jar:3.0.0]
	at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfiguration.<init>(ZookeeperDynamicConfiguration.java:68) ~[dubbo-3.0.0.jar:3.0.0]

  ....

后来检查了下依赖,主要原因是 dubbo-spring-boot-starter 依赖中不包含访问 Zookeeper 所需的依赖,所以报错,加入 Dubbo 提供的 Zookeeper 依赖即可,版本号根据项目实际使用更改,保持兼容性(推荐)。

<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-registry-zookeeper -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-registry-zookeeper</artifactId>
    <version>3.0.0</version>
    <scope>test</scope>
</dependency>

推荐使用 dubbo提供的 Zookeeper 依赖,当然也可以通过下面的依赖(不推荐)。

 <dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.3.3</version>
	<scope>test</scope>
</dependency>

 <dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-framework</artifactId>
	<version>2.8.0</version>
</dependency>

<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-recipes</artifactId>
	<version>2.8.0</version>
</dependency>

【注】Dubbo 支持 zkclientcurator 两种 Zookeeper 客户端实现:在 2.7.x 的版本中已经移除了zkclient 的实现,默认使用的是 curator 客户端,所以也需要 curator 的依赖,不然也会报错。如果要使用 zkclient 客户端,需要自己去扩展。

更多关于注册中心的扩展依赖参考:https://github.com/apache/dubbo/tree/3.0/dubbo-registry

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注