緊接上一節(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è)試和排查,確定問題在于兩方面:
-
httpClient客戶端連接對(duì)象并沒有配置連接池,使用默認(rèn)的連接池不能支持那么多的并發(fā)量。 - 沒有給每一次的連接設(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é)記錄【連接池的配置】