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

引入分布式Id遇到的坑

作者头像
chenchenchen
发布于 2023-01-30 09:26:09
发布于 2023-01-30 09:26:09
50500
代码可运行
举报
文章被收录于专栏:chenchenchenchenchenchen
运行总次数:0
代码可运行

项目引入分布式Id步骤

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

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<!-- 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
代码运行次数:0
运行
AI代码解释
复制
<!--&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
代码运行次数:0
运行
AI代码解释
复制
<dependency>
    <groupId>com.xxx.cloud</groupId>
    <artifactId>welfare-common-base</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

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

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<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
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
		<!-- 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
代码运行次数:0
运行
AI代码解释
复制
./zkServer.sh status

重启zookeeper,并再次查看状态

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 ./zkServer.sh restart

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

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
./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
代码运行次数:0
运行
AI代码解释
复制
    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
代码运行次数:0
运行
AI代码解释
复制
    //分布式锁路径
    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
代码运行次数:0
运行
AI代码解释
复制
<!-- 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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
深度学习基础 | 超详细逐步图解 Transformer
读完先修知识中的文章之后,你会发现:RNN由于其顺序结构训练速度常常受到限制,既然Attention模型本身可以看到全局的信息, 那么一个自然的疑问是我们能不能去掉RNN结构,仅仅依赖于Attention模型,这样我们可以使训练并行化,同时拥有全局信息?
NewBeeNLP
2021/11/04
1.8K0
大语言模型的预训练[1]:基本概念原理、神经网络的语言模型、Transformer模型原理详解、Bert模型原理介绍
预训练属于迁移学习的范畴。现有的神经网络在进行训练时,一般基于反向传播(Back Propagation,BP)算法,先对网络中的参数进行随机初始化,再利用随机梯度下降(Stochastic Gradient Descent,SGD)等优化算法不断优化模型参数。而预训练的思想是,模型参数不再是随机初始化的,而是通过一些任务进行预先训练,得到一套模型参数,然后用这套参数对模型进行初始化,再进行训练。
汀丶人工智能
2023/07/17
6.5K0
大语言模型的预训练[1]:基本概念原理、神经网络的语言模型、Transformer模型原理详解、Bert模型原理介绍
【调研】详解Transformer结构——Attention Is All You Need
        Transformer是一个完全依赖自注意力的面向sequence to sequence任务的NLP模型,由谷歌大脑在17年的论文《Attention is all you need》中首次提出。它抛弃了传统的CNN和RNN神经网络,整个网络结构完全由Attention机制以及前馈神经网络组成,它解决了RNN长期依赖和无法并行化以及CNN缺失全局特征等问题。(尽管LSTM等门机制的结构一定程度上缓解了长期依赖的问题,但是对于特别长期的依赖现象,LSTM依旧无能为力。)
