REST代理下HTTP客户端4.5的Rest模板的连接超时

问题描述 投票:1回答:1

如何设置超时时间?我已经设置了几乎所有超时配置。通常没有代理,它可以正常工作。当我使用代理连接详细信息进行连接时,会发生问题。日志显示它正在连接并且没有收到回复。

2020-03-04 19:53:39,751 DEBUG [main] org.springframework.core.log.CompositeLog: HTTP POST https://destinationurl.io/api/agentmanagement/v3/oauth/token
2020-03-04 19:53:39,803 DEBUG [main] org.springframework.core.log.CompositeLog: Accept=[application/json, application/*+json]
2020-03-04 19:53:39,807 DEBUG [main] org.springframework.core.log.CompositeLog: Writing [grant_type=client_credentials&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&client_assertion=token--cjKRDhVI0OuYo] as "application/x-www-form-urlencoded"
2020-03-04 19:53:39,821 DEBUG [main] org.apache.http.client.protocol.RequestAuthCache: Auth cache not set in the context
2020-03-04 19:53:39,823 DEBUG [main] org.apache.http.impl.conn.PoolingHttpClientConnectionManager: Connection request: [route: {tls}->http://proxyurl:3128->https://destinationurl.io:443][total kept alive: 0; route allocated: 0 of 20; total allocated: 0 of 100]
2020-03-04 19:53:39,838 DEBUG [main] org.apache.http.impl.conn.PoolingHttpClientConnectionManager: Connection leased: [id: 0][route: {tls}->http://proxyurl:3128->https://destinationurl.io:443][total kept alive: 0; route allocated: 1 of 20; total allocated: 1 of 100]
2020-03-04 19:53:39,839 DEBUG [main] org.apache.http.impl.execchain.MainClientExec: Opening connection {tls}->http://proxyurl:3128->https://destinationurl.io:443
2020-03-04 19:53:40,047 DEBUG [main] org.apache.http.impl.conn.DefaultHttpClientConnectionOperator: Connecting to proxyurl/35.158.73.29:3128
2020-03-04 19:53:40,420 DEBUG [main] org.apache.http.impl.conn.DefaultHttpClientConnectionOperator: Connection established 132.186.74.92:65173<->35.158.73.29:3128
2020-03-04 19:53:40,423 DEBUG [main] org.apache.http.impl.conn.LoggingManagedHttpClientConnection: http-outgoing-0 >> CONNECT destinationurl.io:443 HTTP/1.1
2020-03-04 19:53:40,424 DEBUG [main] org.apache.http.impl.conn.LoggingManagedHttpClientConnection: http-outgoing-0 >> Host: destinationurl.io
2020-03-04 19:53:40,424 DEBUG [main] org.apache.http.impl.conn.LoggingManagedHttpClientConnection: http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.10 (Java/1.8.0_201)
2020-03-04 19:53:40,424 DEBUG [main] org.apache.http.impl.conn.Wire: http-outgoing-0 >> "CONNECT destinationurl.io:443 HTTP/1.1[\r][\n]"
2020-03-04 19:53:40,424 DEBUG [main] org.apache.http.impl.conn.Wire: http-outgoing-0 >> "Host: destinationurl.io[\r][\n]"
2020-03-04 19:53:40,424 DEBUG [main] org.apache.http.impl.conn.Wire: http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.10 (Java/1.8.0_201)[\r][\n]"
2020-03-04 19:53:40,424 DEBUG [main] org.apache.http.impl.conn.Wire: http-outgoing-0 >> "[\r][\n]"

附有日志。

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.11</version>
    </dependency>

这是我的配置。我正在使用Spring Boot 2.2。

PoolingHttpClientConnectionManager connectionManager;
if (properties.isAllCertificateAllowed()) {
  connectionManager =
          new PoolingHttpClientConnectionManager(
                  buildSocketFactoryRegistry(trustAllSslConnectionSocketFactory(trustAllSslContext())));
} else {
  connectionManager = new PoolingHttpClientConnectionManager();
}

RequestConfig config = RequestConfig.custom()
        .setSocketTimeout(properties.getConnectTimeout())
        .setConnectTimeout(properties.getConnectTimeout())
        .setConnectionRequestTimeout(properties.getConnectTimeout())
        .build();

/* HttpClientBuilder using http client connection connectionManager */
HttpClientBuilder httpClientBuilder =
        HttpClientBuilder.create()
                .setConnectionManager(connectionManager)
                .useSystemProperties()
                .disableCookieManagement()
                .setDefaultRequestConfig(config);

HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectTimeout(properties.getConnectTimeout()) ;
httpRequestFactory.setReadTimeout(properties.getReadTimeout());
httpRequestFactory.setConnectionRequestTimeout(properties.getConnectionRequestTimeout());

if (properties.isProxyEnabled()) {
  log.info(properties.toString());

  /* proxy username ,password name setting */
  CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(
          new AuthScope(properties.getProxyAddress(), properties.getProxyPort()),
          new UsernamePasswordCredentials(
                  properties.getProxyUsername(), properties.getProxyPassword()));

  httpClientBuilder.setProxy(
          new HttpHost(properties.getProxyAddress(), properties.getProxyPort())); //TODO,"https"
  httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

  CloseableHttpClient httpClient = httpClientBuilder.build();
  httpRequestFactory.setHttpClient(httpClient);
}
RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
java spring spring-boot apache-httpclient-4.x resttemplate
1个回答
1
投票

看起来像是代理问题。应当使用给定的代理详细信息检查代理配置的HTTP / HTTPS模式。您可以在创建restTemplate时提供信任所有证书的选项。请参阅下面的示例以信任所有证书:-

参考:Spring RestTemplate Connection Timeout is not working

© www.soinside.com 2019 - 2024. All rights reserved.