RestTemplate java.net.UnknownHostException

RestTemplate 以 Spring Cloud 域名方式(http://application-name)实现负载均衡调用,错误信息:

1
2
3
4
5
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 GET request for "http://appblog-provider/add": appblog-provider;
nested exception is java.net.UnknownHostException: appblog-provider] with root cause

java.net.UnknownHostException: appblog-provider

解决方法:加上@LoadBalanced

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Configuration
public class CommonConfig {

@Bean
@LoadBalanced //增加 load balance 特性
public RestTemplate restTemplate() {

ClientHttpRequestFactory factory = simpleClientHttpRequestFactory();
return new RestTemplate(factory);
}

public ClientHttpRequestFactory simpleClientHttpRequestFactory() {

SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(15000); //单位为ms
factory.setConnectTimeout(15000); //单位为ms
return factory;
}
}
1
2
@Autowired
private RestTemplate restTemplate;

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :