分布式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