分布式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:2181=xxx.xxx.xxx.xxx

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

相关推荐
张彦峰ZYF12 分钟前
投资策略规划最优决策分析
分布式·算法·金融
processflow流程图2 小时前
分布式kettle调度平台v6.4.0新功能介绍
分布式
全栈开发圈3 小时前
干货分享|分布式数据科学工具 Xorbits 的使用
分布式
运维&陈同学5 小时前
【zookeeper01】消息队列与微服务之zookeeper工作原理
运维·分布式·微服务·zookeeper·云原生·架构·消息队列
时差9535 小时前
Flink Standalone集群模式安装部署
大数据·分布式·flink·部署
菠萝咕噜肉i5 小时前
超详细:Redis分布式锁
数据库·redis·分布式·缓存·分布式锁
只因在人海中多看了你一眼8 小时前
分布式缓存 + 数据存储 + 消息队列知识体系
分布式·缓存
zhixingheyi_tian11 小时前
Spark 之 Aggregate
大数据·分布式·spark
求积分不加C12 小时前
-bash: ./kafka-topics.sh: No such file or directory--解决方案
分布式·kafka
nathan052912 小时前
javaer快速上手kafka
分布式·kafka