双重检查单例模式的的HttpClient线程池实现

关键参数都进行了注释,具备链接自动关闭,超时设置,连接池等功能。个人备忘
httpclient版本为4.5.13,这是修改后的最终版本

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 20220518重构,引入单例连接池
 *
 * @author liuenqi
 */
public class HttpClientUtil {
    private static final Log log = LogFactory.getLog(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) 设置链接最大存活时间 此选项无效
                            // 设置连接自动回收时间
                            .evictIdleConnections(1, TimeUnit.MINUTES)
                            // 设置超时时间
                            .setDefaultRequestConfig(requestConfig).build();
                }
            }
        }
        return httpClient;
    }

    /**
     * 不带header参数的JSON格式Post请求
     *
     * @param url          请求URL
     * @param bodyJsonData body参数
     * @return 响应体
     */
    public static String httpPost(String url, Map<String, Object> bodyJsonData) {
        String result = null;

        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);

        // 组装body
        if (MapUtils.isNotEmpty(bodyJsonData)) {
            StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(bodyJsonData), StandardCharsets.UTF_8);
            stringEntity.setContentType(MediaType.APPLICATION_JSON_VALUE);
            stringEntity.setContentEncoding(StandardCharsets.UTF_8.name());
            httpPost.setEntity(stringEntity);
        }

        // 不要关闭client,会导致连接池被关闭
        CloseableHttpClient client = getHttpClient();

        // 自动释放response
        try (CloseableHttpResponse response = client.execute(httpPost)) {
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, StandardCharsets.UTF_8.name());

            // 状态值判断 非2xx抛出异常
            int status = response.getStatusLine().getStatusCode();
            if (status < HttpStatus.OK.value() || status >= HttpStatus.MULTIPLE_CHOICES.value()) {
                throw new RuntimeException(httpPost.getURI() + " 状态异常: " + status + ",响应: " + result);
            }

            EntityUtils.consume(entity);
        } catch (Exception e) {
            log.error("HttpClientUtil httpPost ERROR: " + e.getMessage(), e);
        } finally {
            // 记得释放连接
            httpPost.releaseConnection();
        }

        return result;
    }

    /**
     * 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) {
        String result = null;

        HttpGet httpGet = new HttpGet();
        try {
            // 拼装header参数
            if (MapUtils.isNotEmpty(headerParam)) {
                for (String key : headerParam.keySet()) {
                    httpGet.addHeader(key, headerParam.get(key));
                }
            }

            // 拼装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());

            // 不要关闭client,会导致连接池被关闭
            CloseableHttpClient client = getHttpClient();
            CloseableHttpResponse response = client.execute(httpGet);

            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, StandardCharsets.UTF_8.name());

            // 状态值判断 非2xx抛出异常
            int status = response.getStatusLine().getStatusCode();
            if (status < HttpStatus.OK.value() || status >= HttpStatus.MULTIPLE_CHOICES.value()) {
                throw new RuntimeException(httpGet.getURI() + " 状态异常: " + status + ",响应: " + result);
            }

            EntityUtils.consume(entity);
        } catch (Exception e) {
            log.error("HttpClientUtil doGet ERROR: " + e.getMessage(), e);
        } finally {
            // 记得释放连接
            httpGet.releaseConnection();
        }

        return result;
    }
}

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表示连接池大小

根据日志打印情况,直接说结论:

  1. 请求从发起到得到相应到被消费之前(非常重要),都处于leased状态;
  2. 请求被消费之后(EntityUtils.consume()或直接关闭连接),进入available状态;
  3. 默认情况下available状态的链接不会被主动释放,等待下一次调用;
  4. client设置了evictIdleConnections属性后链接才可以被自动回收;
  5. setConnectionTimeToLive不是自动回收设置,也不是链接最大有效时长设置,当此项设置时间小于请求响应时间时连接也无法被关闭,具体作用不明;
  6. 可以使用connectionManager.closeIdleConnections方法手动回收处于available的空闲链接;
  7. CSDN上绝大多数关于HttpClient自动回收设置的文章都是错的;

参考文章 https://blog.csdn.net/u013332124/article/details/82694076


版权声明:本文为u012760435原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。