Java httpclient 4.x性能指南以解决问题 [英] java httpclient 4.x performance guide to resolve issue

查看:82
本文介绍了Java httpclient 4.x性能指南以解决问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一篇不错的文章 http://hc.apache.org/httpclient-与http性能,缓冲池等相关的3.x/performance.html 在最新的4.x版本中找不到相同的版本.有人看到吗?我在高负载下遇到了性能问题,并想解决这些问题. 我正在使用4.1版本. 这是探查器的输出:

There was nice article http://hc.apache.org/httpclient-3.x/performance.html related to http performance, pooling, e.t.c. Can't find the same for the latest 4.x version. Did anyone see it? I met perf issues under heavy load and would like to resolve them. I'm using 4.1 version. Here is profiler output:

26% org.apache.http.impl.client.CloseableHttpClient.execute(multiple parameter matches) :26,107,40
26% org.apache.http.impl.client.CloseableHttpClient.execute(org.apache.http.client.methods.HttpUriRequest, org.apache.http.protocol.HttpContext) :82,46
26% org.apache.http.impl.client.AbstractHttpClient.doExecute(org.apache.http.HttpHost, org.apache.http.HttpRequest, org.apache.http.protocol.HttpContext) :882,818
26% org.apache.http.impl.client.AbstractHttpClient.createHttpContext() :301
26% org.apache.http.impl.client.AbstractHttpClient.getConnectionManager() :484
26% org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager() :321
26% org.apache.http.impl.conn.SchemeRegistryFactory.createDefault() :52
26% org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory() :168
26% org.apache.http.conn.ssl.SSLContexts.createDefault() :58
26% javax.net.ssl.SSLContext.init(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[], java.security.SecureRandom) :283
26% sun.security.ssl.SSLContextImpl.engineInit(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[], java.security.SecureRandom) :83,92
26% javax.net.ssl.TrustManagerFactory.init(java.security.KeyStore) :250
26% sun.security.ssl.TrustManagerFactoryImpl.engineInit(java.security.KeyStore) :51
26% sun.security.ssl.TrustManagerFactoryImpl.getCacertsKeyStore(java.lang.String) :221
26% java.security.KeyStore.load(java.io.InputStream, char[]) :1214
26% sun.security.provider.JavaKeyStore$JKS.engineLoad(java.io.InputStream, char[]) :55
26% sun.security.provider.JavaKeyStore.engineLoad(java.io.InputStream, char[]) :723,747
26% java.security.cert.CertificateFactory.generateCertificate(java.io.InputStream) :339
26% sun.security.provider.X509Factory.engineGenerateCertificate(java.io.InputStream) :93
26% sun.security.provider.X509Factory.getFromCache(sun.security.util.Cache, byte[]) :203

我有4种方法使用httpclient通过HTTP发送一些数据,每种方法都消耗总时间的25%.其余过程需要毫秒.看来我以错误的方式使用了httpclient.

I have 4 methods sending some data via HTTP using httpclient and each of these methods consume 25% of total time. The rest processing takes millis. Looks like I'm using httpclient in a wrong way.

查看所有答案,并阅读 https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html 回答所有已实现的问题

See oleg answers + read that https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html answers all realated questions

主要部分有: 构建池管理器的好方法

Main parts are: Nice way to build pooling manager

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
cm.setMaxTotal(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
// Increase max connections for localhost:80 to 50
HttpHost localhost = new HttpHost("locahost", 80);
cm.setMaxPerRoute(new HttpRoute(localhost), 50);

CloseableHttpClient httpClient = HttpClients.custom()
        .setConnectionManager(cm)
        .build();

同时使用HttpClient的方法

A way to use HttpClient concurrently

//While HttpClient instances are thread safe and can be shared
//between multiple threads of execution, it is highly recommended that 
//each thread maintains its own dedicated instance of HttpContext .


static class GetThread extends Thread {

    private final CloseableHttpClient httpClient;
    private final HttpContext context;
    private final HttpGet httpget;

    public GetThread(CloseableHttpClient httpClient, HttpGet httpget) {
        this.httpClient = httpClient;
        this.context = HttpClientContext.create();
        this.httpget = httpget;
    }

    @Override
    public void run() {
        try {
            CloseableHttpResponse response = httpClient.execute(
                    httpget, context);
            try {
                HttpEntity entity = response.getEntity();
            } finally {
                response.close();
            }
        } catch (ClientProtocolException ex) {
            // Handle protocol errors
        } catch (IOException ex) {
            // Handle I/O errors
        }
    }

}

推荐答案

主要建议仍然与3.1相同.

The main recommendation is still the same as it was for 3.1

请务必重新使用HttpClient实例! HttpClient实例非常昂贵.通过丢弃它,不仅丢弃了实例本身,还丢弃了SSL上下文,连接管理器以及连接管理器可能保持活动状态的所有持久连接.

Please DO re-use the HttpClient instance! HttpClient instances are very expensive. By throwing it away not only you throw away the instance itself, you also throw away the SSL context, the connection manager, and all persistent connections that might be kept-alive by the connection manager.

这篇关于Java httpclient 4.x性能指南以解决问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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