依赖

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.13</version>
        </dependency>

HttpClientUtil.java

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;
    }
}

如果需要支持https

Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslConnectionSocketFactory())
                .build();
PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
//最大连接数
httpClientConnectionManager.setMaxTotal(100);
//并发数
httpClientConnectionManager.setDefaultMaxPerRoute(10);


/**
 * 支持SSL 
 */
private SSLConnectionSocketFactory sslConnectionSocketFactory() {
        try {
            return new SSLConnectionSocketFactory(
                    SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
                    NoopHostnameVerifier.INSTANCE);
        } catch (Exception e) {
            return SSLConnectionSocketFactory.getSocketFactory();
        }
 }

如果需要修改请求重试

                    httpClient = HttpClientBuilder.create()
                            // 关闭自动重试
                            // .disableAutomaticRetries()
                             // 自定义自动重试
                            .setRetryHandler(httpRequestRetryHandler())
                            // 设置连接池
                            .setConnectionManager(connectionManager)
                            // setConnectionTimeToLive(2, TimeUnit.MINUTES) 设置链接最大存活时间 此选项无效
                            // 设置连接自动回收时间
                            .evictIdleConnections(1, TimeUnit.MINUTES)
                            // 设置超时时间
                            .setDefaultRequestConfig(requestConfig).build();
                            
 /**
     * 自动重试
     */
    private HttpRequestRetryHandler httpRequestRetryHandler() {
        return (exception, executionCount, context) -> {
            if (executionCount >= 5) {// 如果已经重试了5次,就放弃
                return false;
            }
            if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
                return true;
            }
            if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
                return false;
            }
            if (exception instanceof InterruptedIOException) {// 超时
                return false;
            }
            if (exception instanceof UnknownHostException) {// 目标服务器不可达
                return false;
            }
            if (exception instanceof SSLException) {// SSL握手异常
                return false;
            }

            HttpClientContext clientContext = HttpClientContext
                    .adapt(context);
            HttpRequest request = clientContext.getRequest();
            // 如果请求是幂等的,就再次尝试
            return !(request instanceof HttpEntityEnclosingRequest);
        };
    }

更多请求方法

 /**
     * 不带参数的get请求,如果状态码为200,则返回body,如果不为200,则返回null
     *
     * @param url
     */
    public String doGet(String url) {
        log.info("httpGet_url==>{}", url);
        // 声明 http get 请求
        HttpGet httpGet = new HttpGet(url);
        // 装载配置信息
        httpGet.setConfig(this.config);
        // 发起请求
        try {
            CloseableHttpResponse response = this.httpClient.execute(httpGet);
            // 判断状态码是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                // 返回响应体的内容
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                log.info("httpGet_result==>{}", result);
                return result;
            }
        } catch (Exception e) {
            log.error("httpGet_error==>", e);
            return null;
        } finally {
            //httpGet.releaseConnection();
        }
        return null;
    }

    /**
     * 带参数的get请求,如果状态码为200,则返回body,如果不为200,则返回null
     *
     * @param url
     */
    public String doGet(String url, Map<String, Object> map) {
        URIBuilder uriBuilder = null;
        try {
            uriBuilder = new URIBuilder(url);
            if (map != null) {
                // 遍历map,拼接请求参数
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
                }
            }
            return doGet(uriBuilder.build().toString());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 不带参数post请求
     *
     * @param url
     * @return string json串
     * @throws Exception
     */
    public  String doPost(String url) throws Exception {
        return doPost(url , null,null );
    }

    public  String doPost(String url, Map<String, Object> map) throws Exception {
        return doPost(url , map,null );
    }
    /**
     * 带map参数的post请求
     *
     * @param url
     * @param map
     * @return string json串
     * @throws Exception
     */
    public String doPost(String url, Map<String, Object> map,Map<String, String> headers) throws Exception {
        log.info("httpPost_url==>{},params==>{}",url,map);
        // 声明httpPost请求
        HttpPost httpPost = new HttpPost(url);
        // 加入配置信息
        httpPost.setConfig( this.config );
        if ( headers != null ){
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpPost.setHeader(entry.getKey(),entry.getValue());
            }
        }
        // 判断map是否为空,不为空则进行遍历,封装from表单对象
        if (map != null) {
            List<NameValuePair> list = new ArrayList<>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            // 构造from表单对象
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");

            // 把表单放到post里
            httpPost.setEntity(urlEncodedFormEntity);
        }

        // 发起请求
        try(CloseableHttpResponse response = this.httpClient.execute(httpPost)) {
            if (response.getStatusLine().getStatusCode() == 200) {
                // 返回响应体的内容
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                log.info("httpPost_result==>{}",result);
                return result;
            }
        }finally {
            //httpPost.releaseConnection();
        }
        return null;
    }
    /**
     * 带json参数的post请求,用的最多,因为map可以转为json
     *
     * @param url  请求地址host+path
     * @param json json字符串
     * @return string json串
     */
    public String doPost(String url, String json) {
        log.info("httpPost_url==>{},json==>{}",url,json);
        //设置请求路径,请求格式,配置信息
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.setConfig(this.config);
        // 发起请求
        try {
            //格式化请求数据并设值
            StringEntity se = new StringEntity(json, "UTF-8");
            se.setContentType("text/json");
            httpPost.setEntity(se);
            CloseableHttpResponse response = this.httpClient.execute(httpPost);
            // 判断状态码是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                // 返回响应体的内容
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                log.info("httpPost_result==>{}", result);
                return result;
            }
        } catch (Exception e) {
            log.error("httpPost_error==>", e);
            return null;
        }finally {
            //httpPost.releaseConnection();
        }
        return null;
    }