httpclient高频请求
2018-10-07 本文已影响0人
eblly
1、场景
有某个业务需要使用https请求内部服务。发现服务器下载宽带占用达到30M/s。大大超出宽带可承受的范围。
2、分析
通过抓包发现TLS的证书占用很大,并且每次tcp都需要申请和获取证书。
http每次请求都需要三次握手和四次挥手,如果是高频请求的话,反复创建和销毁是十分耗时的。https因为还需要申请证书更是耗时。
3、实现
package com.world.util;
import org.apache.http.*;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.util.*;
/**
* HttpClient工具类
*/
public class HttpPoolKit {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(HttpPoolKit.class);
private static int timeOut = 10 * 1000;
private static int connectTimeOut = 10 * 1000;
private static int socketTimeOut = 10 * 1000;
private static int maxTotal = 100;
private static int maxPerRoute = 10;
private static int maxRoute = 10;
private static CloseableHttpClient httpClient = null;
private final static Object syncLock = new Object();
static {
InputStream stream = HttpPoolKit.class.getClassLoader().getResourceAsStream("main.properties");
Properties props = new Properties();
try {
props.load(stream);
timeOut = Integer.valueOf(props.getProperty("timeOut"));
connectTimeOut = Integer.valueOf(props.getProperty("connectTimeOut"));
socketTimeOut = Integer.valueOf(props.getProperty("socketTimeOut"));
maxTotal = Integer.valueOf(props.getProperty("maxTotal"));
maxPerRoute = Integer.valueOf(props.getProperty("maxPerRoute"));
maxRoute = Integer.valueOf(props.getProperty("maxRoute"));
} catch (Exception e) {
log.error("load properties error", e);
}
}
private static void config(HttpRequestBase httpRequestBase) {
// 设置Header等
// httpRequestBase.setHeader("User-Agent", "Mozilla/5.0");
// httpRequestBase.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// httpRequestBase.setHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");// "en-US,en;q=0.5");
// httpRequestBase.setHeader("Accept-Charset","ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7");
// 配置请求的超时设置
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(timeOut) // 连接请求超时设置
.setConnectTimeout(connectTimeOut) // 连接超时设置
.setSocketTimeout(socketTimeOut) // socket通信超时设置
.build();
httpRequestBase.setConfig(requestConfig);
}
/**
* 获取HttpClient对象
*
* @return
*/
public static CloseableHttpClient getHttpClient(String url) {
// https://www.aaa.com/bbb/ccc http://www.aaa.com/bbb/ccc aaa.com/bbb/ccc
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(maxTotal, maxPerRoute, maxRoute, hostname, port);
}
}
}
return httpClient;
}
/**
* 创建HttpClient对象
*
* @param maxTotal
* @param maxPerRoute
* @param maxRoute
* @param hostname
* @param port
* @return
*/
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() {
@Override
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;
}
/**
* @param httpost
* @param params
*/
private static void setPostParams(HttpPost httpost,
Map<String, Object> params) {
List<NameValuePair> nvps = new ArrayList<>();
Set<String> keySet = params.keySet();
for (String key : keySet) {
nvps.add(new BasicNameValuePair(key, params.get(key).toString()));
}
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
} catch (UnsupportedEncodingException e) {
log.error("error", e);
}
}
/**
* POST请求URL获取内容
*
* @param url
* @return
* @throws IOException
*/
public static String post(String url, Map<String, Object> params) throws IOException {
HttpPost httppost = new HttpPost(url);
config(httppost);
setPostParams(httppost, params);
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) {
log.error("error", e);
throw e;
} finally {
try {
if (response != null)
response.close();
} catch (IOException e) {
log.error("error", e);
}
}
}
/**
* GET请求URL获取内容
*
* @param url
* @return
*/
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) {
log.error("error", e);
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
log.error("error", e);
}
}
return null;
}
}
测试代码
package com.eblly.network;
import com.eblly.util.HttpUtil;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 测试http请求效率
*
* @author eblly
* @since 2018/10/3
*/
public class MultiHttpTs_2 {
@Test
public void httpTs() throws InterruptedException {
request("测试4-1", 1000, 100);
request("测试4-2", 10000, 100);
// request("测试4-3", 100000, 100);
//不使用http连接池
//测试4-1 ==>耗时: 3697.0毫秒
//测试4-2 ==>耗时: 30483.0毫秒
//使用http连接池
//测试4-1 ==>耗时: 2023.0毫秒
//测试4-2 ==>耗时: 10909.0毫秒
}
/**
* @param msg
* @param requestSize
* @param threadSize
* @throws InterruptedException
*/
private void request(String msg, int requestSize, int threadSize) throws InterruptedException {
CountDownLatch countDownLatch = null;
AtomicInteger atomicInteger = null;
countDownLatch = new CountDownLatch(requestSize);
atomicInteger = new AtomicInteger(requestSize);
long startTime = System.currentTimeMillis();
ExecutorService executorService = Executors.newFixedThreadPool(threadSize);
//使用线程池
for (int i = 0; i < threadSize; i++) {
executorService.execute(new DownRunnable(countDownLatch, atomicInteger));
}
countDownLatch.await();
long endTime = System.currentTimeMillis();
double time = (endTime - startTime);
System.out.println(msg + " ==>耗时: " + time + "毫秒");
}
/**
* 下载线程
*/
private class DownRunnable implements Runnable {
private CountDownLatch countDownLatch;
private AtomicInteger atomicInteger;
public static final String host = "https://www.baidu.com";
public DownRunnable(CountDownLatch countDownLatch, AtomicInteger atomicInteger) {
super();
this.countDownLatch = countDownLatch;
this.atomicInteger = atomicInteger;
}
@Override
public void run() {
try {
while (atomicInteger.decrementAndGet() >= 0) {
try {
// String reposne = HttpUtil.get(host, "");
String reposne = HttpClientUtil.get(host);
} catch (Exception e) {
e.printStackTrace();
}
countDownLatch.countDown();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
测试结果
//不使用http连接池
//测试4-1 ==>耗时: 3697.0毫秒
//测试4-2 ==>耗时: 30483.0毫秒
//使用http连接池
//测试4-1 ==>耗时: 2023.0毫秒
//测试4-2 ==>耗时: 10909.0毫秒
3.1、对比
未使用http连接池未使用http连接池,客户端重复向服务端请求证书。
使用http连接池使用http连接池,不再重复申请证书。