org.apache.dubbo
dubbo
2.7.8
org.apache.zookeeper
zookeeper
3.4.14
com.101tec
zkclient
0.11
org.apache.zookeeper
zookeeper
我部署的zookeeper版本为3.4.14,因此客户端版本也是选用的3.4.14,dubbo选用的是目前新版本。
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.(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.(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.(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连接客户端是行不通的。
如果我们硬是要用zkclient作为zk连接客户端,也是可以的,最简单的办法把dubbo版本降到2.7.0,因为2.7.0版本内置了zkclient的拓展实现,如下:
降低dubbo依赖,重新启动程序,成功连接到zk。
org.apache.dubbo
dubbo
2.7.0
org.apache.zookeeper
zookeeper
3.4.14
com.101tec
zkclient
0.11
org.apache.zookeeper
zookeeper
从上面解决方案截图中可以看看到,在apache dubbo 2.7.0中是有zkclient实现源码的。如果我们要在dubbo最新版本中使用zkclient,可以把代码搬过去,依葫芦画瓢,在类路径下新建META-INF/dubbo目录。然后新建一个文件,文件名为ZookeeperTransporter的全限定类名,最后添加客户端名称和实现类映射,如下:
org.apache.dubbo
dubbo
2.7.8
org.apache.zookeeper
zookeeper
3.4.14
org.apache.curator
curator-recipes
5.1.0
org.apache.zookeeper
zookeeper
curator版本为当前最新版5.1.0,dubbo也是当前最新版本2.7.8。
dubbo新版本默认使用curator作为zk连接客户端,配置文件内容如下:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/zookeeper/admin/ZooKeeperAdmin
at org.apache.curator.framework.CuratorFrameworkFactory.(CuratorFrameworkFactory.java:65)
at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.(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)
一启动就翻车了,报的是org.apache.zookeeper.admin.ZooKeeperAdmin类不存在,想了一想,总觉得是curator依赖有点问题。于是乎跑到curator官网瞧了瞧。
一瞧就发现问题点了,原来高版本的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.(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)
呵呵,又翻车了,这次报的不是org.apache.zookeeper.admin.ZooKeeperAdmin不存在了,而是zk连不上。不是说好的curator 4.2.0版本兼容zk 3.4.x的版本吗,难道是zookeeper版本有问题吗。话不多说,把zk依赖版本降到3.4.12,重新启动程序,结果如下:
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,版本问题都没写清楚。
org.apache.dubbo
dubbo
2.7.8
org.apache.zookeeper
zookeeper
3.4.12
org.apache.curator
curator-recipes
4.2.0
org.apache.zookeeper
zookeeper
新版本dubbo已经不使用zkclient最为zk连接客户端,默认使用curator,这点在dubbo官方文档上没有写清楚,可能是文档没更新的原因,官方文档如下:
当使用curator时,一定要注意引入的curator版本,服务器上部署的zk版本及引入的zk客户端版本需一致,不然会出现缺类或者zk连不上的问题。