Microservices - RestTemplate UnknownHostException

孤街浪徒 提交于 2019-12-05 18:58:06
KenavR

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

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

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

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

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.

More or less, make sure your DNS is properly configured on your server, because, from a restTemplate standpoint, it will throw this exact stacktrace.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!