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

9 篇文章 0 订阅

关键参数都进行了注释,具备链接自动关闭,超时设置,连接池等功能。个人备忘
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表示连接池大小

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

  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

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,使用HttpClient实现对话可以分为以下几个步骤: 1. 创建HttpClient实例 ``` CloseableHttpClient httpClient = HttpClients.createDefault(); ``` 2. 创建HttpPost请求 ``` HttpPost httpPost = new HttpPost(url); ``` 其中,url是对话接口的地址。 3. 设置请求参数 对于ChitGPT,我们需要设置请求参数为JSON格式的字符串,例如: ``` StringEntity entity = new StringEntity("{\"text\": \"" + text + "\", \"context\": \"" + context + "\"}"); entity.setContentType("application/json"); httpPost.setEntity(entity); ``` 其中,text是用户输入的文本,context是对话上下文信息。 4. 执行请求并获取响应 ``` CloseableHttpResponse response = httpClient.execute(httpPost); ``` 5. 处理响应结果 ``` String result = EntityUtils.toString(response.getEntity(), "UTF-8"); ``` 其中,result为对话接口返回的JSON格式的字符串。 完整的代码示例如下: ``` import java.io.IOException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class ChatHttpClient { public static void main(String[] args) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); String url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg="; String text = "你好"; String context = ""; HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity("{\"text\": \"" + text + "\", \"context\": \"" + context + "\"}"); entity.setContentType("application/json"); httpPost.setEntity(entity); CloseableHttpResponse response = httpClient.execute(httpPost); String result = EntityUtils.toString(response.getEntity(), "UTF-8"); System.out.println(result); response.close(); httpClient.close(); } } ``` 需要注意的是,这里使用了青云客的API作为示例,实际应用中需要替换成对应的对话接口。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值