Apache HttpClient读取在一段时间后超时

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

我已经使用Spring Boot + RestTemplate + Apache HttpClient来获取信息。在当前设置中,我正在5个线程中运行请求。但是几分钟后,连接开始超时。

当我重新启动Spring Boot应用程序时,请求在几分钟内再次正常。不知道是什么原因导致这些超时。

2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] o.a.h.client.protocol.RequestAuthCache   : Auth cache not set in the context
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] h.i.c.PoolingHttpClientConnectionManager : Connection request: [route: {s}->https://www.somewebsite.com:443][total available: 5; route allocated: 1 of 20; total allocated: 5 of 20]
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] h.i.c.PoolingHttpClientConnectionManager : Connection leased: [id: 29][route: {s}->https://www.somewebsite.com:443][total available: 4; route allocated: 1 of 20; total allocated: 5 of 20]
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-29: set socket timeout to 0
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-29: set socket timeout to 10000
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] o.a.http.impl.execchain.MainClientExec   : Executing request GET <url> HTTP/1.1
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] o.a.http.impl.execchain.MainClientExec   : Target auth state: UNCHALLENGED
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] o.a.http.impl.execchain.MainClientExec   : Proxy auth state: UNCHALLENGED
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 >> GET <url> HTTP/1.1
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 >> User-Agent: Mozilla/5.0
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 >> Accept: text/plain, application/json, application/*+json, */*
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 >> Host: www.somewebsite.com
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 >> Connection: Keep-Alive
2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 >> Cookie: <Cookies>
2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 >> Accept-Encoding: gzip,deflate
2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 >> "GET <url> HTTP/1.1[\r][\n]"
2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 >> "User-Agent: Mozilla/5.0[\r][\n]"
2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 >> "Accept: text/plain, application/json, application/*+json, */*[\r][\n]"
2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 >> "Host: www.somewebsite.com[\r][\n]"
2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 >> "Connection: Keep-Alive[\r][\n]"
2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 >> "Cookie: <cookies> [\r][\n]"
2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 >> "Accept-Encoding: gzip,deflate[\r][\n]"
2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 >> "[\r][\n]"
2020-03-21 12:52:34.197 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 << "[read] I/O error: Read timed out"
2020-03-21 12:52:34.198 DEBUG 72712 --- [        task-14] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-29: Close connection
2020-03-21 12:52:34.199 DEBUG 72712 --- [        task-14] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-29: Shutdown connection
2020-03-21 12:52:34.199 DEBUG 72712 --- [        task-14] o.a.http.impl.execchain.MainClientExec   : Connection discarded
2020-03-21 12:52:34.199 DEBUG 72712 --- [        task-14] h.i.c.PoolingHttpClientConnectionManager : Connection released: [id: 29][route: {s}->https://www.somewebsite.com:443][total available: 4; route allocated: 0 of 20; total allocated: 4 of 20]

这是我的设置

  @Bean
    public ClientHttpRequestFactory createRequestFactory(final CookieStore cookieStore) {
        final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(20);
        connectionManager.setDefaultMaxPerRoute(20);
        final RequestConfig requestConfig = RequestConfig
                .custom()
                .setCookieSpec(CookieSpecs.STANDARD)
                .setConnectionRequestTimeout(5000)
                .setSocketTimeout(10000)
                .build();

        final CloseableHttpClient httpClient = HttpClients
                .custom()
                .setConnectionManager(connectionManager)
                .setDefaultRequestConfig(requestConfig)
                .setDefaultCookieStore(cookieStore)
                .build();
        return new HttpComponentsClientHttpRequestFactory(httpClient);
    }

    @Bean
    public RestTemplate restTemplate(final ClientHttpRequestFactory clientHttpRequestFactory) {
        final DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(
                "<url>");
        uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);

        final RestTemplate restTemplate = new RestTemplateBuilder()
                .uriTemplateHandler(uriBuilderFactory)
                .defaultHeader("User-Agent", "Mozilla/5.0")
                .build(RestTemplate.class);

        restTemplate.setRequestFactory(clientHttpRequestFactory);
        return restTemplate;
    }

即使不重新使用连接

HttpClients
                .custom()
                .setConnectionReuseStrategy(new NoConnectionReuseStrategy())...

我仍然遇到问题。也许有人遇到过类似的事情或有任何想法?

java spring-boot apache-httpclient-4.x
1个回答
0
投票

好,所以这与HttpClient没有任何关系。显然,服务器使请求超时,并且正在通过cookie跟踪客户端。

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