Dubbo新版本zk注冊中心連接問題及解決
一、使用zkclient作為zk連接客戶端問題
1、Maven依賴如下
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.8</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.11</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency>
我部署的zookeeper版本為3.4.14,因此客戶端版本也是選用的3.4.14,dubbo選用的是目前新版本。
2、服務(wù)提供者配置文件
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <dubbo:application name="dubbo-demo-provider"/> <!--使用zk作為注冊中心時(shí),默認(rèn)使用curator作為客戶端--> <dubbo:registry address="zookeeper://localhost:2181" client="zkclient"/> <dubbo:protocol name="dubbo" port="20890"/> <dubbo:service interface="com.netease.dubbo.service.DubboDemoService" ref="dubboDemoService"/> <bean id="dubboDemoService" class="com.netease.dubbo.service.impl.DubboDemoServiceImpl"/> </beans>
3、啟動服務(wù)提供者
public class XmlDubboProvider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo/dubbo-provider.xml"); context.start(); System.in.read(); } }
啟動服務(wù)提供者后報(bào)錯了,堆棧信息如下:
Exception in thread "main" java.lang.IllegalStateException: No such extension org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter by name zkclient
at org.apache.dubbo.common.extension.ExtensionLoader.findException(ExtensionLoader.java:623)
at org.apache.dubbo.common.extension.ExtensionLoader.createExtension(ExtensionLoader.java:630)
at org.apache.dubbo.common.extension.ExtensionLoader.getExtension(ExtensionLoader.java:429)
at org.apache.dubbo.common.extension.ExtensionLoader.getExtension(ExtensionLoader.java:413)
at org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter$Adaptive.connect(ZookeeperTransporter$Adaptive.java)
at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfiguration.<init>(ZookeeperDynamicConfiguration.java:56)
at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfigurationFactory.createDynamicConfiguration(ZookeeperDynamicConfigurationFactory.java:37)
at org.apache.dubbo.common.config.configcenter.AbstractDynamicConfigurationFactory.lambda$getDynamicConfiguration$0(AbstractDynamicConfigurationFactory.java:39)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
at org.apache.dubbo.common.config.configcenter.AbstractDynamicConfigurationFactory.getDynamicConfiguration(AbstractDynamicConfigurationFactory.java:39)
at org.apache.dubbo.common.config.configcenter.DynamicConfiguration.getDynamicConfiguration(DynamicConfiguration.java:224)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.prepareEnvironment(DubboBootstrap.java:1034)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.startConfigCenter(DubboBootstrap.java:623)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.initialize(DubboBootstrap.java:521)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.start(DubboBootstrap.java:896)
at org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener.onContextRefreshedEvent(DubboBootstrapApplicationListener.java:59)
at org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener.onApplicationContextEvent(DubboBootstrapApplicationListener.java:52)
at org.apache.dubbo.config.spring.context.OneTimeExecutionApplicationContextEventListener.onApplicationEvent(OneTimeExecutionApplicationContextEventListener.java:40)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.netease.dubbo.provider.XmlDubboProvider.main(XmlDubboProvider.java:14)
4、原因分析
Exception in thread "main" java.lang.IllegalStateException: No such extension org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter by name zkclient
看異常信息第一行,提示的是沒有名為zkclient的拓展信息,dubbo官方文檔有關(guān)于拓展點(diǎn)加載和SPI實(shí)現(xiàn)的說明,如下:
而根據(jù)第一行異常提示,ZookeeperTransporter也是屬于拓展點(diǎn),只是在官方文檔上并沒有說明。
既然是拓展點(diǎn),那么根據(jù)dubbo官方文檔說明在引入的dubbo依賴下的META-INF/dubbo目錄肯定會有相關(guān)拓展實(shí)現(xiàn)類信息,打開duboo依賴目錄可以看到很多拓展點(diǎn)信息,internel目錄下的都為dubbo自帶的,如下:
繼續(xù)往下拉,看到了我們想要的東西:
從該文件中看出,目前最新的dubbo 2.7.8版本ZookeeperTransporter的實(shí)現(xiàn)只有curator,也就是說目前我們使用zkclient作為zk連接客戶端是行不通的。
5、解決辦法
(1)降低dubbo版本
如果我們硬是要用zkclient作為zk連接客戶端,也是可以的,最簡單的辦法把dubbo版本降到2.7.0,因?yàn)?.7.0版本內(nèi)置了zkclient的拓展實(shí)現(xiàn),如下:
降低dubbo依賴,重新啟動程序,成功連接到zk。
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.11</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency>
(2)添加ZookeeperTransporter拓展實(shí)現(xiàn)
從上面解決方案截圖中可以看看到,在apache dubbo 2.7.0中是有zkclient實(shí)現(xiàn)源碼的。如果我們要在dubbo最新版本中使用zkclient,可以把代碼搬過去,依葫蘆畫瓢,在類路徑下新建META-INF/dubbo目錄。
然后新建一個文件,文件名為ZookeeperTransporter的全限定類名,最后添加客戶端名稱和實(shí)現(xiàn)類映射,如下:
二、使用curator作為zk連接客戶端版本問題
1、Maven依賴如下
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.8</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>5.1.0</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency>
curator版本為當(dāng)前最新版5.1.0,dubbo也是當(dāng)前最新版本2.7.8。
2、服務(wù)提供者配置文件
dubbo新版本默認(rèn)使用curator作為zk連接客戶端,配置文件內(nèi)容如下:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <dubbo:application name="dubbo-demo-provider"/> <!--使用zk作為注冊中心時(shí),默認(rèn)使用curator作為客戶端,若要使用zkclient需自行拓展--> <dubbo:registry address="zookeeper://localhost:2181" client="curator"/> <dubbo:protocol name="dubbo" port="20890"/> <dubbo:service interface="com.netease.dubbo.service.DubboDemoService" ref="dubboDemoService"/> <bean id="dubboDemoService" class="com.netease.dubbo.service.impl.DubboDemoServiceImpl"/> </beans>
3、啟動服務(wù)提供者
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/zookeeper/admin/ZooKeeperAdmin
at org.apache.curator.framework.CuratorFrameworkFactory.<clinit>(CuratorFrameworkFactory.java:65)
at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:66)
at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter.createZookeeperClient(CuratorZookeeperTransporter.java:26)
at org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperTransporter.connect(AbstractZookeeperTransporter.java:70)
一啟動就翻車了,報(bào)的是org.apache.zookeeper.admin.ZooKeeperAdmin類不存在,想了一想,總覺得是curator依賴有點(diǎn)問題。于是乎跑到curator官網(wǎng)瞧了瞧。
一瞧就發(fā)現(xiàn)問題點(diǎn)了,原來高版本的curator不兼容zk 3.4.x的版本,如果要兼容需將curator版本降低至4.2.0,改完依賴后繼續(xù)啟動。
Exception in thread "main" java.lang.IllegalStateException: zookeeper not connected
at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:83)
at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter.createZookeeperClient(CuratorZookeeperTransporter.java:26)
at org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperTransporter.connect(AbstractZookeeperTransporter.java:70)
呵呵,又翻車了,這次報(bào)的不是org.apache.zookeeper.admin.ZooKeeperAdmin不存在了,而是zk連不上。
不是說好的curator 4.2.0版本兼容zk 3.4.x的版本嗎,難道是zookeeper版本有問題嗎。
話不多說,把zk依賴版本降到3.4.12,重新啟動程序,結(jié)果如下:
2020-10-23 23:05:52.959 INFO [main-SendThread(localhsot:2181)] [org.apache.zookeeper.ClientCnxn] - Opening socket connection to server localhsot/localhsot:2181. Will not attempt to authenticate using SASL (unknown error)
2020-10-23 23:05:52.961 INFO [main] [org.apache.curator.framework.imps.CuratorFrameworkImpl] - Default schema
2020-10-23 23:05:52.992 INFO [main-SendThread(localhsot:2181)] [org.apache.zookeeper.ClientCnxn] - Socket connection established to localhsot/localhsot:2181, initiating session
2020-10-23 23:05:53.084 INFO [main-SendThread(localhsot:2181)] [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server localhsot/localhsot:2181, sessionid = 0x10634c6160d017d, negotiated timeout = 40000
2020-10-23 23:05:53.093 INFO [main-EventThread] [org.apache.curator.framework.state.ConnectionStateManager] - State change: CONNECTED
終于可以了,看來官方文檔也是坑多多,無論是dubbo還是curator,版本問題都沒寫清楚。
4、curator作為zk連接客戶端可行依賴
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.8</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.12</version> </dependency> <!--dubbo目前版本默認(rèn)使用curator作為客戶端,curator4.2.0版本剛好兼容zk 3.4.12及以下版本--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency>
三、總結(jié)
新版本dubbo已經(jīng)不使用zkclient最為zk連接客戶端,默認(rèn)使用curator,這點(diǎn)在dubbo官方文檔上沒有寫清楚,可能是文檔沒更新的原因,官方文檔如下:
當(dāng)使用curator時(shí),一定要注意引入的curator版本,服務(wù)器上部署的zk版本及引入的zk客戶端版本需一致,不然會出現(xiàn)缺類或者zk連不上的問題。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java?-jar命令詳解之運(yùn)行JAR文件、傳遞參數(shù)與性能調(diào)優(yōu)
這篇文章主要介紹了java?-jar命令詳解之運(yùn)行JAR文件、傳遞參數(shù)與性能調(diào)優(yōu)的相關(guān)資料,java?-jar命令用于運(yùn)行可執(zhí)行的JAR文件,它解析JAR文件中的META-INF/MANIFEST.MF文件來確定主類,并執(zhí)行該類的?main方法,運(yùn)行時(shí)可通過參數(shù)傳遞給主類,需要的朋友可以參考下2025-04-04Springboot整合Flowable6.x導(dǎo)出bpmn20的步驟詳解
這篇文章主要介紹了Springboot整合Flowable6.x導(dǎo)出bpmn20,Flowable流程引擎可用于部署B(yǎng)PMN 2.0流程定義,可以十分靈活地加入你的應(yīng)用/服務(wù)/構(gòu)架,本文給出兩種從flowable導(dǎo)出流程定義bpmn20.xml的方式,需要的朋友可以參考下2023-04-04Java面試題沖刺第十二天--數(shù)據(jù)庫(2)
這篇文章主要為大家分享了最有價(jià)值的三道數(shù)據(jù)庫面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-07-07Java實(shí)現(xiàn)微信公眾號獲取臨時(shí)二維碼功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)微信公眾號獲取臨時(shí)二維碼功能,結(jié)合實(shí)例形式分析了java調(diào)用微信公眾號接口實(shí)現(xiàn)臨時(shí)二維碼生成功能相關(guān)操作技巧,需要的朋友可以參考下2019-10-10Spring AOP如何自定義注解實(shí)現(xiàn)審計(jì)或日志記錄(完整代碼)
這篇文章主要介紹了Spring AOP如何自定義注解實(shí)現(xiàn)審計(jì)或日志記錄(完整代碼),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Java動態(tài)數(shù)組Arraylist存放自定義數(shù)據(jù)類型方式
這篇文章主要介紹了Java動態(tài)數(shù)組Arraylist存放自定義數(shù)據(jù)類型方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10