小锋学长生活大爆炸
2022/10/28
1.3K0
Transformer结构及其应用详解——GPT、BERT、MT-DNN、GPT-2
为了缓解传递间的梯度和遗忘问题,设计了各种各样的RNN cell,最著名的两个就是LSTM和GRU了
新智元
2023/08/05
7400
Transformer结构及其应用详解——GPT、BERT、MT-DNN、GPT-2
NLP笔记——NLP概述
在字符上使用 CNN 或 LSTM 以获得基于字符的词表示的做法现在相当普遍,特别是对于形态信息重要或有许多未知单词的丰富的语言和任务,效果更加明显。据我所知,序列标签使用基于字符的表示(Lample 等人,2016;普兰克等人,2016),可以减轻在计算成本增加的情况下必须处理固定词汇表的需要,并支持完全基于字符的 NMT (Ling 等人, 2016;Lee 等人,2017)。
UM_CC
2022/09/22
7210
【Pre-Training】关于 Transformer 那些的你不知道的事
本博客 主要 是本人在学习 Transformer 时的「所遇、所思、所解」,通过以 「十六连弹」 的方式帮助大家更好的理解 该问题。
阿泽 Crz
2020/07/29
9050
【Pre-Training】关于 Transformer 那些的你不知道的事
Transformer
Encoder-Decoder 通常称作 编码器-解码器,是深度学习中常见的模型框架,很多常见的应用都是利用编码-解码框架设计的,如:
为为为什么
2022/08/06
7100
Transformer
Transformer (Attention is all you need) 详解
在之前的博客中有简单讲到Transformer,这篇博客将会专门详细的一起看看内部的计算与实现。
大鹅
2021/08/26
2.7K1
Transformer的基本原理
Seq2Seq[1]框架最初是在神经机器翻译(Neural Machine Translation,NMT)领域中提出,用于将一种语言(sequence)翻译成另一种语言(sequence)。其结构如下图所示:
felixzhao
2022/09/26
1.2K0
从One-hot, Word embedding到Transformer,一步步教你理解Bert
NLP:自然语言处理(NLP)是信息时代最重要的技术之一。理解复杂的语言也是人工智能的重要组成部分。而自google在2018年10月底公布BERT在11项nlp任务中的卓越表后,BERT(Bidirectional Encoder Representation from Transformers)就成为NLP一枝独秀,本文将为大家层层剖析bert。
AI科技大本营
2019/07/03
1.9K0
从One-hot, Word embedding到Transformer,一步步教你理解Bert
【NLP】关于Transformer,面试官们都怎么问
前些时间,赶完论文,开始对 Transformer、GPT、Bert 系列论文来进行仔仔细细的研读,然后顺手把站内的相关问题整理了一下
yuquanle
2020/03/12
1.5K0
【NLP】关于Transformer,面试官们都怎么问
Transformer各层网络结构详解!面试必备!(附代码实现)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
mantch
2019/09/29
2.7K0
Transformer各层网络结构详解!面试必备!(附代码实现)
通俗讲解从Transformer到BERT模型!
在学会 Transformer 和 Bert 之前,我们需要理解Attention和Self-Attention机制。Attention的本质是要找到输入的feature的权重分布,这个feature在某一个维度有一个长度的概念,如果我们输入一个长为 n 的 feature,那么 Attention 就要学习一个长为 n 的分布权重,这个权重是由相似度计算出来的,最后返回的得分就将会是权重与feature的加权和。
Datawhale
2020/08/28
1.9K0
通俗讲解从Transformer到BERT模型!
细讲 | Attention Is All You Need
自从Attention机制在提出之后,加入Attention的Seq2Seq模型在各个任务上都有了提升,所以现在的seq2seq模型指的都是结合rnn和attention的模型。传统的基于RNN的Seq2Seq模型难以处理长序列的句子,无法实现并行,并且面临对齐的问题。
zenRRan
2018/12/24
4.4K0
细讲 | Attention Is All You Need
【Pre-Training】Transformer:Attention Is All You Need
今天阅读的来自谷歌大脑的同学于 2017 年发表的论文《Attention Is All You Need》,目前论文被引次数高达 6100 次。
阿泽 Crz
2020/07/21
5390
NLP界最强特征提取器--Transformer
前面介绍NLP领域词向量技术的时候提到了目前最炙手可热的语言模型BERT,而BERT之所以能取得这样的瞩目成就,离不开其采用的超强特征提取器Transformer。
流川枫
2020/04/24
5.1K0
NLP之从word2vec到ELMO GPT再到BERT与attention transformer过程笔记与详解
在NLP自然语言处理学习或者发展过程中,在13年word2vec word embedding后,人们发现一个单词通过Word Embedding表示,很容易找到语义相近的单词,但单一词向量表示,不可避免一词多义问题。于是迎来Google的ELMO transformer BERT等动态表征模型,BERT模型更是刷新了GLUE benchmark的11项测试任务最高记录。
大鹅
2021/02/21
3.4K0
​迁移学习在NLP中的演化:从基础到前沿
论文标题:Evolution of Transfer Learning in Natural Language Processing
AI科技评论
2019/10/31
9040
​迁移学习在NLP中的演化:从基础到前沿
自然语言处理中的迁移学习(上)
本文转载自公众号「哈工大SCIR」(微信ID:HIt_SCIR),该公众号为哈尔滨工业大学社会计算与信息检索研究中心(刘挺教授为中心主任)的师生的信息分享平台,本文作者为哈工大SCIR 徐啸。
AI科技评论
2019/10/23
1.4K0
自然语言处理中的迁移学习(上)
干货 | Attention注意力机制超全综述
Attention机制通俗的说,对于某个时刻的输出y,它在输入x上各个部分上的注意力,这里的注意力也就是权重,即输入x的各个部分对某时刻输入y贡献的权重,在此基础上我们先来简单理解一下Transformer模型中提到的self-attention和context-attention
zenRRan
2019/08/05
17.3K0
干货 | Attention注意力机制超全综述
推荐阅读
相关推荐
深度学习基础 | 超详细逐步图解 Transformer
更多 >
LV.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:获取分布式锁失败
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档