HttpClient 4.5.2-(四)連接超時(shí)的配置

緊接上一節(jié),本節(jié)記錄 【連接超時(shí)的配置】
  工作中有這樣一個(gè)需求,要對(duì)上千臺(tái)的服務(wù)器進(jìn)行更新測(cè)試,這是需要開幾百個(gè)線程做并發(fā)測(cè)試,每個(gè)線程中都有N次的Http請(qǐng)求,看服務(wù)器是否可以頂?shù)米?/code>。但是碰到一個(gè)問題,那就是訪問的過程中有些線程莫名其妙就不訪問了,觀察服務(wù)器端,并沒有請(qǐng)求進(jìn)來。開始各種懷疑,是線程自己死掉了?還是Windows對(duì)每個(gè)進(jìn)程有線程的限制?亦或者是HttpClient對(duì)總連接數(shù)量有限制?
  最后經(jīng)過每一步的測(cè)試和排查,確定問題在于兩方面:

  1. httpClient客戶端連接對(duì)象并沒有配置連接池,使用默認(rèn)的連接池不能支持那么多的并發(fā)量。
  2. 沒有給每一次的連接設(shè)置超時(shí)時(shí)間獲取連接超時(shí) 請(qǐng)求超時(shí) 響應(yīng)超時(shí),如果沒有設(shè)置超時(shí)時(shí)間,連接可能會(huì)一直存在阻塞,所以線程一直停在那里,其實(shí)線程并沒有死掉。
也就是說,我們只需把上述兩個(gè)問題解決問題就迎刃而解了。本節(jié)先說連接超時(shí)時(shí)間的設(shè)置。

上代碼:
package com.lynchj.writing;

/**
 * Http請(qǐng)求工具類
 * 
 * @author 大漠知秋
 */
public class HttpRequestUtils {
    
}
  • 獲取帶超時(shí)間的httpClient客戶端連接對(duì)象
/**
 * 獲取Http客戶端連接對(duì)象
 * 
 * @param timeOut 超時(shí)時(shí)間
 * @return Http客戶端連接對(duì)象
 */
public static HttpClient getHttpClient(Integer timeOut) {
    // 創(chuàng)建Http請(qǐng)求配置參數(shù)
    RequestConfig requestConfig = RequestConfig.custom()
        // 獲取連接超時(shí)時(shí)間
        .setConnectionRequestTimeout(timeOut)
        // 請(qǐng)求超時(shí)時(shí)間
        .setConnectTimeout(timeOut)
        // 響應(yīng)超時(shí)時(shí)間
        .setSocketTimeout(timeOut)
        .build();
    
    // 創(chuàng)建httpClient
    return HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
}
  • POST請(qǐng)求方法
/**
 * GET請(qǐng)求
 * 
 * @param url 請(qǐng)求地址
 * @param timeOut 超時(shí)時(shí)間
 * @return
 */
public static String httpGet(String url, Integer timeOut) {
    String msg = "-1";
    
    // 獲取客戶端連接對(duì)象
    CloseableHttpClient httpClient = getHttpClient(timeOut);
    // 創(chuàng)建GET請(qǐng)求對(duì)象
    HttpGet httpGet = new HttpGet(url);
    
    CloseableHttpResponse response = null;
    
    try {
        // 執(zhí)行請(qǐng)求
        response = httpClient.execute(httpGet);
        // 獲取響應(yīng)實(shí)體
        HttpEntity entity = response.getEntity();
        // 獲取響應(yīng)信息
        msg = EntityUtils.toString(entity, "UTF-8");
    } catch (ClientProtocolException e) {
        System.err.println("協(xié)議錯(cuò)誤");
        e.printStackTrace();
    } catch (ParseException e) {
        System.err.println("解析錯(cuò)誤");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IO錯(cuò)誤");
        e.printStackTrace();
    } finally {
        if (null != response) {
            try {
                response.close();
            } catch (IOException e) {
                System.err.println("釋放鏈接錯(cuò)誤");
                e.printStackTrace();
            }
        }
    }
    
    return msg;
}
  • 測(cè)試main方法
public static void main(String[] args) {
        
    System.out.println(httpGet("http://www.baidu.com", 6000));
    
}

