根据这个博客 https://spring.io/blog/2015/07/14/microservices-with-spring
能够毫无问题地运行应用程序。按照这个顺序:
但是,当尝试通过使用 http://localhost:3333/ 网址访问任何帐户服务端点(如 http://ACCOUNTS-SERVICE )的 Web 应用程序( http://ACCOUNTS-SERVICE/accounts/123456789 )访问任何服务时,我收到错误响应:
Response Status: 500 (Internal Server Error)
Cause: org.springframework.web.client.ResourceAccessException I/O error on GET request for "http://ACCOUNTS-SERVICE/accounts/123456789": ACCOUNTS-SERVICE; nested exception is java.net.UnknownHostException: ACCOUNTS-SERVICE
当我向 Web 服务器而不是 http://localhost:2223/ 提供帐户服务的真实地址( http://ACCOUNTS-SERVICE )时,一切正常,但在这种情况下没有服务发现。
源代码存放在:https://github.com/paulc4/microservices-demo
最佳答案
这个问题是由于在 Brixton 发布系列(Spring Cloud 1.1.0.RELEASE)中不再自动创建 RestTemplate,所以 RestTemplate 无法正确解析 http://ACCOUNTS-SERVICE使用服务发现服务器的 url。
在使用 @LoadBalanced 声明 RestTemplate bean 后能够解决这个问题,如下所示:
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
关于spring-boot - Eureka 服务发现期间的 java.net.UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38310904/