1、工作中个人使用工具类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import com.sitech.app.common.util.HttpClintUtils;
/**
* httpClinet网路请求工具类
* @author bjp-st-zhangbb
*
*/
public class HttpReqUtils {
private static final Logger LOGGER = Logger.getLogger(HttpReqUtils.class);
/**
* post请求(用于请求json格式的参数)
*/
public static String doPostJson(String url, String params){
HttpClient httpclient = new DefaultHttpClient();
if (url.contains("https")) {
HttpClintUtils.enableSSL((DefaultHttpClient)httpclient);
}
HttpPost httpPost = new HttpPost(url);// 创建httpPost
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
String charSet = "UTF-8";
String resline = null;
StringBuilder resbuilder = null;// 可变String
InputStream resinputStream = null;
BufferedReader reslineReader = null;
try {
StringEntity entity = new StringEntity(params, charSet);
httpPost.setEntity(entity);
HttpResponse response = null;
response = httpclient.execute(httpPost);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
LOGGER.info("doPostJson接口地址 === " + url);
LOGGER.info("doPostJson状态码 === " + state);
if (state == HttpStatus.SC_OK) {
resbuilder = new StringBuilder();
resinputStream = response.getEntity().getContent();
reslineReader = new BufferedReader(new InputStreamReader(resinputStream, "UTF-8"));// 对响应的结果以UTF-8编码
while ((resline = reslineReader.readLine()) != null) {
resbuilder.append(resline);
}
return resbuilder.toString();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
httpPost.abort();
}
return null;
}
/**
* post请求(用于请求k-v格式的参数)
*/
@SuppressWarnings("deprecation")
public static String doPostMap(String url, Map<String,String> params){
BufferedReader in = null;
// 定义HttpClient
HttpClient client = new DefaultHttpClient();
// 实例化HTTP方法
HttpPost request = new HttpPost();
try {
if (url.contains("https")) {
HttpClintUtils.enableSSL((DefaultHttpClient)client);
}
request.setURI(new URI(url));
if (params != null) {
//设置参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Iterator<String> iter = params.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String value = String.valueOf(params.get(name));
nvps.add(new BasicNameValuePair(name, value));
}
request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
}
HttpResponse response = client.execute(request);
int code = response.getStatusLine().getStatusCode();
LOGGER.info("doPostMap接口地址 === " + url);
LOGGER.info("doPostMap状态吗 === " + code);
if(code == 200){ //请求成功
in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent(),"utf-8"));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
return sb.toString();
}
else{
LOGGER.info("状态码:" + code);
return null;
}
}
catch(Exception e){
e.printStackTrace();
return null;
}finally{
request.abort();
}
}
/**
* get请求
*/
public static String doGet(String url) {
HttpClient client = new DefaultHttpClient();
if (url.contains("https")) {
HttpClintUtils.enableSSL((DefaultHttpClient)client);
}
//发送get请求
HttpGet request = new HttpGet(url);
byte[] bytes = null;
try {
HttpResponse response = client.execute(request);
int code = response.getStatusLine().getStatusCode();
LOGGER.info("doGet接口地址 === " + url);
LOGGER.info("doGet状态吗 === " + code);
if ( code== HttpStatus.SC_OK) {
bytes = EntityUtils.toByteArray(response.getEntity());
String strResult = new String(bytes, "utf-8");
return strResult;
}
}
catch (IOException e) {
e.printStackTrace();
}finally{
request.abort();
}
return null;
}
}
2、高并发下使用HttpClient分析
项目的原实现比较粗略,就是每次请求时初始化一个httpclient,生成一个httpPost对象,执行,然后从返回结果取出entity,保存成一个字符串,最后显式关闭response和client。我们一点点分析和优化。
- httpclient反复创建开销:httpclient是一个线程安全的类,没有必要由每个线程在每次使用时创建,全局保留一个即可。
- 反复创建tcp连接的开销:tcp的三次握手与四次挥手两大裹脚布过程,对于高频次的请求来说,消耗实在太大。试想如果每次请求我们需要花费5ms用于协商过程,那么对于qps为100的单系统,1秒钟我们就要花500ms用于握手和挥手。又不是高级领导,我们程序员就不要搞这么大做派了,改成keep alive方式以实现连接复用!
- 重复缓存entity的开销:原本的逻辑里,相当于额外复制了一份content到一个字符串里,而原本的httpResponse仍然保留了一份content,需要被consume掉,在高并发且content非常大的情况下,会消耗大量内存。
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity);
3、实现
按上面的分析,我们主要要做三件事:一是单例的client,二是缓存的保活连接,三是更好的处理返回结果。提到连接缓存,很容易联想到数据库连接池。httpclient4提供了一个PoolingHttpClientConnectionManager 作为连接池。
通过以下步骤来优化:
1、定义一个keep alive strategy:是否使用keep-alive要根据业务情况来定,而且http的keep-alive 和tcp的KEEPALIVE不是一个东西。
ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator
(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase
("timeout")) {
return Long.parseLong(value) * 1000;
}
}
return 60 * 1000;//如果没有约定,则默认定义时长为60s
}
};
2、 配置一个PoolingHttpClientConnectionManager:也可以针对每个路由设置并发数。
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(500);
connectionManager.setDefaultMaxPerRoute(50);//例如默认每路由最高50并发,具体依据业务来定
3、生成httpclient
httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setKeepAliveStrategy(kaStrategy)
.setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build())
.build();
注意:使用setStaleConnectionCheckEnabled方法来逐出已被关闭的链接不被推荐。更好的方式是手动启用一个线程,定时运行closeExpiredConnections 和closeIdleConnections方法,如下所示。
public static class IdleConnectionMonitorThread extends Thread {
private final HttpClientConnectionManager connMgr;
private volatile boolean shutdown;
public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
super();
this.connMgr = connMgr;
}
@Override
public void run() {
try {
while (!shutdown) {
synchronized (this) {
wait(5000);
// Close expired connections
connMgr.closeExpiredConnections();
// Optionally, close connections
// that have been idle longer than 30 sec
connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
}
}
} catch (InterruptedException ex) {
// terminate
}
}
public void shutdown() {
shutdown = true;
synchronized (this) {
notifyAll();
}
}
}
4、使用httpclient执行method时降低开销:这里要注意的是,不要关闭connection。一种可行的获取内容的方式类似于,把entity里的东西复制一份:
res = EntityUtils.toString(response.getEntity(),"UTF-8");
EntityUtils.consume(response1.getEntity());
但是,更推荐的方式是定义一个ResponseHandler,方便你我他,不再自己catch异常和关闭流。在此我们可以看一下相关的源码:
public <T> T execute(final HttpHost target, final HttpRequest request,
final ResponseHandler<? extends T> responseHandler, final HttpContext context)
throws IOException, ClientProtocolException {
Args.notNull(responseHandler, "Response handler");
final HttpResponse response = execute(target, request, context);
final T result;
try {
result = responseHandler.handleResponse(response);
} catch (final Exception t) {
final HttpEntity entity = response.getEntity();
try {
EntityUtils.consume(entity);
} catch (final Exception t2) {
// Log this exception. The original exception is more
// important and will be thrown to the caller.
this.log.warn("Error consuming content after an exception.", t2);
}
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
if (t instanceof IOException) {
throw (IOException) t;
}
throw new UndeclaredThrowableException(t);
}
// Handling the response was successful. Ensure that the content has
// been fully consumed.
final HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);//看这里看这里
return result;
}
可以看到,如果我们使用resultHandler执行execute方法,会最终自动调用consume方法,而这个consume方法如下所示:
public static void consume(final HttpEntity entity) throws IOException {
if (entity == null) {
return;
}
if (entity.isStreaming()) {
final InputStream instream = entity.getContent();
if (instream != null) {
instream.close();
}
}
}
可以看到最终它关闭了输入流
4、其他配置
通过以上步骤,基本就完成了一个支持高并发的httpclient的写法,下面是一些额外的配置和提醒。
1、httpclient的一些超时配置:CONNECTION_TIMEOUT是连接超时时间,SO_TIMEOUT是socket超时时间,这两者是不同的。连接超时时间是发起请求前的等待时间;socket超时时间是等待数据的超时时间。
HttpParams params = new BasicHttpParams();
//设置连接超时时间
Integer CONNECTION_TIMEOUT = 2 * 1000; //设置请求超时2秒钟 根据业务调整
Integer SO_TIMEOUT = 2 * 1000; //设置等待数据超时时间2秒钟 根据业务调整
//定义了当从ClientConnectionManager中检索ManagedClientConnection实例时使用的毫秒级的超时时间
//这个参数期望得到一个java.lang.Long类型的值。如果这个参数没有被设置,默认等于CONNECTION_TIMEOUT,因此一定要设置。
Long CONN_MANAGER_TIMEOUT = 500L; //在httpclient4.2.3中我记得它被改成了一个对象导致直接用long会报错,后来又改回来了
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT);
params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT);
//在提交请求之前 测试连接是否可用
params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);
//另外设置http client的重试次数,默认是3次;当前是禁用掉(如果项目量不到,这个默认即可)
httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
2、如果配置了nginx的话,nginx也要设置面向两端的keep-alive:nginx默认和client端打开长连接而和server端使用短链接。注意client端的keepalive_timeout和keepalive_requests参数,以及upstream端的keepalive参数设置,这三个参数的意义在此也不再赘述。