У меня есть сценарий, в котором я использую 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())...
Я все еще сталкиваюсь с проблемой. Может, кто-то сталкивался с чем-то похожим или есть идеи?