經(jīng)多次測(cè)試結(jié)果,發(fā)現(xiàn)如果僅僅這是這么配置的話,還是會(huì)存在設(shè)置超時(shí)時(shí)間不起作用的情況,最后排查結(jié)果

  • 經(jīng)過修改后的獲取客戶端連接工具的方法
/**
 * 獲取Http客戶端連接對(duì)象
 * 
 * @param timeOut 超時(shí)時(shí)間
 * @return Http客戶端連接對(duì)象
 */
public static CloseableHttpClient getHttpClient(Integer timeOut) {
    // 創(chuàng)建Http請(qǐng)求配置參數(shù)
    RequestConfig requestConfig = RequestConfig.custom()
        // 獲取連接超時(shí)時(shí)間
        .setConnectionRequestTimeout(timeOut)
        // 請(qǐng)求超時(shí)時(shí)間
        .setConnectTimeout(timeOut)
        // 響應(yīng)超時(shí)時(shí)間
        .setSocketTimeout(timeOut)
        .build();
    
    /**
     * 測(cè)出超時(shí)重試機(jī)制為了防止超時(shí)不生效而設(shè)置
     *  如果直接放回false,不重試
     *  這里會(huì)根據(jù)情況進(jìn)行判斷是否重試
     */
    HttpRequestRetryHandler retry = new HttpRequestRetryHandler() {
        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= 3) {// 如果已經(jīng)重試了3次,就放棄
                return false;
            }
            if (exception instanceof NoHttpResponseException) {// 如果服務(wù)器丟掉了連接,那么就重試
                return true;
            }
            if (exception instanceof SSLHandshakeException) {// 不要重試SSL握手異常
                return false;
            }
            if (exception instanceof InterruptedIOException) {// 超時(shí)
                return true;
            }
            if (exception instanceof UnknownHostException) {// 目標(biāo)服務(wù)器不可達(dá)
                return false;
            }
            if (exception instanceof ConnectTimeoutException) {// 連接被拒絕
                return false;
            }
            if (exception instanceof SSLException) {// ssl握手異常
                return false;
            }
            HttpClientContext clientContext = HttpClientContext.adapt(context);
            HttpRequest request = clientContext.getRequest();
            // 如果請(qǐng)求是冪等的,就再次嘗試
            if (!(request instanceof HttpEntityEnclosingRequest)) {
                return true;
            }
            return false;
        }
    };
    
    // 創(chuàng)建httpClient
    return HttpClients.custom()
            // 把請(qǐng)求相關(guān)的超時(shí)信息設(shè)置到連接客戶端
            .setDefaultRequestConfig(requestConfig)
            // 把請(qǐng)求重試設(shè)置到連接客戶端
            .setRetryHandler(retry)
            .build();
}
  • 最后完整代碼
package com.lynchj.writing;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;

import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

/**
 * Http請(qǐng)求工具類
 * 
 * @author 大漠知秋
 */
public class HttpRequestUtils {
    
