在nGinx服务器端,客户端的连接保持活动时间设置为30秒。但是使用这段代码和附加的日志,HttpClient 4.3。不尊重keep alive并在每次请求后关闭连接。为什么会这样?
我反复尝试netstat-an,发现客户机在for循环执行期间的不同时间打开了不同的端口,每2秒就有一次FIN状态,然后得出了这个结论。
下面给出的日志还显示,everyrequest之后连接已关闭
static ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
return 30 * 1000;
}
};
public static void main(String[] args) throws Exception {
java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINER);
java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINER);
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "debug");
callServer();
}
public static void callServer() throws Exception {
KeyStore keyStore = KeyStore.getInstance("pkcs12");
FileInputStream instream = new FileInputStream(new File("/engineering/workspace/nginx_pilot/keystores/clientkeystore.pkcs"));
try {
keyStore.load(instream, "****".toCharArray());
} finally {
instream.close();
}
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
instream = new FileInputStream(new File("/engineering/workspace/nginx_pilot/keystores/serverKeystore.jks"));
try {
trustStore.load(instream, "****".toCharArray());
} finally {
instream.close();
}
SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, "pass".toCharArray()).loadTrustMaterial(trustStore).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE).build();
CloseableHttpResponse response = null;
try {
HttpGet httpget = new HttpGet("https://" + getString() + ":8443/GenevaServers/GetConfig/unit.test.asset.ast1");
System.out.println("executing request" + httpget.getRequestLine());
for (int i = 0; i < 10000; i++) {
System.out.println("Beginning");
long currentTimeMillis = System.currentTimeMillis();
response = httpclient.execute(httpget);
System.out.println((System.currentTimeMillis() - currentTimeMillis) + "ms");
HttpEntity entity = response.getEntity();
System.out.println(IOUtils.toString(entity.getContent()));
EntityUtils.consume(entity);
System.out.println("End");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
response.close();
httpclient.close();
}
}
这些是日志。
2014/01/22 19:24:50:252 PST [DEBUG] RequestAddCookies - CookieSpec selected: best-match
2014/01/22 19:24:50:252 PST [DEBUG] RequestAuthCache - Auth cache not set in the context
2014/01/22 19:24:50:252 PST [DEBUG] PoolingHttpClientConnectionManager - Connection request: [route: {s}->https://ichor.corp.com:8443][total kept alive: 2; route allocated: 2 of 2; total allocated: 2 of 20]
2014/01/22 19:24:50:253 PST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-0: Close connection
2014/01/22 19:24:50:254 PST [DEBUG] PoolingHttpClientConnectionManager - Connection leased: [id: 2][route: {s}->https://ichor.corp.com:8443][total kept alive: 1; route allocated: 2 of 2; total allocated: 2 of 20]
2014/01/22 19:24:50:254 PST [DEBUG] MainClientExec - Opening connection {s}- >https://ichor.corp.com:8443
2014/01/22 19:24:50:254 PST [DEBUG] HttpClientConnectionManager - Connecting to ichor.corp.com/17.169.1.18:8443
2014/01/22 19:24:50:392 PST [DEBUG] MainClientExec - Executing request GET /GenevaServers/GetConfig/unit.test.asset.ast1 HTTP/1.1
2014/01/22 19:24:50:392 PST [DEBUG] MainClientExec - Target auth state: UNCHALLENGED
2014/01/22 19:24:50:392 PST [DEBUG] MainClientExec - Proxy auth state: UNCHALLENGED
2014/01/22 19:24:50:392 PST [DEBUG] headers - http-outgoing-2 >> GET /GenevaServers/GetConfig/unit.test.asset.ast1 HTTP/1.1
2014/01/22 19:24:50:392 PST [DEBUG] headers - http-outgoing-2 >> Host: ichor.corp.com:8443
2014/01/22 19:24:50:392 PST [DEBUG] headers - http-outgoing-2 >> Connection: Keep-Alive
2014/01/22 19:24:50:392 PST [DEBUG] headers - http-outgoing-2 >> User-Agent: Apache- HttpClient/4.3.1 (java 1.5)
2014/01/22 19:24:50:392 PST [DEBUG] headers - http-outgoing-2 >> Accept-Encoding: gzip,deflate
2014/01/22 19:24:50:392 PST [DEBUG] wire - http-outgoing-2 >> "GET /GenevaServers/GetConfig/unit.test.asset.ast1 HTTP/1.1[\r][\n]"
2014/01/22 19:24:50:392 PST [DEBUG] wire - http-outgoing-2 >> "Host: ichor.corp.com:8443[\r][\n]"
2014/01/22 19:24:50:392 PST [DEBUG] wire - http-outgoing-2 >> "Connection: Keep-Alive[\r][\n]"
2014/01/22 19:24:50:392 PST [DEBUG] wire - http-outgoing-2 >> "User-Agent: Apache-HttpClient/4.3.1 (java 1.5)[\r][\n]"
2014/01/22 19:24:50:392 PST [DEBUG] wire - http-outgoing-2 >> "Accept-Encoding: gzip,deflate[\r][\n]"
2014/01/22 19:24:50:393 PST [DEBUG] wire - http-outgoing-2 >> "[\r][\n]"
2014/01/22 19:24:50:462 PST [DEBUG] wire - http-outgoing-2 << "HTTP/1.1 200 OK[\r][\n]"
2014/01/22 19:24:50:462 PST [DEBUG] wire - http-outgoing-2 << "Server: nginx/1.4.1[\r][\n]"
2014/01/22 19:24:50:463 PST [DEBUG] wire - http-outgoing-2 << "Date: Thu, 23 Jan 2014 03:24:09 GMT[\r][\n]"
2014/01/22 19:24:50:463 PST [DEBUG] wire - http-outgoing-2 << "Content-Type: application/json[\r][\n]"
2014/01/22 19:24:50:463 PST [DEBUG] wire - http-outgoing-2 << "Content-Length: 270[\r][\n]"
2014/01/22 19:24:50:463 PST [DEBUG] wire - http-outgoing-2 << "Connection: keep-alive[\r][\n]"
2014/01/22 19:24:50:463 PST [DEBUG] wire - http-outgoing-2 << "[\r][\n]"
任何建议都会有很大帮助。事先谢谢你。
当然有,但这是一个特例
SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, "pass".toCharArray()).loadTrustMaterial(trustStore).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
您正在使用SSL密钥与目标服务器进行身份验证。这使得连接代表特定的用户并承载特定的安全上下文。换句话说,连接有一个特定的状态(用户主体)。您很可能不希望将此连接租给可能代表完全不同用户的任意执行线程,是吗?HttpClient 4(与之前的版本不同)跟踪有状态连接,并确保它们不能被具有不同执行上下文的线程重用。
有几种方法可以解决这个问题
>
手动管理连接状态
禁用连接状态跟踪。此措施将产生安全影响,因此必须仔细考虑
我对Quarkus应用程序中的配置进行了非常简单的调整,如Quarkus指南所述: 我这样做是为了使用注释包装/解包我的对象: 当与我的实际API相对时,这很好,但每当我在一个测试中使用restasured时,restasured似乎并不尊重我的ObjectMapper配置,也不会像注释所示那样包装CreateArticleRequest。 这将我的请求正文序列化为: ...而不是... 我实际上
问题内容: 在纯Java SE 6环境中: Eclipse控制台中未显示任何内容。 l.info(“”) 及以上的作品就好了,但低于任何 罚款 只是似乎没有工作。有什么问题吗?TIA。 问题答案: 即使Logger级别设置为ALL,ConsoleHandler(记录器上的默认Handler)仍然具有INFO的默认级别。这来自 JAVA_HOME / jre / lib中 的默认logging.pr
我想安排一系列动画,在屏幕上动画介绍文字。最后一个动画在完成时,应该触发运行游戏的游戏勾号逻辑。 出于某种原因,一切都在立即发生。有人能解释为什么会发生这种情况吗?或者有更好的选择吗?
我被要求建立一个网站,一个共同开发人员告诉我,我需要包括keep-alive头。 嗯,我读了很多关于它的书,但我仍然有问题。 msdn-> null
我正在我的Linux box上试验TCP keep alive,并编写了以下小型服务器: 然后我将系统范围的TCP保持活动状态设置调整为: 然后我从Windows连接到服务器,并运行Wireshark跟踪以查看保持活动的数据包。下图显示了结果。
我使用okhttp3和retrofit2来获取json文件。我尝试了所有方法来使用keep-Alive连接来使数据下载更快,但似乎没有任何效果。 我已经实现了拦截器,并添加了keep-alive头。但它似乎就是不想工作。有人能看看我的代码,告诉我我在这里做错了什么吗?这是我的代码: 我想在这里补充的一点是,我使用Glide从服务器加载图像,Glide似乎使用Keep-Alive设置安静很好,因为G