在背景發送http請求時,每次都要經過三次握手的過程,這是一個比較耗時的操作且穩定性不好,經常連接配接失敗。是以采用httpclient連接配接池,發起請求時直接從池裡面擷取連接配接,不用每次發起請求都經過三次握手,大大的提高的并發的效率
1.maven依賴
org.apache.httpcomponents
httpclient
4.5.2
com.alibaba
fastjson
1.2.3
2.http請求工具類
package com.litice.core.util;
import org.apache.http.HttpStatus;
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.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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.net.URLDecoder;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
public class HttpJsonUtil {
private static Logger logger = LoggerFactory.getLogger(HttpJsonUtil.class);
// 預設逾時時間:10s
private static final int TIME_OUT = 10 * 1000;
private static PoolingHttpClientConnectionManager cm = null;
static{
LayeredConnectionSocketFactory sslsf = null;
try{
sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault());
}catch(NoSuchAlgorithmException e){
logger.error("建立SSL連接配接失敗...");
}
Registry sRegistry = RegistryBuilder.create()
.register("https", sslsf)
.register("http", new PlainConnectionSocketFactory())
.build();
cm = new PoolingHttpClientConnectionManager(sRegistry);
// 設定最大的連接配接數
cm.setMaxTotal(200);
// 設定每個路由的基礎連接配接數【預設,每個路由基礎上的連接配接不超過2個,總連接配接數不能超過20】
cm.setDefaultMaxPerRoute(20);
}
private static CloseableHttpClient getHttpClient(){
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm).build();
return httpClient;
}
public static JSONObject httpGet(String url) {
JSONObject jsonResult = null;
CloseableHttpClient httpClient = getHttpClient();
// get請求傳回結果
CloseableHttpResponse response = null;
try {
// 配置請求逾時時間
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(TIME_OUT).setConnectionRequestTimeout(TIME_OUT)
.setSocketTimeout(TIME_OUT).build();
// 發送get請求
HttpGet request = new HttpGet(url);
request.setConfig(requestConfig);
response = httpClient.execute(request);
// 請求發送成功,并得到響應
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 讀取伺服器傳回過來的json字元串資料
String strResult = EntityUtils.toString(response.getEntity());
// 把json字元串轉換成json對象
jsonResult = JSONObject.parseObject(strResult);
url = URLDecoder.decode(url, "UTF-8");
} else {
logger.error("get請求送出失敗:" + url);
}
} catch (IOException e) {
logger.error("get請求送出失敗:" + url, e);
} finally {
if( response != null){
try {
EntityUtils.consume(response.getEntity());
response.close();
} catch (IOException e) {
logger.error("關閉response失敗:", e);
}
}
}
return jsonResult;
}
}
```