    /**
     * 獲取Http客戶端連接對(duì)象
     * 
     * @param timeOut 超時(shí)時(shí)間
     * @return Http客戶端連接對(duì)象
     */
    public static CloseableHttpClient getHttpClient(Integer timeOut) {
        // 創(chuàng)建Http請(qǐng)求配置參數(shù)
        RequestConfig requestConfig = RequestConfig.custom()
            // 獲取連接超時(shí)時(shí)間
            .setConnectionRequestTimeout(timeOut)
            // 請(qǐng)求超時(shí)時(shí)間
            .setConnectTimeout(timeOut)
            // 響應(yīng)超時(shí)時(shí)間
            .setSocketTimeout(timeOut)
            .build();
        
        /**
         * 測(cè)出超時(shí)重試機(jī)制為了防止超時(shí)不生效而設(shè)置
         *  如果直接放回false,不重試
         *  這里會(huì)根據(jù)情況進(jìn)行判斷是否重試
         */
        HttpRequestRetryHandler retry = new HttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                if (executionCount >= 3) {// 如果已經(jīng)重試了3次,就放棄
                    return false;
                }
                if (exception instanceof NoHttpResponseException) {// 如果服務(wù)器丟掉了連接,那么就重試
                    return true;
                }
                if (exception instanceof SSLHandshakeException) {// 不要重試SSL握手異常
                    return false;
                }
                if (exception instanceof InterruptedIOException) {// 超時(shí)
                    return true;
                }
                if (exception instanceof UnknownHostException) {// 目標(biāo)服務(wù)器不可達(dá)
                    return false;
                }
                if (exception instanceof ConnectTimeoutException) {// 連接被拒絕
                    return false;
                }
                if (exception instanceof SSLException) {// ssl握手異常
                    return false;
                }
                HttpClientContext clientContext = HttpClientContext.adapt(context);
                HttpRequest request = clientContext.getRequest();
                // 如果請(qǐng)求是冪等的,就再次嘗試
                if (!(request instanceof HttpEntityEnclosingRequest)) {
                    return true;
                }
                return false;
            }
        };
        
        // 創(chuàng)建httpClient
        return HttpClients.custom()
                // 把請(qǐng)求相關(guān)的超時(shí)信息設(shè)置到連接客戶端
                .setDefaultRequestConfig(requestConfig)
                // 把請(qǐng)求重試設(shè)置到連接客戶端
                .setRetryHandler(retry)
                .build();
    }
    
    /**
     * GET請(qǐng)求
     * 
     * @param url 請(qǐng)求地址
     * @param timeOut 超時(shí)時(shí)間
     * @return
     */
    public static String httpGet(String url, Integer timeOut) {
        String msg = "-1";
        
        // 獲取客戶端連接對(duì)象
        CloseableHttpClient httpClient = getHttpClient(timeOut);
        // 創(chuàng)建GET請(qǐng)求對(duì)象
        HttpGet httpGet = new HttpGet(url);
        
        CloseableHttpResponse response = null;
        
        try {
            // 執(zhí)行請(qǐng)求
            response = httpClient.execute(httpGet);
            // 獲取響應(yīng)實(shí)體
            HttpEntity entity = response.getEntity();
            // 獲取響應(yīng)信息
            msg = EntityUtils.toString(entity, "UTF-8");
        } catch (ClientProtocolException e) {
            System.err.println("協(xié)議錯(cuò)誤");
            e.printStackTrace();
        } catch (ParseException e) {
            System.err.println("解析錯(cuò)誤");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO錯(cuò)誤");
            e.printStackTrace();
        } finally {
            if (null != response) {
                try {
                    EntityUtils.consume(response.getEntity());
                    response.close();
                } catch (IOException e) {
                    System.err.println("釋放鏈接錯(cuò)誤");
                    e.printStackTrace();
                }
            }
        }
        
        return msg;
    }
    
    public static void main(String[] args) {
        
        System.out.println(httpGet("http://www.baidu.com", 6000));
        
    }
    
}

至此,本節(jié)完畢,下一節(jié)記錄【連接池的配置】

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評(píng)論 19 139
  • 第一章 Nginx簡(jiǎn)介 Nginx是什么 沒有聽過Nginx?那么一定聽過它的“同行”Apache吧!Ngi...
    JokerW閱讀 33,017評(píng)論 24 1,002
  • 本來是個(gè)簡(jiǎn)單工具的使用,沒必要寫什么博客的,但是StartUML有幾個(gè)地方在畫圖的時(shí)候和別的工具(rose)不太一...
    千山萬水迷了鹿閱讀 4,350評(píng)論 0 1
  • 《風(fēng)雨中的菊花》閱讀題 風(fēng)雨中的菊花 午后的天灰蒙蒙的,烏云壓得很低,似乎要下雨。 多爾先生情緒很低落,他最煩在這...
    躲進(jìn)小樓看燈火閱讀 11,680評(píng)論 0 0
  • (寫在感恩節(jié)) 夜晚三點(diǎn)半的鬧鐘響得實(shí)兀 黑夜倦曲在冰涼的馬路 對(duì)樓的窗戶閃耀著燈光 誰(shuí)家嬰孩半夜啼哭 星星眨著節(jié)...
    山上人家123閱讀 237評(píng)論 5 12

友情鏈接更多精彩內(nèi)容