搭建SpringCloud项目,在添加断路器时,出现了如下错误
nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist
完整报错:
2019-09-27 14:23:15.959 ERROR 27177 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [cn.com.agree.scEureka_303.ScEureka303Application]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:596) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.access$900(ConfigurationClassParser.java:108) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.lambda$processGroupImports$1(ConfigurationClassParser.java:805) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_211]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:801) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:771) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:185) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.1.9.BUILD-SNAPSHOT.jar:2.1.9.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) [spring-boot-2.1.9.BUILD-SNAPSHOT.jar:2.1.9.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) [spring-boot-2.1.9.BUILD-SNAPSHOT.jar:2.1.9.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.9.BUILD-SNAPSHOT.jar:2.1.9.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.1.9.BUILD-SNAPSHOT.jar:2.1.9.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) [spring-boot-2.1.9.BUILD-SNAPSHOT.jar:2.1.9.BUILD-SNAPSHOT]
at cn.com.agree.scEureka_303.ScEureka303Application.main(ScEureka303Application.java:17) [classes/:na]
Caused by: java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180) ~[spring-core-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.core.type.classreading.SimpleMetadataReader.(SimpleMetadataReader.java:51) ~[spring-core-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) ~[spring-core-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:86) ~[spring-boot-2.1.9.BUILD-SNAPSHOT.jar:2.1.9.BUILD-SNAPSHOT]
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:73) ~[spring-boot-2.1.9.BUILD-SNAPSHOT.jar:2.1.9.BUILD-SNAPSHOT]
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81) ~[spring-core-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:682) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getRelated(ConfigurationClassParser.java:1074) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getAnnotationAttributes(ConfigurationClassParser.java:1055) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.collectImports(ConfigurationClassParser.java:538) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:511) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:302) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:586) ~[spring-context-5.1.10.BUILD-SNAPSHOT.jar:5.1.10.BUILD-SNAPSHOT]
... 19 common frames omitted
使用的各个版本如下:
springboot 2.1.9.BUILD-SNAPSHOT'
spring-cloud-starter-netflix-eureka-server', version: '2.1.2.RELEASE'
spring-cloud-starter-eureka-server', version: '1.4.2.RELEASE'
spring-cloud-dependencies', version: 'Greenwich.BUILD-SNAPSHOT'
'spring-cloud-starter-ribbon', version: '1.4.7.RELEASE'
'spring-cloud-starter-hystrix', version: '1.4.7.RELEASE'
build.gradle的完整文件为:
plugins {
id 'org.springframework.boot' version '2.1.9.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'cn.com.agree'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-server', version: '2.1.2.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka-server', version: '1.4.2.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-dependencies', version: 'Greenwich.BUILD-SNAPSHOT', ext: 'pom'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-ribbon', version: '1.4.7.RELEASE'
// compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix', version: '2.1.0.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-hystrix', version: '1.4.7.RELEASE'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
最好发现是断路器的版本和springboot版本不匹配:
将 compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-hystrix', version: '1.4.7.RELEASE'
替换为 compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix', version: '2.1.0.RELEASE'
即可解决.
解决思路:
(1)由下面这个链接的最后一个回复发现可能是版本问题:https://github.com/spring-projects/spring-boot/issues/12985
(2)查看https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix
后发现spring-cloud-starter-hystrix'被废弃了
(3)根据提示换成最新的 spring-cloud-starter-netflix-hystrix 即可