微服务-RestTemplate UnknownHostException [英] Microservices - RestTemplate UnknownHostException

查看:1025
本文介绍了微服务-RestTemplate UnknownHostException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Eureka服务注册服务器进行了简单的设置,使用了公共API的服务以及使用RestTemplate从公共API调用的服务.尤里卡告诉我服务已成功注册,但是当我调用服务

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

@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);
    }
}

我收到以下异常

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

谢谢

推荐答案

patrick-grimard 所建议的那样到Brixton并更改代码需要解决此问题.工作解决方案位于 Github .

As suggested by patrick-grimard switching to Brixton and changing the code were needed fixed the issues. Working Solution is on Github.

还将发布的id从请求参数更改为请求正文,这也更改了我将其添加到请求中的方式.

Also changed the posted id from request param to request body, which also changed the way I add it to the request.

服务端点

@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创建

@Configuration
public class PublicAPIConfiguration {
    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

呼叫服务

@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);
    }
}

这篇关于微服务-RestTemplate UnknownHostException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