前言
Http連接需要的三次握手開銷很大, 傳統的HttpURLConnection並不支持連接池, HTTP1.1以上默認開啓keepalive, 對於現在springcloud橫行的社會, feign可以配置好http連接池, 不過總會有某些個別的接口不在服務中, 還有一些非springboot的老舊項目也要加入cloud大家族中, 對於內部頻繁訪問的url地址, 這就需要一款量身定做的工具類了.
講解
先來看看測試效果, 兩種工具類的時間消耗對比.
這個是測試用的方法, 前後分別調用了某一接口100次, 進行時間統計, 系統頁面按F12能看到平均握手速度大概是3ms
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
result = commonManager.getPolicyGoNew(inputStr);
}
long end = System.currentTimeMillis();
System.out.println(String.valueOf(end - start));
System.out.println("***********");
long start2 = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
result = commonManager.getPolicyGo(inputStr);
}
long end2 = System.currentTimeMillis();
System.out.println(String.valueOf(end2 - start2));
100次快了647ms, 平均一次6ms, 握三次手大概要這麼久吧
9612
***********
10259
第二次我調換了順序, 500.for, 500次快了2341ms, 平均一次4.6ms, 更接近3ms了
50906
***********
48565
52327
***********
47021
錯誤的請求數據, 可以立馬返回數據, 更能說明是否節省了握手時間
9625
***********
8868
9251
***********
8718
不過我之前都是調用的生產環境的接口, 效率還是比較高的, 下面我在uat環境測試一下
正常查詢詳情數據消耗時間
500次快了4900ms, 平均一次10ms
72712
***********
67839
錯誤的請求數據, 可以立馬返回數據, 更能說明是否節省了握手時間
500次快了2400ms, 平均一次4.8ms
6439
***********
4063
代碼
原httputil, 看實現部分就好
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.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.util.Map;
public class HttpUtils {
public interface ResponseCallback {
void onResponse(CloseableHttpResponse response) throws Exception;
void onError(int resultCode, String resultStr) throws Exception;
}
public static void headerPost(String urlStr, String data, ResponseCallback callback, Map<String, String> headerMap) throws Exception {
doPost(urlStr, data, callback, headerMap, 300000);
}
private static void doPost(String urlStr, String data, ResponseCallback callback, Map<String, String> headerMap, int timeOut) throws Exception {
HttpUriRequest request = null;
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
request = new HttpPost(urlStr);
if (headerMap != null) {
for(Map.Entry<String, String> entry : headerMap.entrySet()){
request.setHeader(entry.getKey(), entry.getValue());
}
}
if (null != data && !"".equals(data)) {
((HttpPost) request).setEntity(new ByteArrayEntity(data.getBytes("utf-8")));
}
RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(timeOut).setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();
client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
response = client.execute(request);
int resultCode = response.getStatusLine().getStatusCode();
if (resultCode == HttpStatus.SC_OK) {
callback.onResponse(response);
} else {
throw new Exception(resultCode + ":system error");
}
} catch (Exception e) {
callback.onError(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.toString());
} finally {
if (request != null && !request.isAborted()) {
request.abort();
request = null;
}
if (client != null) {
HttpClientUtils.closeQuietly(client);
client = null;
}
if (response != null) {
HttpClientUtils.closeQuietly(response);
response = null;
}
}
}
}
帶連接池的httputil, 初版支持一個url連接, 可更改爲Map維護
import org.apache.http.*;
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.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.routing.HttpRoute;
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.entity.ByteArrayEntity;
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.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.util.Map;
public class HttpClientUtil {
static final int timeOut = 10 * 1000;
private static CloseableHttpClient httpClient = null;
private final static Object syncLock = new Object();
private static void config(HttpRequestBase httpRequestBase) {
// 配置請求的超時設置
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(timeOut)
.setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();
httpRequestBase.setConfig(requestConfig);
}
/**
* 獲取HttpClient對象
*/
public static CloseableHttpClient getHttpClient(String url) {
String hostname = url.split("/")[2];
int port = 80;
if (hostname.contains(":")) {
String[] arr = hostname.split(":");
hostname = arr[0];
port = Integer.parseInt(arr[1]);
}
if (httpClient == null) {
synchronized (syncLock) {
if (httpClient == null) {
httpClient = createHttpClient(200, 40, 100, hostname, port);
}
}
}
return httpClient;
}
public static CloseableHttpClient createHttpClient(int maxTotal, int maxPerRoute, int maxRoute, String hostname, int port) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder
.<ConnectionSocketFactory> create().register("http", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
// 將最大連接數增加
cm.setMaxTotal(maxTotal);
// 將每個路由基礎的連接增加
cm.setDefaultMaxPerRoute(maxPerRoute);
HttpHost httpHost = new HttpHost(hostname, port);
// 將目標主機的最大連接數增加
cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
// 請求重試處理
HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 5) {// 如果已經重試了5次,就放棄
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服務器丟掉了連接,那麼就重試
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重試SSL握手異常
return false;
}
if (exception instanceof InterruptedIOException) {// 超時
return false;
}
if (exception instanceof UnknownHostException) {// 目標服務器不可達
return false;
}
if (exception instanceof ConnectTimeoutException) {// 連接被拒絕
return false;
}
if (exception instanceof SSLException) {// SSL握手異常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果請求是冪等的,就再次嘗試
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
};
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setRetryHandler(httpRequestRetryHandler).build();
return httpClient;
}
public static String post(String url, String data, Map<String, String> headerMap) throws IOException {
HttpPost httppost = new HttpPost(url);
config(httppost);
if (headerMap != null) {
for(Map.Entry<String, String> entry : headerMap.entrySet()){
httppost.setHeader(entry.getKey(), entry.getValue());
}
}
if (null != data && !"".equals(data)) {
httppost.setEntity(new ByteArrayEntity(data.getBytes("utf-8")));
}
CloseableHttpResponse response = null;
try {
response = getHttpClient(url).execute(httppost, HttpClientContext.create());
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
//EntityUtils.consume(entity);
return result;
} catch (Exception e) {
throw e;
} finally {
try {
if (response != null)
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String get(String url) {
HttpGet httpget = new HttpGet(url);
config(httpget);
CloseableHttpResponse response = null;
try {
response = getHttpClient(url).execute(httpget,
HttpClientContext.create());
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
return result;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (response != null)
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
總結
不管怎麼來回測試, 都有時間上的差別, 連接池明顯要快一些, 而且對服務器的資源消耗也小, 對接口提供者也比較友好, 當然這一切有個前提, 就是雙方都是http1.1以上, 默認開啓keepAlive, 畢竟http2.0協議還沒有太普及.