java.net.SocketTimeoutException: Read timed out 异常排查

问题描述:使用RestTemplate调用接口出现该异常,相关调用代码:

ResponseEntity responseEntity;
        try {
            responseEntity = restTemplate.getForEntity( url, ResultVO.class );
        } catch (Exception e) {
            log.error("ops获取项目失败" + e.getMessage());
            throw new OAuth2Exception( FAILED_TO_GET_PROJECT );
        }

原因:httpClient在获取链接时候超时
解决:将超时时间设置久一点

@Bean
public RestTemplate restTemplate() {
    //复杂构造函数的使用
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setConnectTimeout(30000);// 设置超时
    requestFactory.setReadTimeout(30000);
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setRequestFactory(requestFactory);
    return restTemplate;
}

解决了。

转载于:https://www.cnblogs.com/liuyupen/p/11162089.html

你可能感兴趣的:(java.net.SocketTimeoutException: Read timed out 异常排查)