1、CloseableHttpClient的使用和优化(案例1)
https://blog.youkuaiyun.com/u010285974/article/details/85696239
2、HttpClient连接池设置引发的一次雪崩(案例2)
https://blog.youkuaiyun.com/u010889990/article/details/96236617
3、使用httpClient连接池处理get或post请求
https://m.wang1314.com/doc/webapp/topic/20923280.html
创建一个HttpClient
public class HttpClientUtil{
private static RequestConfig requestConfig = null;
//httpClient
private static final CloseableHttpClient httpClient;
private static final PoolingHttpClientConnectionManager cm;
//httpGet方法
// private static final HttpGet httpget;
//httpPost方法
// private static final HttpPost httpPost;
//响应处理器
// private static final ResponseHandler<String> responseHandler;
static{
//初始化HTTP请求配置,设置连接池请求、获取数据和建立连接超时时间
requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(40000)
.setSocketTimeout(40000)
.setConnectTimeout(40000)
.build();
// System.setProperty("http.maxConnections","50");//最大活跃连接数
// System.setProperty("http.keepAlive", "true");//长连接
//连接池配置
cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(500);//最大连接数
cm.setDefaultMaxPerRoute(100);//每个路由最大连接数
//创建定制http客户端
httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setRetryHandler(new DefaultHttpRequestRetryHandler(0,false))//设置http client的重试次数,默认是3次;当前是禁用掉(如果项目量不到,这个默认即可)
.build();
//初始化response解析器
// responseHandler = new BasicResponseHandler();
}
连接池配置连接数
1、方法一
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(500);//最大连接数
cm.setDefaultMaxPerRoute(100);//每个路由最大连接数
httpClient = HttpClients.custom().setConnectionManager(cm).build();
2、方法二
System.setProperty("http.maxConnections","50");//最大活跃连接数
httpClient = HttpClients.custom().useSystemProperties().build();
3、方法三
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setMaxConnTotal(500).setMaxConnPerRoute(100);
HttpClient httpClient = builder.build();