关键参数都进行了注释,具备链接自动关闭,超时设置,连接池等功能。个人备忘
httpclient版本为4.5.13,这是修改后的最终版本
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.collections4.MapUtils;
import org.apache.http.HttpEntity;
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.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.core.MediaType;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* 基于双重检查单例的HttpClient4连接池
*/
public class HttpClientUtil {
private static final Logger log = LoggerFactory.getLogger(HttpClientUtil.class);
private static volatile CloseableHttpClient httpClient = null;
private HttpClientUtil() {
}
private static CloseableHttpClient getHttpClient() {
if (Objects.isNull(httpClient)) {
synchronized (HttpClientUtil.class) {
if (Objects.isNull(httpClient)) {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
// 连接池总容量
connectionManager.setMaxTotal(100);
// 每个host为一组,此参数用于控制每组中连接池的容量 【重要】
connectionManager.setDefaultMaxPerRoute(10);
RequestConfig requestConfig = RequestConfig.custom()
// 从连接池中取连接超时时间
.setConnectionRequestTimeout(5000)
// 建立链接超时时间
.setConnectTimeout(5000)
// 等待读取数据时间
.setSocketTimeout(20000)
.build();
httpClient = HttpClientBuilder.create()
// 关闭自动重试
.disableAutomaticRetries()
// 设置连接池
.setConnectionManager(connectionManager)
// setConnectionTimeToLive(2, TimeUnit.MINUTES) 设置链接最大存活时间 此属性无效 注意
// 回收空闲超过2分钟的链接
.evictIdleConnections(2, TimeUnit.MINUTES)
// 设置超时时间
.setDefaultRequestConfig(requestConfig)
.build();
}
}
}
return httpClient;
}
/**
* 不带header参数的JSON格式Post请求
*
* @param url 请求URL
* @param bodyJson body参数
* @return 响应体
*/
public static String httpPost(String url, JSONObject bodyJson) {
return httpPost(url, null, bodyJson);
}
/**
* 携带header参数的JSON格式Post请求
*
* @param url 请求URL
* @param headerParam header参数
* @param bodyJson body参数
* @return 响应体
*/
public static String httpPost(String url, Map<String, String> headerParam, JSONObject bodyJson) {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", MediaType.APPLICATION_JSON);
// 组装header
if (MapUtils.isNotEmpty(headerParam)) {
for (String key : headerParam.keySet()) {
httpPost.addHeader(key, headerParam.get(key));
}
}
// 组装body
if (Objects.nonNull(bodyJson)) {
StringEntity stringEntity = new StringEntity(bodyJson.toJSONString(), StandardCharsets.UTF_8);
stringEntity.setContentType(MediaType.APPLICATION_JSON);
stringEntity.setContentEncoding(StandardCharsets.UTF_8.name());
httpPost.setEntity(stringEntity);
}
// 不要关闭client,会导致连接池被关闭
CloseableHttpClient client = getHttpClient();
try (CloseableHttpResponse response = client.execute(httpPost)) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, StandardCharsets.UTF_8.name());
EntityUtils.consume(entity);
return result;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
/**
* Get请求
*
* @param url 请求URL
* @return 响应体
*/
public static String httpGet(String url) {
return httpGet(url, null, null);
}
/**
* 携带url参数的Get请求
*
* @param url 请求URL
* @param urlParam url参数
* @return 响应体
*/
public static String httpGet(String url, Map<String, String> urlParam) {
return httpGet(url, null, urlParam);
}
/**
* 携带header参数与url参数的Get请求
*
* @param url 请求URL
* @param headerParam header参数
* @param urlParam url参数
* @return 响应体
*/
public static String httpGet(String url, Map<String, String> headerParam, Map<String, String> urlParam) {
HttpGet httpGet = new HttpGet();
// 组装header
if (MapUtils.isNotEmpty(headerParam)) {
for (String key : headerParam.keySet()) {
httpGet.addHeader(key, headerParam.get(key));
}
}
try {
// 组装url参数
URIBuilder uriBuilder = new URIBuilder(url);
if (MapUtils.isNotEmpty(urlParam)) {
for (String key : urlParam.keySet()) {
uriBuilder.addParameter(key, urlParam.get(key));
}
}
httpGet.setURI(uriBuilder.build());
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
// 不要关闭client,会导致连接池被关闭
CloseableHttpClient client = getHttpClient();
try (CloseableHttpResponse response = client.execute(httpGet)) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, StandardCharsets.UTF_8.name());
EntityUtils.consume(entity);
return result;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
}
附基于httpclient 5.1.4版本的实现
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.net.URIBuilder;
import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
import org.apache.hc.core5.pool.PoolReusePolicy;
import org.apache.hc.core5.util.TimeValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* 基于双重检查单例的HttpClient5连接池
*/
public class HttpClientUtil {
private static volatile CloseableHttpClient httpClient = null;
private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
private HttpClientUtil() {
}
private static CloseableHttpClient getHttpClient() {
if (Objects.isNull(httpClient)) {
synchronized (HttpClientUtil.class) {
if (Objects.isNull(httpClient)) {
RequestConfig requestConfig = RequestConfig.custom()
// 从链接池获取链接超时时间
.setConnectionRequestTimeout(10, TimeUnit.SECONDS)
// 链接建立超时时间
.setConnectTimeout(10, TimeUnit.SECONDS)
// 数据读取超时时间
.setResponseTimeout(1, TimeUnit.MINUTES).build();
PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
// 池子总链接数
.setMaxConnTotal(100)
// 每个 ip:port 为一个route,设置每个route的最大链接数
.setMaxConnPerRoute(20)
// 松策略,高负载时会超过上述限制
.setPoolConcurrencyPolicy(PoolConcurrencyPolicy.LAX)
// 平等使用已有链接,尽量避免其过期
.setConnPoolPolicy(PoolReusePolicy.LIFO)
// 连接池中链接生存时间
.setConnectionTimeToLive(TimeValue.ofMinutes(5))
// 构建
.build();
httpClient = HttpClientBuilder.create()
// 关闭身份验证缓存
.disableAuthCaching()
// 关闭自动重试
.disableAutomaticRetries()
// 配置链接池
.setConnectionManager(connectionManager)
// 配置链接
.setDefaultRequestConfig(requestConfig)
// 构建
.build();
}
}
}
return httpClient;
}
/**
* 携带header参数与url参数的Get请求
*
* @param url 请求URL
* @param headerParam header参数
* @param urlParam url参数
* @return 响应体
*/
public static String doGet(String url, Map<String, String> headerParam, Map<String, Object> urlParam) {
try {
// 拼装url参数
URIBuilder uriBuilder = new URIBuilder(url);
if (MapUtils.isNotEmpty(urlParam)) {
for (String key : urlParam.keySet()) {
uriBuilder.addParameter(key, String.valueOf(urlParam.get(key)));
}
}
// 拼装header参数
HttpGet httpGet = new HttpGet(uriBuilder.build());
if (MapUtils.isNotEmpty(headerParam)) {
for (String key : headerParam.keySet()) {
httpGet.addHeader(key, headerParam.get(key));
}
}
// 不要关闭client,会导致连接池被关闭
CloseableHttpClient client = getHttpClient();
try (CloseableHttpResponse response = client.execute(httpGet)) {
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
/**
* 携带header参数的post请求
*
* @param url 请求URL
* @param headerParam header参数
* @param bodyJsonString json字符串body参数
* @return 响应体
*/
public static String doPost(String url, Map<String, String> headerParam, String bodyJsonString) {
try {
HttpPost httpPost = new HttpPost(url);
if (MapUtils.isNotEmpty(headerParam)) {
for (String key : headerParam.keySet()) {
httpPost.addHeader(key, headerParam.get(key));
}
}
httpPost.setEntity(new StringEntity(StringUtils.isBlank(bodyJsonString) ? "{}" : bodyJsonString, ContentType.APPLICATION_JSON));
// 不要关闭client,会导致连接池被关闭
CloseableHttpClient client = getHttpClient();
try (CloseableHttpResponse response = client.execute(httpPost)) {
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
/**
* 携带header参数的put请求
*
* @param url 请求URL
* @param headerParam header参数
* @param bodyJsonString json字符串body参数
* @return 响应体
*/
public static String doPut(String url, Map<String, String> headerParam, String bodyJsonString) {
try {
HttpPut httpPut = new HttpPut(url);
if (MapUtils.isNotEmpty(headerParam)) {
for (String key : headerParam.keySet()) {
httpPut.addHeader(key, headerParam.get(key));
}
}
httpPut.setEntity(new StringEntity(StringUtils.isBlank(bodyJsonString) ? "{}" : bodyJsonString, ContentType.APPLICATION_JSON));
// 不要关闭client,会导致连接池被关闭
CloseableHttpClient client = getHttpClient();
try (CloseableHttpResponse response = client.execute(httpPut)) {
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
/**
* 携带header参数的delete请求
*
* @param url 请求URL
* @param headerParam header参数
* @return 响应体
*/
public static String doDelete(String url, Map<String, String> headerParam) {
try {
HttpDelete httpDelete = new HttpDelete(url);
if (MapUtils.isNotEmpty(headerParam)) {
for (String key : headerParam.keySet()) {
httpDelete.addHeader(key, headerParam.get(key));
}
}
// 不要关闭client,会导致连接池被关闭
CloseableHttpClient client = getHttpClient();
try (CloseableHttpResponse response = client.execute(httpDelete)) {
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
}
UPDATE 20220630
// 设置链接最大存活时间
HttpClientBuilder.create().setConnectionTimeToLive(2, TimeUnit.MINUTES)
经测试,setConnectionTimeToLive
参数不会自动回收空闲连接,具体此项作用待查。
正确的链接自动回收应该这样设置
// 设置连接自动回收
CloseableHttpClient httpClient = HttpClientBuilder.create().evictIdleConnections(2, TimeUnit.MINUTES).build();
以下是验证过程
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws IOException, InterruptedException {
SpringApplication.run(DemoApplication.class, args);
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
// 连接池总容量
connectionManager.setMaxTotal(5);
// 每个host为一组,此参数用于控制每组中连接池的容量 【重要】
connectionManager.setDefaultMaxPerRoute(10);
RequestConfig requestConfig = RequestConfig.custom()
// 从连接池中取连接超时时间
.setConnectionRequestTimeout(15000)
// 建立链接超时时间
.setConnectTimeout(150000000)
// 等待读取数据时间
.setSocketTimeout(2000000000).build();
CloseableHttpClient httpClient = HttpClientBuilder.create()
// 关闭自动重试
.disableAutomaticRetries()
// 设置连接池
.setConnectionManager(connectionManager)
// 设置超时时间
.setDefaultRequestConfig(requestConfig)
// 设置连接自动回收
.evictIdleConnections(2, TimeUnit.SECONDS)
.build();
new Thread(() -> {
do {
System.out.println(LocalDateTime.now() + connectionManager.getTotalStats().toString());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} while (true);
}).start();
CloseableHttpResponse response = httpClient.execute(new HttpGet("https://www.stackoverflow.com/"));
EntityUtils.consume(response.getEntity());
System.out.println(LocalDateTime.now() + " FINISH");
}
}
输出结果是这样的:
2022-06-30T17:00:08.178[leased: 0; pending: 0; available: 0; max: 5]
2022-06-30T17:00:09.189[leased: 1; pending: 0; available: 0; max: 5]
2022-06-30T17:00:10.082 FINISH
2022-06-30T17:00:10.202[leased: 0; pending: 0; available: 2; max: 5]
2022-06-30T17:00:11.216[leased: 0; pending: 0; available: 2; max: 5]
2022-06-30T17:00:12.230[leased: 0; pending: 0; available: 0; max: 5]
2022-06-30T17:00:13.241[leased: 0; pending: 0; available: 0; max: 5]
leased
表示正在使用的连接pending
表示正在准备的连接(类似与浏览器F12时看到的请求pending)available
表示已释放可重复使用的连接max
表示连接池大小
根据日志打印情况,直接说结论:
- 请求从发起到得到相应到被消费之前(非常重要),都处于
leased
状态; - 请求被消费之后(
EntityUtils.consume()
或直接关闭连接),进入available
状态; - 默认情况下
available
状态的链接不会被主动释放,等待下一次调用; - 对
client
设置了evictIdleConnections
属性后链接才可以被自动回收; setConnectionTimeToLive
不是自动回收设置,也不是链接最大有效时长设置,当此项设置时间小于请求响应时间时连接也无法被关闭,具体作用不明;- 可以使用
connectionManager.closeIdleConnections
方法手动回收处于available
的空闲链接; - CSDN上绝大多数关于HttpClient自动回收设置的文章都是错的;
参考文章 https://blog.csdn.net/u013332124/article/details/82694076