在通用模块common-base中引入分布式id生成算法
common-base中的pom文件引入id-generator和id-generator-core的maven依赖
<!-- id-generator -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
<scope>provided</scope>
</dependency>
<!-- id-generator-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<!--<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
在需要使用到分布式Id的服务模块的启动类上添加@EnableIdGenerator注解
服务模块中的配置文件中添加配置,不添加则使用默认配置(默认配置不使用zookeeper生成Id,不会有问题)
使用zookeeper配置生成Id启动失败,报错如下:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.
BeanCreationException: Error creating bean with name 'idService' defined in com.xxx.commons.idgenerator.configuration.
AutoConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.
BeanInstantiationException: Failed to instantiate [com.xxx.commons.idgenerator.service.intf.IdService]: Factory method 'idService' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/curator/retry/ExponentialBackoffRetry
翻译如下:
上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory。
BeanCreationException:在com.xxx.commons.idGenerator.configuration中创建名为“idService”的bean时出错。
自动配置:通过工厂方法进行bean实例化失败;嵌套异常为org.springframework.beans。
BeanInstantiationException:未能实例化[com.xxx.commons.idgenerator.service.intf.idservice]:工厂方法“idservice”引发异常;嵌套异常为java.lang.noclassDeffoundError:org/apache/curator/retry/exponentialbackoffRetry
默认配置正常启动,如下
zookeeper配置启动失败,如下
dubug发现在RetryPolicy retryPolicy = new ExponentialBackoffRetry的时候未能创建对象
主要报错如下:
Error creating bean with name 'idService'
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
nested exception is java.lang.NoClassDefFoundError: org/apache/curator/retry/ExponentialBackoffRetry
怀疑是该对象依赖的包未正确引入
检测common-base的pom文件中有该依赖
本地仓库中也有该包
怀疑是引用的服务模块中未能找到该依赖,在服务模块的pom文件中添加该依赖后可以正常启动
服务模块的pom文件中添加通用模块依赖,是通过引入打成jar包的common-base
然后在服务模块中引入
<!--<!–引用父pom下的ms-commons服务–>-->
<dependency>
<groupId>com.xxx.cloud</groupId>
<artifactId>welfare-common-base</artifactId>
<version>${welfare-common-base.version}</version>
<scope>system</scope>
<systemPath>${project.basedir}/../lib/welfare-common-base-0.0.1-SNAPSHOT.jar</systemPath>
</dependency>
如果直接在pom文件中引入common-base可以使用,但不符合要求
<dependency>
<groupId>com.xxx.cloud</groupId>
<artifactId>welfare-common-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
如果将IdGenerator作为一个模块分出来,也没有问题,但不符合要求
需要使用到的服务模块引用使用即可
<dependency>
<groupId>com.xxx.aplus</groupId>
<artifactId>welfare-common-baseId</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
正确引用方式如下:
之前怀疑过是jar包版本问题:
确保包含如下依赖,注意该依赖所依赖的的zookeeper的jar版本需与zookeeper服务器版本匹配,具体版本对应见zookeeper文档或者一个个版本调试一下试试(如果与zookeeper版本匹配问题会报KeeperErrorCode = Unimplemented错误)
Curator 存在版本兼容问题。
Curator 2.x.x-兼容两个zk 3.4.x 和zk 3.5.x,
Curator 3.x.x-兼容兼容zk 3.5。
Versions
The are currently two released versions of Curator, 2.x.x and 3.x.x:
Curator 2.x.x - compatible with both ZooKeeper 3.4.x and ZooKeeper 3.5.x
Curator 3.x.x - compatible only with ZooKeeper 3.5.x and includes support for new features such as dynamic reconfiguration, etc.
也怀疑过是服务模块为编译导致创建bean失败