猿问

spring boot2 RestTemplate 请求URL超时

spring boot2 RestTemplate 请求URL超时

RestTemplate配置代码如下

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(30000);
        httpRequestFactory.setConnectTimeout(30000);
        httpRequestFactory.setReadTimeout(30000);

        return builder.requestFactory(() -> httpRequestFactory).build();
    }

报错信息如下:

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest": Connect to pro-api.coinmarketcap.com:443 [pro-api.coinmarketcap.com/13.35.121.116, pro-api.coinmarketcap.com/13.35.121.31, pro-api.coinmarketcap.com/13.35.121.107] failed: Read timed out; nested exception is org.apache.http.conn.ConnectTimeoutException: Connect to pro-api.coinmarketcap.com:443 [pro-api.coinmarketcap.com/13.35.121.116, pro-api.coinmarketcap.com/13.35.121.31, pro-api.coinmarketcap.com/13.35.121.107] failed: Read timed out
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:696)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:622)
    at com.chinawkb.monitor.service.impl.CoinMarketServiceImpl.getData(CoinMarketServiceImpl.java:58)
    at com.chinawkb.monitor.service.impl.CoinMarketServiceImpl.updateTokenInfos(CoinMarketServiceImpl.java:39)
    at com.chinawkb.monitor.EthereumMonitorApplication$Test.run(EthereumMonitorApplication.java:59)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:792)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:776)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)
    at com.chinawkb.monitor.EthereumMonitorApplication.main(EthereumMonitorApplication.java:38)
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to pro-api.coinmarketcap.com:443 [pro-api.coinmarketcap.com/13.35.121.116, pro-api.coinmarketcap.com/13.35.121.31, pro-api.coinmarketcap.com/13.35.121.107] failed: Read timed out
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:373)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:394)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:87)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:687)
    ... 10 common frames omitted
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:396)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:355)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    ... 23 common frames omitted

已确认rest接口是可以访问的(同一台机器上的postMan可以正常运行)

有没有人遇到过,求教!!

萧十郎
浏览 2823回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答