目录
一、使用zkclient作为zk连接客户端问题
1、Maven依赖如下
2、服务提供者配置文件
3、启动服务提供者
4、原因分析
5、解决办法
(1)降低dubbo版本
(2)添加ZookeeperTransporter拓展实现
二、使用curator作为zk连接客户端版本问题
1、Maven依赖如下
2、服务提供者配置文件
3、启动服务提供者
4、curator作为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、服务提供者配置文件
<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作为注册中心时,默认使用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、启动服务提供者
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(); } }
启动服务提供者后报错了,堆栈信息如下:
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)
Exception in thread "main" java.lang.IllegalStateException: No such extension org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter by name zkclient
看异常信息第一行,提示的是没有名为zkclient的拓展信息,dubbo官方文档有关于拓展点加载和SPI实现的说明,如下:
而根据第一行异常提示,ZookeeperTransporter也是属于拓展点,只是在官方文档上并没有说明。
既然是拓展点,那么根据dubbo官方文档说明在引入的dubbo依赖下的META-INF/dubbo目录肯定会有相关拓展实现类信息,打开duboo依赖目录可以看到很多拓展点信息,internel目录下的都为dubbo自带的,如下:
继续往下拉,看到了我们想要的东西:
从该文件中看出,目前最新的dubbo 2.7.8版本ZookeeperTransporter的实现只有curator,也就是说目前我们使用zkclient作为zk连接客户端是行不通的。
5、解决办法
(1)降低dubbo版本
如果我们硬是要用zkclient作为zk连接客户端,也是可以的,最简单的办法把dubbo版本降到2.7.0,因为2.7.0版本内置了zkclient的拓展实现,如下:
降低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拓展实现
从上面解决方案截图中可以看看到,在apache dubbo 2.7.0中是有zkclient实现源码的。如果我们要在dubbo最新版本中使用zkclient,可以把代码搬过去,依葫芦画瓢,在类路径下新建META-INF/dubbo目录。然后新建一个文件,文件名为ZookeeperTransporter的全限定类名,最后添加客户端名称和实现类映射,如下:
二、使用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版本为当前最新版5.1.0,dubbo也是当前最新版本2.7.8。
2、服务提供者配置文件
dubbo新版本默认使用curator作为zk连接客户端,配置文件内容如下:
<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作为注册中心时,默认使用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、启动服务提供者
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)
一瞧就发现问题点了,原来高版本的curator不兼容zk 3.4.x的版本,如果要兼容需将curator版本降低至4.2.0,改完依赖后继续启动。
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)
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
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目前版本默认使用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>
三、总结
新版本dubbo已经不使用zkclient最为zk连接客户端,默认使用curator,这点在dubbo官方文档上没有写清楚,可能是文档没更新的原因,官方文档如下:
当使用curator时,一定要注意引入的curator版本,服务器上部署的zk版本及引入的zk客户端版本需一致,不然会出现缺类或者zk连不上的问题。