1. 程式人生 > >HttpClient高併發下效能優化-http連線池

HttpClient高併發下效能優化-http連線池

首先,明確兩點:

1.http連線池不是萬能的,過多的長連線會佔用伺服器資源,導致其他服務受阻
2.http連線池只適用於請求是經常訪問同一主機(或同一個介面)的情況下
3.併發數不高的情況下資源利用率低下

那麼,當你的業務符合上面3點,那麼你可以考慮使用http連線池來提高伺服器效能

使用http連線池的優點:

1.複用http連線,省去了tcp的3次握手和4次揮手的時間,極大降低請求響應的時間
2.自動管理tcp連線,不用人為地釋放/建立連線

使用http連線池的大致流程 :

1.建立PoolingHttpClientConnectionManager例項
2.給manager設定引數
3.給manager設定重試策略
4.給manager設定連線管理策略
5.開啟監控執行緒,及時關閉被伺服器單向斷開的連線
6.構建HttpClient例項
7.建立HttpPost/HttpGet例項,並設定引數
8.獲取響應,做適當的處理
9.將用完的連線放回連線池

public class HttpConnectionPoolUtil {

    private static Logger logger = LoggerFactory.getLogger(HttpConnectionPoolUtil.class);

    private static final int CONNECT_TIMEOUT = Config.getHttpConnectTimeout();// 設定連線建立的超時時間為10s
    private static final int SOCKET_TIMEOUT = Config.getHttpSocketTimeout();
    private static final int MAX_CONN = Config.getHttpMaxPoolSize(); // 最大連線數
    private static final int Max_PRE_ROUTE = Config.getHttpMaxPoolSize();
    private static final int MAX_ROUTE = Config.getHttpMaxPoolSize();
    private static CloseableHttpClient httpClient; // 傳送請求的客戶端單例
    private static PoolingHttpClientConnectionManager manager; //連線池管理類
    private static ScheduledExecutorService monitorExecutor;

    private final static Object syncLock = new Object(); // 相當於執行緒鎖,用於執行緒安全

    /**
     * 對http請求進行基本設定
     * @param httpRequestBase http請求
     */
    private static void setRequestConfig(HttpRequestBase httpRequestBase){
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECT_TIMEOUT)
                .setConnectTimeout(CONNECT_TIMEOUT)
                .setSocketTimeout(SOCKET_TIMEOUT).build();

