RestTemplate java.net.UnknownHostException

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

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

@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;
    }
}
@Autowired
private RestTemplate restTemplate;

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/26/resttemplate-java-net-unknownhostexception/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
RestTemplate java.net.UnknownHostException
RestTemplate 以 Spring Cloud 域名方式(http://application-name)实现负载均衡调用,错误信息: Servlet.service() for servlet [dispatcherServlet] in co……
<<上一篇
下一篇>>
文章目录
关闭
目 录