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

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

问题描述

有好文章 http://hc.apache.org/httpclient-3.x/performance.html 与http性能、池化等相关找不到与最新 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.

请参阅 oleg 答案 + 阅读 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天全站免登陆