从@configuration类调用另一个eureka客户端失败

时间:2018-12-20 14:29:20

标签: spring spring-boot netflix-eureka

我想使用@Configuration类中的(loadBalanced)restTemplate调用hello服务(使用Netflix Eureka),但出现错误:

  

POST请求“ http://hello/”时发生I / O错误:您好;嵌套的异常是java.net.UnknownHostException:您好

如果我按需进行相同的呼叫-可以。

我的配置类:

@Configuration
public class OAuth2ResourceServerConfig extends WebSecurityConfigurerAdapter {

    @Autowired  //It's load balanced in a separate config class
    private RestTemplate restTemplate;

    @Bean
    private String callToHelloService() {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        Map body = new HashMap<String, String>();
        body.put("hello", "world");

        HttpEntity<Map> entity = new HttpEntity<Map>(body, headers);

        HttpStatus status;

        try {
            status = restTemplate.postForEntity("http://hello/", entity, String.class).getStatusCode();
            System.out.print("\nCommunication succeed! status: " + status.value() + "\n");
        }
        catch (Exception ex) {
            System.out.print(ex.getMessage() + "\n");
        }
        ....
        ....
    }
}

0 个答案:

没有答案