一、背景
1、环境
开发工具 idea 2019
gradle 5.0
2、微服务搭建过程中,使用gradle作为项目自动化构建工具,之前一直用maven,换过来后还是有点不适应,但是可以接受,言归正传,选择好了springboot的version和springcloud的version,然后开始搭建注册中心,一切顺利,然后搭建一个服务,要注册到注册中心,启动的时候问题来了,解决了有半天时间,穷尽我的所学,还是一无所获,一头雾水,一脸懵逼,启动异常如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.eurekaClient' defined in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.netflix.discovery.EurekaClient]: Factory method 'eurekaClient' threw exception; nested exception is java.lang.NoSuchMethodError: com.google.common.collect.MapMaker.keyEquivalence(Lcom/google/common/base/Equivalence;)Lcom/google/common/collect/MapMaker;
Caused by: java.lang.NoSuchMethodError: com.google.common.collect.MapMaker.keyEquivalence(Lcom/google/common/base/Equivalence;)Lcom/google/common/collect/MapMaker;
二、解决方法
1、提示很明显,找不到keyEquivalence这个方法,按照一般的解决套路,先去External Libraries中找这个jar包,看到两个jar包,
guava-19.0.jar(groupId:com.google.collections)
google-collections-1.0.jar(com.google)
有意思的是根据包名+类名com.google.common.collect.MapMaker找到以上俩jar包都存在该类,但是guava中没有keyEquivalence这个方法,但是诡异的是我的build.gradle中并没有增加这两个jar的依赖,难道是其他的依赖导致?
2、是时候发挥gradle的优势了
1)展开gradle
找到 task ----> help---->dependencies,然后双击运行查看当前项目的jar包依赖,当然,这是一种run模式,
另一种方式是在Terminal下面,执行gradle denpendencies > D:\denpendencies.txt 这样就可以输出依赖关系到文本,
2)分析denpendencies(篇幅较长,但是很有必要)
| +--- org.springframework.cloud:spring-cloud-netflix-eureka-server:2.0.2.RELEASE | | +--- org.springframework.boot:spring-boot-starter-web:2.0.6.RELEASE | | | +--- org.springframework.boot:spring-boot-starter:2.0.6.RELEASE (*) | | | +--- org.springframework.boot:spring-boot-starter-json:2.0.6.RELEASE | | | | +--- org.springframework.boot:spring-boot-starter:2.0.6.RELEASE (*) | | | | +--- org.springframework:spring-web:5.0.10.RELEASE | | | | | +--- org.springframework:spring-beans:5.0.10.RELEASE (*) | | | | | \--- org.springframework:spring-core:5.0.10.RELEASE (*) | | | | +--- com.fasterxml.jackson.core:jackson-databind:2.9.7 | | | | | +--- com.fasterxml.jackson.core:jackson-annotations:2.9.0 | | | | | \--- com.fasterxml.jackson.core:jackson-core:2.9.7 | | | | +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.7 | | | | | +--- com.fasterxml.jackson.core:jackson-core:2.9.7 | | | | | \--- com.fasterxml.jackson.core:jackson-databind:2.9.7 (*) | | | | +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.7 | | | | | +--- com.fasterxml.jackson.core:jackson-annotations:2.9.0 | | | | | +--- com.fasterxml.jackson.core:jackson-core:2.9.7 | | | | | \--- com.fasterxml.jackson.core:jackson-databind:2.9.7 (*) | | | | \--- com.fasterxml.jackson.module:jackson-module-parameter-names:2.9.7 | | | | +--- com.fasterxml.jackson.core:jackson-core:2.9.7 | | | | \--- com.fasterxml.jackson.core:jackson-databind:2.9.7 (*) | | | +--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE | | | | +--- javax.annotation:javax.annotation-api:1.3.2 | | | | +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34 | | | | +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34 | | | | \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34 | | | | \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34 | | | +--- org.hibernate.validator:hibernate-validator:6.0.13.Final | | | | +--- javax.validation:validation-api:2.0.1.Final | | | | +--- org.jboss.logging:jboss-logging:3.3.2.Final | | | | \--- com.fasterxml:classmate:1.3.4 | | | +--- org.springframework:spring-web:5.0.10.RELEASE (*) | | | \--- org.springframework:spring-webmvc:5.0.10.RELEASE | | | +--- org.springframework:spring-aop:5.0.10.RELEASE (*) | | | +--- org.springframework:spring-beans:5.0.10.RELEASE (*) | | | +--- org.springframework:spring-context:5.0.10.RELEASE (*) | | | +--- org.springframework:spring-core:5.0.10.RELEASE (*) | | | +--- org.springframework:spring-expression:5.0.10.RELEASE (*) | | | \--- org.springframework:spring-web:5.0.10.RELEASE (*) | | +--- org.springframework.boot:spring-boot-starter-actuator:2.0.6.RELEASE | | | +--- org.springframework.boot:spring-boot-starter:2.0.6.RELEASE (*) | | | +--- org.springframework.boot:spring-boot-actuator-autoconfigure:2.0.6.RELEASE | | | | +--- org.springframework.boot:spring-boot-actuator:2.0.6.RELEASE | | | | | \--- org.springframework.boot:spring-boot:2.0.6.RELEASE (*) | | | | +--- org.springframework.boot:spring-boot-autoconfigure:2.0.6.RELEASE (*) | | | | +--- com.fasterxml.jackson.core:jackson-databind:2.9.7 (*) | | | | +--- org.springframework:spring-core:5.0.10.RELEASE (*) | | | | +--- org.springframework:spring-context:5.0.10.RELEASE (*) | | | | \--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.7 (*) | | | \--- io.micrometer:micrometer-core:1.0.7 | | | +--- org.hdrhistogram:HdrHistogram:2.1.10 | | | \--- org.latencyutils:LatencyUtils:2.0.3 | | +--- org.springframework.boot:spring-boot-starter-freemarker:2.0.6.RELEASE | | | +--- org.springframework.boot:spring-boot-starter:2.0.6.RELEASE (*) | | | +--- org.freemarker:freemarker:2.3.28 | | | \--- org.springframework:spring-context-support:5.0.10.RELEASE | | | +--- org.springframework:spring-beans:5.0.10.RELEASE (*) | | | +--- org.springframework:spring-context:5.0.10.RELEASE (*) | | | \--- org.springframework:spring-core:5.0.10.RELEASE (*) | | +--- org.springframework.cloud:spring-cloud-commons:2.0.2.RELEASE (*) | | +--- org.springframework.cloud:spring-cloud-netflix-core:2.0.2.RELEASE | | | +--- org.springframework.boot:spring-boot-autoconfigure:2.0.6.RELEASE (*) | | | \--- org.springframework.boot:spring-boot-starter-aop:2.0.6.RELEASE | | | +--- org.springframework.boot:spring-boot-starter:2.0.6.RELEASE (*) | | | +--- org.springframework:spring-aop:5.0.10.RELEASE (*) | | | \--- org.aspectj:aspectjweaver:1.8.13 | | +--- org.springframework.cloud:spring-cloud-netflix-eureka-client:2.0.2.RELEASE | | | \--- org.springframework.cloud:spring-cloud-netflix-core:2.0.2.RELEASE (*) | | +--- com.netflix.eureka:eureka-client:1.9.3 | | | +--- org.codehaus.jettison:jettison:1.3.7 | | | | \--- stax:stax-api:1.0.1 | | | +--- com.netflix.netflix-commons:netflix-eventbus:0.3.0 | | | | +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.25 | | | | +--- com.netflix.netflix-commons:netflix-infix:0.3.0 | | | | | +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.25 | | | | | +--- commons-jxpath:commons-jxpath:1.3 | | | | | +--- joda-time:joda-time:2.3 -> 2.9.9 | | | | | +--- org.antlr:antlr-runtime:3.4 | | | | | | +--- org.antlr:stringtemplate:3.2.1 | | | | | | | \--- antlr:antlr:2.7.7 | | | | | | \--- antlr:antlr:2.7.7 | | | | | +--- com.google.code.findbugs:jsr305:3.0.1| | | | | +--- com.google.guava:guava:14.0.1 -> 19.0 | | | | | \--- com.google.code.gson:gson:2.1 -> 2.8.5
没有看懂?看主线
| +--- org.springframework.cloud:spring-cloud-netflix-eureka-server:2.0.2.RELEASE | | +--- com.netflix.eureka:eureka-client:1.9.3 | | | +--- org.codehaus.jettison:jettison:1.3.7 | | | +--- com.netflix.netflix-commons:netflix-eventbus:0.3.0 | | | | +--- com.netflix.netflix-commons:netflix-infix:0.3.0 | | | | | +--- com.google.guava:guava:14.0.1 -> 19.0
突然发现问题了,eureka-server的版本是2.0.2.RELEASE,下面com.google.guava的版本从14.0.1自动升级到了19.0,然后到maven仓库看看依赖(https://mvnrepository.com/artifact/com.google.guava/guava),其实这时候应该可以看出来是怎么回事了,eureka的版本决定了guava的版本,这两个相对版本造成了guava的19.0这个版本中没有com.google.common.collect.MapMaker.keyEquivalence,然后点开23.0这个版本发现,居然有keyEquivalence!!!!!
果断在build.gradle中增加升级依赖
// https://mvnrepository.com/artifact/com.google.guava/guava compile group: 'com.google.guava', name: 'guava', version: '23.0'重启服务,完美
2019-05-22 15:46:37.477 INFO 5900 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application dap-autodiagnose with eureka with status UP 2019-05-22 15:46:37.477 INFO 5900 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1558511197477, current=UP, previous=STARTING] 2019-05-22 15:46:37.479 INFO 5900 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_DAP-AUTODIAGNOSE/MHQSH-L0897.paicdom.local:dap-autodiagnose:8900: registering service... 2019-05-22 15:46:37.499 INFO 5900 --- [ main] o.a.coyote.http11.Http11NioProtocol : Starting ProtocolHandler ["http-nio-8900"] 2019-05-22 15:46:37.515 INFO 5900 --- [ main] o.a.tomcat.util.net.NioSelectorPool : Using a shared selector for servlet write/read 2019-05-22 15:46:37.541 INFO 5900 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8900 (http) with context path '' 2019-05-22 15:46:37.543 INFO 5900 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8900 2019-05-22 15:46:37.546 INFO 5900 --- [ main] c.p.r.a.DapAutodiagnoseApplication : Started DapAutodiagnoseApplication in 7.661 seconds (JVM running for 8.775)
三、总结
1、坑有哪些
1)网上有一些说法让删除google的jar包,请问springcloud本身的依赖包你怎么删除,不负责任!
2)清理缓存,重写build。。。。。。。。。。。不想多说
2、说明
我的工程里面用的版本是这个,切记springboot和springcloud的版本是有兼容性的,选对合适的版本很重要
springBootVersion = '2.0.6.RELEASE'
springCloudVersion ='Finchley.RELEASE'
#### 统一版本很重要 Spring Cloud 版本与 Spring Boot 版本必须对应,不然会出现各种兼容问题。
* Spring Boot -- 2.0.6.RELEASE * Spring Cloud -- Finchley.SR2
版本对应关系查询:http://spring.io/projects/spring-cloud#overview
Spring Boot版本列表查询:https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent
Spring Cloud版本列表查询:https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies