Meituan-Dianping / Leaf

Distributed ID Generate Service
Apache License 2.0
6.46k stars 1.84k forks source link

在springboot 2.x上使用报错 #133

Open lly835 opened 4 years ago

lly835 commented 4 years ago

springboot 2.x出了挺久了,应该适配

guohongtao0214 commented 4 years ago

这个需要改一下配置,我们项目中是Spring Boot 2.x ,我改进了一下,可以运行,你可以看看我这个,附带一些源码的解析,不过只限于MySQL版本,https://github.com/guohongtao0214/Leaf

lly835 commented 4 years ago

这个需要改一下配置,我们项目中是Spring Boot 2.x ,我改进了一下,可以运行,你可以看看我这个,附带一些源码的解析,不过只限于MySQL版本,https://github.com/guohongtao0214/Leaf

我等官方leaf升级

lee-0421 commented 4 years ago

具体什么问题呢

lly835 commented 4 years ago

具体什么问题呢

用的SpringBoot 2.3.2.RELEASE, https://github.com/Meituan-Dianping/Leaf/tree/feature/spring-boot-starter/leaf-server commit id 8be4338

        <dependency>
            <artifactId>leaf-boot-starter</artifactId>
            <groupId>com.sankuai.inf.leaf</groupId>
            <version>1.0.1-RELEASE</version>
        </dependency>

项目报错日志

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2026-03-10 15:19:53.493  INFO 1556 --- [           main] c.i.d.DistributedIdApplication           : No active profile set, falling back to default profiles: default
2026-03-10 15:19:54.115  INFO 1556 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2026-03-10 15:19:54.122  INFO 1556 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2026-03-10 15:19:54.123  INFO 1556 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2026-03-10 15:19:54.173  INFO 1556 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2026-03-10 15:19:54.173  INFO 1556 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 648 ms
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2026-03-10 15:19:54.276  WARN 1556 --- [           main] c.a.d.p.v.MySqlValidConnectionChecker    : Cannot resolve com.mysq.jdbc.Connection.ping method.  Will use 'SELECT 1' instead.

java.lang.NullPointerException: null
    at com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker.<init>(MySqlValidConnectionChecker.java:50) ~[druid-1.0.18.jar:1.0.18]
    at com.alibaba.druid.pool.DruidDataSource.initValidConnectionChecker(DruidDataSource.java:944) [druid-1.0.18.jar:1.0.18]
    at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:659) [druid-1.0.18.jar:1.0.18]
    at com.sankuai.inf.leaf.service.SegmentService.<init>(SegmentService.java:31) [leaf-core-1.0.1-RELEASE.jar:na]
    at com.sankuai.inf.leaf.plugin.LeafSpringBootStarterAutoConfigure.initLeafSegmentStarter(LeafSpringBootStarterAutoConfigure.java:28) [leaf-boot-starter-1.0.1-RELEASE.jar:na]
    at com.sankuai.inf.leaf.plugin.LeafSpringBootStarterAutoConfigure$$EnhancerBySpringCGLIB$$9114c2d8.CGLIB$initLeafSegmentStarter$0(<generated>) [leaf-boot-starter-1.0.1-RELEASE.jar:na]
    at com.sankuai.inf.leaf.plugin.LeafSpringBootStarterAutoConfigure$$EnhancerBySpringCGLIB$$9114c2d8$$FastClassBySpringCGLIB$$bb5714b1.invoke(<generated>) [leaf-boot-starter-1.0.1-RELEASE.jar:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) [spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at com.sankuai.inf.leaf.plugin.LeafSpringBootStarterAutoConfigure$$EnhancerBySpringCGLIB$$9114c2d8.initLeafSegmentStarter(<generated>) [leaf-boot-starter-1.0.1-RELEASE.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_251]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_251]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_251]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_251]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:483) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) [spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at com.imooc.distributedid.DistributedIdApplication.main(DistributedIdApplication.java:12) ~[classes/:na]

2026-03-10 15:19:54.284  INFO 1556 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2026-03-10 15:19:54.349  INFO 1556 --- [           main] c.s.inf.leaf.segment.SegmentIDGenImpl    : Init ...
2026-03-10 15:19:54.349  INFO 1556 --- [           main] c.s.inf.leaf.segment.SegmentIDGenImpl    : update cache from db
2026-03-10 15:19:54.729 ERROR 1556 --- [reate-220165304] com.alibaba.druid.pool.DruidDataSource   : create connection error, url: jdbc:mysql://127.0.0.1:3306/leaf, errorCode 0, state null

java.sql.SQLException: validateConnection false
    at com.alibaba.druid.pool.DruidAbstractDataSource.validateConnection(DruidAbstractDataSource.java:1249) ~[druid-1.0.18.jar:1.0.18]
    at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1474) ~[druid-1.0.18.jar:1.0.18]
    at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:1969) ~[druid-1.0.18.jar:1.0.18]

2026-03-10 15:19:54.766 ERROR 1556 --- [reate-220165304] com.alibaba.druid.pool.DruidDataSource   : create connection error, url: jdbc:mysql://127.0.0.1:3306/leaf, errorCode 0, state null

java.sql.SQLException: validateConnection false
    at com.alibaba.druid.pool.DruidAbstractDataSource.validateConnection(DruidAbstractDataSource.java:1249) ~[druid-1.0.18.jar:1.0.18]
    at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1474) ~[druid-1.0.18.jar:1.0.18]
    at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:1969) ~[druid-1.0.18.jar:1.0.18]
nju-Nicko commented 4 years ago

druid版本调整下看看

lucky8875 commented 3 years ago

亲测 主要是spring版本不兼容,spring版本修改为

leaf/pom.xml

<spring.version>5.2.9.RELEASE</spring.version> 

springboot版本为 leaf-server/pom.xml

<spring-boot-dependencies.version>2.3.4.RELEASE</spring-boot-dependencies.version>