在通用模块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,不会有问题)
服务模块的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>
正确引用方式如下:
使用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文件中添加该依赖后可以正常启动
之前怀疑过是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失败
具体报错如下
ERROR [-,,,] --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call the method org.apache.curator.framework.api.CreateBuilder.creatingParentsIfNeeded()Lorg/apache/curator/framework/api/ProtectACLCreateModePathAndBytesable; but it does not exist. Its class, org.apache.curator.framework.api.CreateBuilder, is available from the following locations:
jar:file:.../maven/repository/org/apache/curator/curator-framework/4.0.1/curator-framework-4.0.1.jar!/org/apache/curator/framework/api/CreateBuilder.class
It was loaded from the following location:
file:.../maven/repository/org/apache/curator/curator-framework/4.0.1/curator-framework-4.0.1.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.apache.curator.framework.api.CreateBuilder
Process finished with exit code 1
该目录下确实存在所依赖的包
额外引入curator相关包curator-framework、curator-recipes、curator-client
<!-- id weork -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<!-- id weork -->
1、检查服务器zookeeper状态
./zkServer.sh status
重启zookeeper,并再次查看状态
./zkServer.sh restart
如果部署的是集群,则每台服务都重复以上操作
连接客户端,连接成功后使用命令ls /查看节点
./zkCli.sh -server 127.0.0.1:2181
连接失败则查看配置文件zoo.cof,是否是连接端口错误
检查idea配置文件中配置application.properties的ip端口,重新启动连接即可
连接Zookeeper成功,但是报错Id Service zookeeper 注册id失败:获取分布式锁失败
报错代码如下
private final String groupName = "/snowflake";
InterProcessMutex lock = new InterProcessMutex(cf, groupName);
if (!lock.acquire(30000, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("获取分布式锁失败");
}
使用groupName获取锁失败,查看日志
正确的锁路径应该是/snowflake/lock/xxx
workId路径应该是/snowflake/[workId]/xxx
重新为获取锁创建新路径
//分布式锁路径
private static final String lockName = "/snowflake/lock";
InterProcessMutex lock = new InterProcessMutex(cf, lockName);
if (!lock.acquire(30000, TimeUnit.MILLISECONDS)) {
String mes = "ZookeeperWorkerIdProvider register 获取分布式锁失败";
LogCommons.error(mes);
throw new RuntimeException(mes);
}
再次启动即可
<!-- 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>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>2.12.0</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>
<!--Jackson required包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.2</version>
</dependency>