java 微服务 - RestTemplate UnknownHostException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37159662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Microservices - RestTemplate UnknownHostException
提问by KenavR
I have a simple setup with a Eureka service registration server, a service for the public API and a service that gets called from the public API using RestTemplate. Eureka tells me that the services are successfully registered, but when I call the service
我有一个 Eureka 服务注册服务器、一个公共 API 服务和一个使用 RestTemplate 从公共 API 调用的服务的简单设置。Eureka告诉我服务注册成功,但是当我调用服务时
@Service
public class MyServiceService {
@Autowired
private RestTemplate restTemplate;
private final String serviceUrl;
public MyServiceService() {
this.serviceUrl = "http://MY-SERVICE";
}
public Map<String, String> getTest() {
Map<String, String> vars = new HashMap<>();
vars.put("id", "1");
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
return restTemplate.postForObject(serviceUrl+"/test", "", Map.class, vars);
}
}
I get the following exception
我收到以下异常
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed;
nested exception is org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://MY-SERVICE/test": MY-SERVICE;
nested exception is java.net.UnknownHostException: MY-SERVICE] with root cause java.net.UnknownHostException: MY-SERVICE
I created a sample project to illustrate my setup, maybe someone could take a look at it and tell me what's wrong with my setup.
我创建了一个示例项目来说明我的设置,也许有人可以看看它并告诉我我的设置有什么问题。
https://github.com/KenavR/spring-boot-microservices-example
https://github.com/KenavR/spring-boot-microservices-example
thanks
谢谢
回答by KenavR
As suggested by patrick-grimardswitching to Brixton and changing the code were needed fixed the issues. Working Solution is on Github.
正如patrick-grimard建议的那样,切换到 Brixton 并更改代码需要解决这些问题。工作解决方案在Github 上。
Also changed the posted id
from request param to request body, which also changed the way I add it to the request.
id
还将发布的请求参数更改为请求正文,这也改变了我将其添加到请求的方式。
Service endpoint
服务端点
@RequestMapping(method = RequestMethod.POST, produces = "application/json; charset=utf-8")
public @ResponseBody Map<String, String> getTest(@RequestBody Map<String, Long> params) {
Map<String, String> response = new HashMap<>();
response.put("name", "My Service");
return response;
}
RestTemplate creation
RestTemplate 创建
@Configuration
public class PublicAPIConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
Calling service
呼叫服务
@Service
public class MyServiceService {
@Autowired
private RestTemplate restTemplate;
private final String serviceUrl;
public MyServiceService() {
this.serviceUrl = "http://my-service";
}
public Map<String, String> getTest() {
Map<String, Long> vars = new HashMap<>();
vars.put("id", 1L);
return restTemplate.postForObject(serviceUrl+"/test", vars, Map.class);
}
}
回答by Brent Thoenen
Just for anyone in the future that might also happen to come across this issue, i had the exact same stacktrace, but was slightly different for a solution.
对于将来也可能遇到此问题的任何人,我有完全相同的堆栈跟踪,但解决方案略有不同。
My issue was not related to a configuration from a coding standpoint. It was related to the server i was running the code on. I was ignoring the fact that it was on a DMZ, which doesn't have a DNS, so you have to manually map a domain to an IP.
从编码的角度来看,我的问题与配置无关。它与我运行代码的服务器有关。我忽略了它位于没有 DNS 的 DMZ 上的事实,因此您必须手动将域映射到 IP。
More or less, make sure your DNS is properly configured on your server, because, from a restTemplate standpoint, it will throw this exact stacktrace.
或多或少,请确保您的 DNS 在您的服务器上正确配置,因为从 restTemplate 的角度来看,它会抛出这个确切的堆栈跟踪。