        httpRequestBase.setConfig(requestConfig);
    }

    public static CloseableHttpClient getHttpClient(String url){
        String hostName = url.split("/")[2];
        System.out.println(hostName);
        int port = 80;
        if (hostName.contains(":")){
            String[] args = hostName.split(":");
            hostName = args[0];
            port = Integer.parseInt(args[1]);
        }

        if (httpClient == null){
            //多執行緒下多個執行緒同時呼叫getHttpClient容易導致重複建立httpClient物件的問題,所以加上了同步鎖
            synchronized (syncLock){
                if (httpClient == null){
                    httpClient = createHttpClient(hostName, port);
                    //開啟監控執行緒,對異常和空閒執行緒進行關閉
                    monitorExecutor = Executors.newScheduledThreadPool(1);
                    monitorExecutor.scheduleAtFixedRate(new TimerTask() {
                        @Override
                        public void run() {
                            //關閉異常連線
                            manager.closeExpiredConnections();
                            //關閉5s空閒的連線
                            manager.closeIdleConnections(Config.getHttpIdelTimeout(), TimeUnit.MILLISECONDS);
                            logger.info("close expired and idle for over 5s connection");
                        }
                    }, Config.getHttpMonitorInterval(), Config.getHttpMonitorInterval(), TimeUnit.MILLISECONDS);
                }
            }
        }
        return httpClient;
    }

    /**
     * 根據host和port構建httpclient例項
     * @param host 要訪問的域名
     * @param port 要訪問的埠
     * @return
     */
    public static CloseableHttpClient createHttpClient(String host, int port){
        ConnectionSocketFactory plainSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
        LayeredConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", plainSocketFactory)
                .register("https", sslSocketFactory).build();

        manager = new PoolingHttpClientConnectionManager(registry);
        //設定連線引數
        manager.setMaxTotal(MAX_CONN); // 最大連線數
        manager.setDefaultMaxPerRoute(Max_PRE_ROUTE); // 路由最大連線數

        HttpHost httpHost = new HttpHost(host, port);
        manager.setMaxPerRoute(new HttpRoute(httpHost), MAX_ROUTE);

        //請求失敗時,進行請求重試
        HttpRequestRetryHandler handler = new HttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
                if (i > 3){
                    //重試超過3次,放棄請求
                    logger.error("retry has more than 3 time, give up request");
                    return false;
                }
                if (e instanceof NoHttpResponseException){
                    //伺服器沒有響應,可能是伺服器斷開了連線,應該重試
                    logger.error("receive no response from server, retry");
                    return true;
                }
                if (e instanceof SSLHandshakeException){
                    // SSL握手異常
                    logger.error("SSL hand shake exception");
                    return false;
                }
                if (e instanceof InterruptedIOException){
                    //超時
                    logger.error("InterruptedIOException");
                    return false;
                }
                if (e instanceof UnknownHostException){
                    // 伺服器不可達
                    logger.error("server host unknown");
                    return false;
                }
                if (e instanceof ConnectTimeoutException){
                    // 連線超時
                    logger.error("Connection Time out");
                    return false;
                }
                if (e instanceof SSLException){
                    logger.error("SSLException");
                    return false;
                }

                HttpClientContext context = HttpClientContext.adapt(httpContext);
                HttpRequest request = context.getRequest();
                if (!(request instanceof HttpEntityEnclosingRequest)){
                    //如果請求不是關閉連線的請求
                    return true;
                }
                return false;
            }
        };

        CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).setRetryHandler(handler).build();
        return client;
    }

    /**
     * 設定post請求的引數
     * @param httpPost
     * @param params
     */
    private static void setPostParams(HttpPost httpPost, Map<String, String> params){
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        Set<String> keys = params.keySet();
        for (String key: keys){
            nvps.add(new BasicNameValuePair(key, params.get(key)));
        }
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static JsonObject post(String url, Map<String, String> params){
        HttpPost httpPost = new HttpPost(url);
        setRequestConfig(httpPost);
        setPostParams(httpPost, params);
        CloseableHttpResponse response = null;
        InputStream in = null;
        JsonObject object = null;
        try {
            response = getHttpClient(url).execute(httpPost, HttpClientContext.create());
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                in = entity.getContent();
                String result = IOUtils.toString(in, "utf-8");
                Gson gson = new Gson();
                object = gson.fromJson(result, JsonObject.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if (in != null) in.close();
                if (response != null) response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return object;
    }

    /**
     * 關閉連線池
     */
    public static void closeConnectionPool(){
        try {
            httpClient.close();
            manager.close();
            monitorExecutor.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

關鍵的地方有以下幾點:
1.httpclient例項必須是單例,且該例項必須使用HttpClients.custom().setConnectionManager()來繫結一個PollingHttpClientConnectionManager,這樣該client每次傳送請求都會通過manager來獲取連線,如果連線池中沒有可用連線的話,則該會阻塞執行緒,直到有可用的連線

2.httpclients4.5.x版本直接呼叫ClosableHttpResponse.close()就能直接把連線放回連線池,而不是關閉連線,以前的版本貌似要呼叫其他方法才能把連線放回連線池

3.由於伺服器一般不會允許無限期的長連線,所以需要開啟監控執行緒,每隔一段時間就檢測一下連線池中連線的情況,及時關閉異常連線和長時間空閒的連線,避免佔用伺服器資源.



作者:PigPIgAutumn
連結:https://www.jianshu.com/p/c852cbcf3d68
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯絡作者獲得授權並註明出處。