当前位置: 首页 > article >正文

分布式ID生成框架Leaf升级踩坑

背景:

在项目中需要一个统一的拿单号等唯一ID的服务,就想起了之前用到的leaf,但是因为项目要求,leaf的版本不符合,需要做一些升级

项目地址:https://github.com/Meituan-Dianping/Leaf

升级点:

1,去掉leaf-core项目中pom.xml中的properties配置,统一到parent项目中。

因为都是公用的,所以可以直接去掉。

2,leaf-server项目中改成

<spring-boot-dependencies.version>2.7.17</spring-boot-dependencies.version>

3,修改leaf项目的leaf-parent pom.xml文件

<spring.version>5.3.31</spring.version>
<druid.version>1.2.5</druid.version>
<mysql-connector-java.version>8.0.30</mysql-connector-java.version>

完成上面3个修改点,就可以了

启动配置:

leaf.name=xxx-leaf
leaf.segment.enable=true
leaf.jdbc.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/leaf?autoReconnect=true&allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC&useLocalSessionState=true
leaf.jdbc.username=root
leaf.jdbc.password=xxxxxx

leaf.snowflake.enable=true
leaf.snowflake.zk.address=xxx.xxx.xxx.xxx:2181
leaf.snowflake.port=8102

遇到的问题:

2023-11-30 17:15:41.406 ERROR 31449 --- [           main] c.s.i.l.s.SnowflakeZookeeperHolder       : Start node ERROR {}
java.lang.NumberFormatException: For input string: "leaf/forever/172.20.0.1:8102"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_382]
        at java.lang.Integer.parseInt(Integer.java:580) ~[na:1.8.0_382]
        at java.lang.Integer.parseInt(Integer.java:615) ~[na:1.8.0_382]
        at com.sankuai.inf.leaf.snowflake.SnowflakeZookeeperHolder.init(SnowflakeZookeeperHolder.java:89) ~[leaf-core-1.0.1.jar!/:na]
        at com.sankuai.inf.leaf.snowflake.SnowflakeIDGenImpl.<init>(SnowflakeIDGenImpl.java:50) [leaf-core-1.0.1.jar!/:na]
        at com.sankuai.inf.leaf.snowflake.SnowflakeIDGenImpl.<init>(SnowflakeIDGenImpl.java:36) [leaf-core-1.0.1.jar!/:na]
        at com.sankuai.inf.leaf.server.service.SnowflakeService.<init>(SnowflakeService.java:28) [classes!/:na]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [na:1.8.0_382]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) [na:1.8.0_382]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [na:1.8.0_382]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) [na:1.8.0_382]
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:213) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1326) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1232) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) ~[spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:710) ~[spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:693) ~[spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:408) ~[spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) [spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.31.jar!/:5.3.31]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:929) ~[spring-context-5.3.31.jar!/:5.3.31]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:591) ~[spring-context-5.3.31.jar!/:5.3.31]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.17.jar!/:2.7.17]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-2.7.17.jar!/:2.7.17]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409) ~[spring-boot-2.7.17.jar!/:2.7.17]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.17.jar!/:2.7.17]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.7.17.jar!/:2.7.17]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289) ~[spring-boot-2.7.17.jar!/:2.7.17]
        at com.sankuai.inf.leaf.server.LeafServerApplication.main(LeafServerApplication.java:10) ~[classes!/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_382]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_382]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_382]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_382]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[leaf.jar:na]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[leaf.jar:na]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[leaf.jar:na]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) ~[leaf.jar:na]

2023-11-30 17:15:41.407 ERROR 31449 --- [           main] c.s.i.l.s.SnowflakeZookeeperHolder       : Read file error

java.io.FileNotFoundException: /tmp/xxx-leaf/leafconf/8102/workerID.properties (No such file or directory)

解决方法:

1,创建目录/tmp/hub-leaf/leafconf/8102/  (这个8102就是在上面启动配置中的那个port)

2,  创建文件workerID.properties, 内容为workerID=1

再去启动就可以了。

代码地址:

https://download.csdn.net/download/wangerrong/88585488

测试:

1,拿整数ID单号:http://localhost:8101/api/segment/get/order_no_seq  (order_no_seq是leaf_alloc表中新增的biz_tag)

2,拿雪花算法的ID单号: http://localhost:8101/api/snowflake/get/test  (test是可以自己改的,一般是业务场景需要的,比如order)

3,查看单号缓存的监控:http://localhost:8101/cache


http://www.kler.cn/a/154377.html

相关文章:

  • GIT将源码推送新分支
  • 从零入门激光SLAM(二十三)——direct_visual_lidar_calibration全型号激光雷达-相机标定包
  • Java:从诞生到未来的编程语言进化史与应用前景
  • git简介和本地仓库创建,并提交修改。git config init status add commit
  • 《C语言程序设计现代方法》note-5 数组
  • ThinkPHP 模型如何更新数据
  • vue 前端实现login页登陆 验证码
  • Vue3-pnpm包管理器创建项目
  • Python库Selenium自动化使用详细教程
  • 洛谷 P6704 [COCI2010-2011#7] GITARA
  • 错误处理(9)
  • ASP.NET版本WOL服务的使用
  • 算法通关村第三关—数组基本操作(青铜)
  • 05:2440----代码重定义
  • VMware下载安装教程
  • Python 3 使用 read()、readline()、readlines() 函数 读取文件
  • Gateway网关--java
  • ahk热字串:字符串输入后,按空格后,打开网址 or 不按空格直接打开网址
  • 多项式拟合求解
  • 每日一题(LeetCode)----哈希表--三数之和
  • 播放器开发(五):视频帧处理并用SDL渲染播放
  • 什么时候适合做ui自动化测试?什么时候做接口自动化测试
  • UE小计:Unreal Engine 中 Actor 数据的 JSON 序列化
  • 【el-form】表单label添加?及tooltip
  • C 标准库 <math.h>
  • centos7 yum安装nginx