• HttpClient Fluent API 高并发优化


          apache的httpcomponents-client 4.2之后提供了一套易于使用的facade API称为Fluent API,对于一般使用场景来说,使用起来非常简便,且性能也有一定保证,因为其内部使用了连接池技术,但是在一些具体项目调优过程中也需要对其进行优化和定制。 

         先上代码:

    package com.wangan.utils;
    
    import java.io.IOException;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    
    import javax.net.ssl.SSLContext;
    
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.fluent.Executor;
    import org.apache.http.client.fluent.Request;
    import org.apache.http.config.Registry;
    import org.apache.http.config.RegistryBuilder;
    import org.apache.http.conn.socket.ConnectionSocketFactory;
    import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
    import org.apache.http.conn.socket.PlainConnectionSocketFactory;
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.conn.ssl.SSLInitializationException;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * httpclient fluent工具类
     */
    public class HttpFluentUtil {
        private Logger logger = LoggerFactory.getLogger(HttpFluentUtil.class);
        private final static int MaxPerRoute = 200;
        private final static int MaxTotal = 400;
        final static PoolingHttpClientConnectionManager CONNMGR;
        final static HttpClient CLIENT;
        final static Executor executor;
    
        static {
            LayeredConnectionSocketFactory ssl = null;
            try {
                ssl = SSLConnectionSocketFactory.getSystemSocketFactory();
            } catch (final SSLInitializationException ex) {
                final SSLContext sslcontext;
                try {
                    sslcontext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
                    sslcontext.init(null, null, null);
                    ssl = new SSLConnectionSocketFactory(sslcontext);
                } catch (final SecurityException ignore) {
                } catch (final KeyManagementException ignore) {
                } catch (final NoSuchAlgorithmException ignore) {
                }
            }
    
            final Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", ssl != null ? ssl : SSLConnectionSocketFactory.getSocketFactory()).build();
    
            CONNMGR = new PoolingHttpClientConnectionManager(sfr);
            CONNMGR.setDefaultMaxPerRoute(MaxPerRoute);
            CONNMGR.setMaxTotal(MaxTotal);
            CLIENT = HttpClientBuilder.create().setConnectionManager(CONNMGR).build();
            executor = Executor.newInstance(CLIENT);
        }
    
        public static String Get(String uri, int connectTimeout, int socketTimeout) throws IOException {
            return executor.execute(Request.Get(uri).connectTimeout(connectTimeout).socketTimeout(socketTimeout))
                    .returnContent().asString();
        }
    
        public static String Post(String uri, StringEntity stringEntity, int connectTimeout, int socketTimeout)
                throws IOException {
            return executor.execute(Request.Post(uri).socketTimeout(socketTimeout)
                    .addHeader("Content-Type", "application/json").body(stringEntity)).returnContent().asString();
        }
    
        public static void main(String[] args) {
            HttpUtil httpUtil = new HttpUtil();
            String url = "http://localhost:9064/portal/app/test"; // 服务端sleep5秒再返回
    
            for (int i = 0; i < 5; i++) { // MaxPerRoute若设置为2,则5线程分3组返回(2、2、1),共15秒
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String result = HttpFluentUtil.Get(url, 2000, 2000);
                            System.out.println(result);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
    
                    }
                }).start();
            }
    
        }
    }

    在原org.apache.http.client.fluent包下Executor中MaxPerRoute代表同一路由下最大连接数,MaxTotal总最大连接数,默认值分别为100,200.

    在HttpFluentUtil中我们分别将这两个数字设置为2、4,然后启动5个线程去访问服务端,服务端设置sleep(5000),执行上述程序,5线程分3组每隔5秒(2、2、1)返回了结果,

    调整MaxPerRoute、MaxTotal为(100、200)后,5线程在5秒后同时返回了结果。

    在实际项目中,默认的MaxPerRoute=100、MaxTotal=200依然会出现瓶颈的情况,可根据实际情况进行调整。

  • 相关阅读:
    react 学习
    swiper
    日历插件
    插件 uploadify
    前端 websocket用法
    jQuery插件Highcharts
    可用于nodejs的SuperAgent(ajax API)
    git的基本命令
    自己对javascript闭包的了解
    函数中的this的四种绑定形式
  • 原文地址:https://www.cnblogs.com/lyhero11/p/10571222.html
Copyright © 2020-2023  润新知