Apache HttpClient 4.5:连接重置 [英] Apache HttpClient 4.5: Connection Resets

查看:33
本文介绍了Apache HttpClient 4.5:连接重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 httpClient 4.5 版与我们的外部供应商站点连接.我们不需要任何连接池或持久连接,所以我使用 BasicHttpClientConnectionManager 来创建 HttpClient.

I am using httpClient version 4.5 to connect with our external vendor site. We do not need any connection pool or persistent connection, so I am using the BasicHttpClientConnectionManager to create the HttpClient.

这适用于最少数量的请求,但如果我以 1TPS 的速度测试 1 小时,在测试结束时,我们开始看到间歇性连接重置.(猜测请求数 > 100)

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. (guessing request count > 100)

处理对 {s}->https://apiURL 的请求时捕获的 I/O 异常 (java.net.SocketException):443:连接重置

I/O exception (java.net.SocketException) caught when processing request to {s}->https://apiURL:443: Connection reset

请在下方找到用于建立连接的代码片段.

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);

我看到针对所报告的类似问题的修复已在 4.5 版中提供.(参考:https://issues.apache.org/jira/browse/HTTPCLIENT-1655) 由 Oleg

I saw that fix for a similar issue reported is already available with version 4.5. (Ref: https://issues.apache.org/jira/browse/HTTPCLIENT-1655) provided by Oleg

如果是这样,不知道为什么我仍然面临这个问题.有人可以帮忙吗.

If thats the case, not sure why I am still facing this issue. Could someone please help on this.

谢谢!

推荐答案

Oleg,

当我仍然看到上面发布的连接重置错误时,我一直在使用 httpclient 版本 4.5.3.

I had been using the httpclient version 4.5.3 when I was still seeing the connection reset errors as posted above.

后来注意到重置问题的修复已提交到版本 4.5.1 (https://issues.apache.org/jira/browse/HTTPCLIENT-1655).因此,只是尝试更新该特定版本,运行测试并且不再看到连接重置错误.我原以为从 4.5.1 开始的更高版本中也应该可以使用此修复程序.但是,我想它在更高版本中不知何故错过了,已经证实它肯定仍然是 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). 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. 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.

因此,结论是使用 httpclient 4.5.1 jar 修复了连接重置错误.

So, conclusion is that the connection reset error was fixed using httpclient 4.5.1 jar.

谢谢!

这篇关于Apache HttpClient 4.5:连接重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