Motan SPI

2016/12/22

Motan:Maven打包项目运行时找不到注册协议zookeeper问题解决方案

maven-shade-plugin 打包项目

采用maven-shade-plugin插件编译打包motan项目工程,运行jar包会报错,提示:register error! Could not find extension for registry protocol:zookeeper。程序在classpath下找不到zookeeper,Motan的服务注册实现机制,是采用SPI进行扩展和实现的。ZooKeeper就是其中一种服务实现。

maven-assembly-plugin插件存在一个问题,它在对第三方打包时,对于META-INF文件下的同名文件打包时会进行覆盖。

添加如下配置文件即可。 由于Motan META-INF.services里的RegistryFactory 命名相同,导致在maven编译时Zookeeper的接口配置文件被覆盖,所以在运行时报错,找不到zookeeper协议。

<transformer                                   implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/services/com.weibo.api.motan.registry.RegistryFactory</resource>
</transformer>

在这里采用配置文件追加的形式编译,配置就会不被覆盖了。 最后编译出来的jar包里的META-INF/services/com.weibo.api.motan.registry.RegistryFactory就不会被覆盖 com.weibo.api.motan.registry.support.LocalRegistryFactory com.weibo.api.motan.registry.support.DirectRegistryFactory com.weibo.api.motan.registry.zookeeper.ZookeeperRegistryFactory

InitializationFactory

加载所有SPI,位于resources\META-INF\services文件下。

ExtensionLoader

Post Directory