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

查看:104
本文介绍了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天全站免登陆