本文介绍: 在项目需要一个统一的拿单号等唯一ID的服务,就想起了之前用到leaf,但是因为项目要求,leaf版本不符合,需要做一些升级。1,创建目录/tmp/hubleaf/leafconf/8102/ (这个8102就是在上面启动配置中的那个port)1,去掉leafcore项目中pom.xml中的properties配置,统一parent项目中。3,修改leaf项目的leafparent pom.xml文件。2,leafserver项目中改成。因为都是公用的,所以可以直接去掉

背景

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

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

升级点:

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

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

2,leafserver项目中改成

&lt;springbootdependencies.version&gt;2.7.17</springbootdependencies.version&gt;

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

<spring.version&gt;5.3.31</spring.version&gt;
<druid.version&gt;1.2.5</druid.version&gt;
<mysqlconnectorjava.version&gt;8.0.30</mysqlconnectorjava.version&gt;

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

启动配置:

leaf.name=xxx-leaf
leaf.segment.enable=true
leaf.jdbc.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/leaf?autoReconnect=true&amp;allowMultiQueries=true&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF8&amp;serverTimezone=UTC&amp;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

原文地址:https://blog.csdn.net/wangerrong/article/details/134725848

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_24818.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注