前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >引入分布式Id遇到的坑

引入分布式Id遇到的坑

作者头像
chenchenchen
发布2023-01-30 17:26:09
4510
发布2023-01-30 17:26:09
举报
文章被收录于专栏:chenchenchen

项目引入分布式Id步骤

在通用模块common-base中引入分布式id生成算法

common-base中的pom文件引入id-generator和id-generator-core的maven依赖

代码语言:javascript
复制
<!-- 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

然后在服务模块中引入

代码语言:javascript
复制
<!--&lt;!&ndash;引用父pom下的ms-commons服务&ndash;&gt;-->
<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可以使用,但不符合要求

代码语言:javascript
复制
<dependency>
    <groupId>com.xxx.cloud</groupId>
    <artifactId>welfare-common-base</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

如果将IdGenerator作为一个模块分出来,也没有问题,但不符合要求

需要使用到的服务模块引用使用即可

代码语言:javascript
复制
<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>

 正确引用方式如下:

Error creating bean,Failed to instantiate,NoClassDefFoundError

使用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。

代码语言:javascript
复制
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失败

An attempt was made to call the method CreateBuilder.creatingParentsIfNeeded(); but it does not exist

具体报错如下

代码语言:javascript
复制
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

代码语言:javascript
复制
		<!-- 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 -->

zookeeper拒绝连接

1、检查服务器zookeeper状态

代码语言:javascript
复制
./zkServer.sh status

重启zookeeper,并再次查看状态

代码语言:javascript
复制
 ./zkServer.sh restart

如果部署的是集群,则每台服务都重复以上操作

连接客户端,连接成功后使用命令ls /查看节点

代码语言:javascript
复制
./zkCli.sh -server 127.0.0.1:2181

连接失败则查看配置文件zoo.cof,是否是连接端口错误

检查idea配置文件中配置application.properties的ip端口,重新启动连接即可

Caused by: java.lang.RuntimeException:获取分布式锁失败

连接Zookeeper成功,但是报错Id Service zookeeper 注册id失败:获取分布式锁失败

报错代码如下

代码语言:javascript
复制
    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

重新为获取锁创建新路径

代码语言:javascript
复制
    //分布式锁路径
    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);
                }

再次启动即可

代码语言:javascript
复制
<!-- 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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-08-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 项目引入分布式Id步骤
  • Error creating bean,Failed to instantiate,NoClassDefFoundError
  • An attempt was made to call the method CreateBuilder.creatingParentsIfNeeded(); but it does not exist
  • zookeeper拒绝连接
  • Caused by: java.lang.RuntimeException:获取分布式锁失败
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档