简体   繁体   English

Apache HttpClient 4.5:连接重置

[英]Apache HttpClient 4.5: Connection Resets

I am using httpClient version 4.5 to connect with our external vendor site. 我使用httpClient 4.5版连接我们的外部供应商网站。 We do not need any connection pool or persistent connection, so I am using the BasicHttpClientConnectionManager to create the HttpClient. 我们不需要任何连接池或持久连接,所以我使用BasicHttpClientConnectionManager来创建HttpClient。

This works fine for minimal number of requests, but if I test this for 1TPS for 1 hour, by the end of the test, we start seeing intermittent connection resets. 这适用于最少数量的请求,但如果我测试1TPS 1小时,则在测试结束时,我们开始看到间歇性连接重置。 (guessing request count > 100) (猜测请求数> 100)

I/O exception (java.net.SocketException) caught when processing request to {s}-> https://apiURL:443 : Connection reset 处理{s}的请求时捕获的I / O异常(java.net.SocketException) - > https:// apiURL:443 :连接重置

Please find below the code snippet for making connection. 请在下面找到用于建立连接的代码段。

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), new X509TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2" }, null,SSLConnectionSocketFactory.getDefaultHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", sslsf).register("http", new PlainConnectionSocketFactory()).build();

HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);
HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(1, false);

RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(Integer.parseInt(30000)).setConnectTimeout(Integer.parseInt(30000)).setConnectionRequestTimeout(30000).setCookieSpec(CookieSpecs.STANDARD).build();

CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(defaultRequestConfig).setRetryHandler(retryHandler).evictExpiredConnections().build();

HttpPost httpPost = new HttpPost(<endpoint>);
httpPost.setEntity(new UrlEncodedFormEntity(requestData));
httpResponse = httpClient.execute(httpPost);

I saw that fix for a similar issue reported is already available with version 4.5. 我看到报告的类似问题的修复程序已经可用于4.5版。 (Ref: https://issues.apache.org/jira/browse/HTTPCLIENT-1655 ) provided by Oleg (参考: https//issues.apache.org/jira/browse/HTTPCLIENT-1655 )由Oleg提供

If thats the case, not sure why I am still facing this issue. 如果是这样,不知道为什么我仍然面临这个问题。 Could someone please help on this. 有人可以帮忙吗?

Thanks! 谢谢!

Hi Oleg , 奥列格

I had been using the httpclient version 4.5.3 when I was still seeing the connection reset errors as posted above. 当我仍然看到上面发布的连接重置错误时,我一直在使用httpclient版本4.5.3。

Later noticed that the fix for reset issue was committed to the version 4.5.1 ( https://issues.apache.org/jira/browse/HTTPCLIENT-1655 ). 后来注意到重置问题的修复程序已提交到版本4.5.1( https://issues.apache.org/jira/browse/HTTPCLIENT-1655 )。 So, just tried updating that particular version, ran a test and not seeing the connection reset errors any more. 所以,只是尝试更新该特定版本,运行测试,而不再看到连接重置错误。 I had expected that this fix should also be available in the higher versions starting from 4.5.1. 我曾预计此修复程序也应该从4.5.1开始的更高版本中可用。 But, I guess its somehow missed out in the higher versions, have verified that its still an issue with version 4.5.3 for sure. 但是,我猜它在某种程度上错过了更高版本,已经证实它仍然是版本4.5.3的问题。

So, conclusion is that the connection reset error was fixed using httpclient 4.5.1 jar. 因此,结论是使用httpclient 4.5.1 jar修复了连接重置错误。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM