【发布时间】:2018-02-26 10:53:33
【问题描述】:
我有一个简单的 Spring Boot 应用程序,它有一个简单的 REST 客户端,看起来像这样:
@Service
public class MyRestClient {
private static final String url = "http://localhost:8080/";
private RestTemplate restTemplate;
@Autowired
public MyRestClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String invoke() {
return restTemplate.getForObject(url, String.class);
}
}
这与 Spring Boot 完美配合。
现在我正在尝试将Spring Cloud 添加到项目中以进行功能区客户端负载平衡。我点击了这里的链接:
https://spring.io/guides/gs/client-side-load-balancing/
或者这里,这似乎是复制和粘贴,但具有更多更新的依赖项:
http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon
即使没有向MyRestClient 添加任何注释,我添加以下内容的那一刻:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
我得到以下异常:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
... 31 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients
at org.springframework.http.client.HttpComponentsClientHttpRequestFactory.<init>(HttpComponentsClientHttpRequestFactory.java:88) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]
at java.lang.Class.newInstance(Class.java:442) ~[na:1.8.0_131]
at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:77) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.boot.web.client.RestTemplateBuilder.detectRequestFactory(RestTemplateBuilder.java:596) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.web.client.RestTemplateBuilder.configureRequestFactory(RestTemplateBuilder.java:559) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.web.client.RestTemplateBuilder.configure(RestTemplateBuilder.java:527) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.web.client.RestTemplateBuilder.build(RestTemplateBuilder.java:515) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.web.client.RestTemplateBuilder.build(RestTemplateBuilder.java:501) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
为什么我的 REST 客户端在没有这个依赖的情况下工作,但是没有添加任何注释或任何东西,当我添加这个依赖时,我得到这个异常?
我尝试在此处和那里添加文档或示例中的各种依赖项,例如 spring-cloud-dependencies(似乎已弃用)、spring-cloud-netflix 等,但无济于事。
添加什么正确的依赖项才能使其正常工作?
【问题讨论】:
-
请发布您的
pom.xml。 Spring Cloud 具有与 Spring Boot 内联发布的发布序列。 -
请发布您的
application.yml。 -
@DarrenForsythe 如果您能解释与发布火车相关的问题可能是什么(它们可能会非常令人困惑,尤其是当您因为有自己的而不能使用 Spring 父 pom 时)。对于试图解决类似问题的人来说,这可能是有益的。谢谢。
-
Release Trains 与 Spring Boot 内联版本。例如。
Dalston期待 Spring boot1.5.x,Camden期待1.4.X,Brixton期待1.3.X。将 Spring Cloud 版本与意外 Spring Boot 版本混合可能会导致运行时问题,如您所详述,除非单独的依赖项提供HttpClients。
标签: spring rest spring-boot spring-cloud spring-cloud-netflix