Ken*_*avR 4 java spring-boot microservices spring-cloud netflix-eureka
我有一个简单的设置,包括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);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下异常
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
Run Code Online (Sandbox Code Playgroud)
我创建了一个示例项目来说明我的设置,也许有人可以看看它并告诉我我的设置有什么问题.
https://github.com/KenavR/spring-boot-microservices-example
谢谢
正如patrick-grimard建议转换到Brixton并更改代码需要修复问题.Working Solution在Github上.
id还将发布的请求参数更改为请求正文,这也改变了我将其添加到请求的方式.
服务端点
@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;
}
Run Code Online (Sandbox Code Playgroud)
RestTemplate创建
@Configuration
public class PublicAPIConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
Run Code Online (Sandbox Code Playgroud)
致电服务
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10589 次 |
| 最近记录: |