【发布时间】:2019-10-02 12:40:24
【问题描述】:
有 SpringBoot 2.1.4.RELEASE 应用程序具有 spring-cloud-starter-netflix-ribbon 2.1.1.RELEASE 依赖项。
在测试应用程序启动期间尝试运行 spring 集成测试(使用@SpringBootTest)会抛出这样的异常:
Caused by: java.lang.ClassNotFoundException:com.netflix.config.CachedDynamicIntProperty
项目中使用的Maven云依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
测试属性文件:
feign.hystrix.enabled=true
eureka.client.enabled=false
ribbon.eureka.enabled=false
some-my-mocked-service.ribbon.listOfServers=localhost:${mocked.port}
例外:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.netflix.loadbalancer.ILoadBalancer]: Factory method 'ribbonLoadBalancer' threw exception; nested exception is java.lang.NoClassDefFoundError: Lcom/netflix/config/CachedDynamicIntProperty;
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
【问题讨论】:
-
你确定这真的是完整的堆栈跟踪吗?
-
不,最初的堆栈跟踪要长得多,但我提取了主要原因。这篇文章也很有用:`引起:org.springframework.beans.factory.BeanCreationException:创建在org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration中定义的名称为'ribbonLoadBalancer'的bean时出错:通过工厂方法进行的bean实例化失败;嵌套异常是 ....//来自问题主体的异常
-
好吧,只是问,因为有时人们不会复制问题的根本原因,而是复制堆栈跟踪的其他部分。使用自动装配越多,这种情况就越常见:-)
-
也许你需要以下依赖
<dependency> <groupId>com.netflix.archaius</groupId> <artifactId>archaius-core</artifactId> <version>0.1.4</version> </dependency> -
archaius-core 没有帮助(
标签: java spring spring-cloud netflix-ribbon