springboot整合httpclient

参考:httpclient连接池管理,你用对了? | 并发编程网 – ifeve.com

SpringBoot整合HttpClient_秋水.丶的博客-优快云博客_httpclient springboot 整合

HttpClient详细使用示例_justry_deng的博客-优快云博客_httpclient

HttpClient4.2.1忽略https中的SSL证书失效 - 张汉生 - 博客园 

版本:httpclient4.5.13   springboot2.4.0

yml:

#最大连接数
http:
  maxTotal: 100
  #并发数
  defaultMaxPerRoute: 20
  #创建连接的最长时间
  connectTimeout: 2000
  #从连接池中获取到连接的最长时间
  connectionRequestTimeout: 5000
  #数据传输的最长时间
  socketTimeout: 10000
  #提交请求前测试连接是否可用
  staleConnectionCheckEnabled: true

HttpClientConfig:

package com.demo1.config;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpClientConfig {
    //最大连接数
    @Value("${http.maxTotal}")
    private Integer maxTotal;

    //并发数
    @Value("${http.defaultMaxPerRoute}")
    private Integer defaultMaxPerRoute;

    //创建连接的最长时间
    @Value("${http.connectTimeout}")
    private Integer connectTimeout;

    //从连接池中获取到连接的最长时间
    @Value("${http.connectionRequestTimeout}")
    private Integer connectionRequestTimeout;

    //数据传输的最长时间
    @Value("${http.socketTimeout}")
    private Integer socketTimeout;

    //提交请求前测试连接是否可用
    @Value("${http.staleConnectionCheckEnabled}")
    private boolean staleConnectionCheckEnabled;

    /**
     * 首先实例化一个连接池管理器,设置最大连接数、并发连接数
     *
     * @return httpClientConnectionManager
     */
    @Bean(name = "httpClientConnectionManager")
    public PoolingHttpClientConnectionManager getHttpClientConnectionManager() {
        PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
        //最大连接数
        httpClientConnectionManager.setMaxTotal(maxTotal);

        //并发数
        httpClientConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
        return httpClientConnectionManager;
    }

    /**
     * 实例化连接池,设置连接池管理器
     * 这里需要以参数形式注入上面实例化的连接池管理器
     *
     * @param httpClientConnectionManager 连接池管理器
     * @return httpClientBuilder 连接池
     */
    @Bean(name = "httpClientBuilder")
    public HttpClientBuilder getHttpClientBuilder(@Qualifier("httpClientConnectionManager") PoolingHttpClientConnectionManager httpClientConnectionManager) {
        //HttpClientBuilder中的构造方法被protected修饰,所以这里不能直接使用new来实例化一个HttpClientBuilder,可以使用HttpClientBuilder提供的静态方法create()来获取HttpClientBuilder对象
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setConnectionManager(httpClientConnectionManager);
        return httpClientBuilder;
    }

    /**
     * 注入连接池,用于获取httpClient
     *
     * @param httpClientBuilder 连接池
     * @return 获取httpclient
     */
    @Bean
    public CloseableHttpClient getCloseableHttpClient(@Qualifier("httpClientBuilder") HttpClientBuilder httpClientBuilder) {
        //        try {
//            //HttpClient设置忽略SSL,实现HTTPS访问, 解决Certificates does not conform to algorithm constraints
//            //http://www.manongjc.com/detail/13-cpmpdgbiafnliyo.html
//            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//                @Override
//                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
//                    return true;
//                }
//            }).build();
//            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
//            return HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
        return httpClientBuilder.build();//不忽略
    }


    /**
     * Builder是RequestConfig的一个内部类
     * 通过RequestConfig的custom方法来获取到一个Builder对象
     * 设置builder的连接信息
     * 这里还可以设置proxy,cookieSpec等属性,有需要的话可以在此设置
     *
     * @return builder
     */
    @Bean(name = "builder")
    public RequestConfig.Builder getBuilder() {
        RequestConfig.Builder builder = RequestConfig.custom();
        //将属性 set进 builder 中
        builder.setConnectTimeout(connectTimeout)
                .setConnectionRequestTimeout(connectionRequestTimeout)
                .setSocketTimeout(socketTimeout)
                .setStaleConnectionCheckEnabled(staleConnectionCheckEnabled);
        return builder;
    }

    /**
     * 使用builder构建一个RequestConfig对象
     *
     * @param builder 连接信息
     * @return builder.build()
     */
    @Bean
    public RequestConfig getRequestConfig(@Qualifier("builder") RequestConfig.Builder builder) {
        return builder.build();
    }

}
HttpClientUtil:
package com.demo1.config;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.SyncFailedException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.*;

@Component
public class HttpClientUtil {

    @Autowired
    private CloseableHttpClient httpClient;

    @Autowired
    private RequestConfig config;

    // 编码格式。发送编码格式统一用UTF-8
    private static final String ENCODING = "UTF-8";

    /**
     * Get请求
     * @param url 地址
     * @param headers 请求头
     * @param params 请求参数
     * @return
     */
    public String doGet(String url, Map<String, String> headers, Map<String, String> params) {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        // 创建访问的地址
        HttpGet httpGet=getUrlParam(url, params);
        httpGet.setConfig(config);
        packageHeader(headers, httpGet);

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
//            System.out.println("响应状态为:" + response.getStatusLine());
//            if (responseEntity != null) {
//                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
//                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
//            }
            return EntityUtils.toString(responseEntity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.回收链接到连接池
            if (null != response) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * Get请求
     * @param url 地址
     * @param params 请求参数
     * @return
     */
    public String doGet(String url, Map<String, String> params){
        return doGet(url,null,params);
    }

    /**
     * Get请求
     * @param url 地址
     * @return
     */
    public String doGet(String url){
        return doGet(url,null,null);
    }

    /**
     * Post请求
     * @param url 地址
     * @param headers  请求头
     * @param params  请求参数
     * @param params  url请求参数
     * @return
     */
    public String doPost(String url, Map<String, String> headers, Map<String, String> params,Map<String, String> UrlParams){
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        // 创建访问的地址
        HttpPost httpPost = postUrlParam(url,UrlParams);
        httpPost.setConfig(config);

        packageHeader(headers, httpPost);
        packageParam(headers, httpPost);

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.回收链接到连接池
            if (null != response) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * Post请求
     * @param url 地址
     * @param headers 请求头
     * @param urlParams url请求参数
     * @return
     */
    public String doPost(String url, Map<String,String> headers,Map<String, String> urlParams){
        return doPost(url,headers,null,urlParams);
    }

    /**
     * Post请求
     * @param url 地址
     * @param urlParams url请求参数
     * @return
     */
    public String doPost(String url, Map<String, String> urlParams){
        return doPost(url,null,null,urlParams);
    }

    /**
     * Post请求
     * @param url 地址
     * @return
     */
    public String doPost(String url){
        return doPost(url,new HashMap<>());
    }

    /**
     * Post请求
     * @param url 地址
     * @param headers  请求头
     * @param params  url请求参数
     * @param json  json请求体
     * @return
     */
    public String doPost(String url, Map<String, String> headers, Map<String, String> params,String json){
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        // 创建访问的地址
        HttpPost httpPost = postUrlParam(url,params);
        httpPost.setConfig(config);
        if(headers==null){
            headers=new HashMap<>();
        }
        headers.put("Content-Type", "application/json;charset=utf8");
        packageHeader(headers, httpPost);
        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        try {
            httpPost.setEntity(new StringEntity(json));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.回收链接到连接池
            if (null != response) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 简单json请求
     * @param url 地址
     * @param json  json请求体
     * @return
     */
    public String doPost(String url, String json){
        Map<String, String> headers=new HashMap<>();
        headers.put("Content-Type", "application/json;charset=utf8");
        return doPost(url,headers,null,json);
    }

    private HttpGet getUrlParam(String url, Map<String, String> params) {
        try{
            URIBuilder uriBuilder = new URIBuilder(url);
            if (params != null) {
                Set<Map.Entry<String, String>> entrySet = params.entrySet();
                for (Map.Entry<String, String> entry : entrySet) {
                    uriBuilder.setParameter(entry.getKey(), entry.getValue());
                }
            }
            // 创建Get请求
            return new HttpGet(uriBuilder.build());
        }catch (URISyntaxException e){
            throw new RuntimeException("url语法错误!");
        }
    }

    private HttpPost postUrlParam(String url, Map<String, String> params) {
        try{
            URIBuilder uriBuilder = new URIBuilder(url);
            if (params != null) {
                Set<Map.Entry<String, String>> entrySet = params.entrySet();
                for (Map.Entry<String, String> entry : entrySet) {
                    uriBuilder.setParameter(entry.getKey(), entry.getValue());
                }
            }
            // 创建Get请求
            return new HttpPost(uriBuilder.build());
        }catch (URISyntaxException e){
            throw new RuntimeException("url语法错误!");
        }
    }

    /**
     * Description: 封装请求头
     * @param params
     * @param httpMethod
     */
    private void packageHeader(Map<String, String> params, HttpRequestBase httpMethod) {
        // 封装请求头
        if (params != null) {
            Set<Map.Entry<String, String>> entrySet = params.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                // 设置到请求头到HttpRequestBase对象中
                httpMethod.setHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    /**
     * Description: 封装请求参数
     *
     * @param params
     * @param httpMethod
     * @throws UnsupportedEncodingException
     */
    private void packageParam(Map<String, String> params, HttpEntityEnclosingRequestBase httpMethod) {
        // 封装请求参数
        if (params != null) {
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            Set<Map.Entry<String, String>> entrySet = params.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }

            // 设置到请求的http对象中
            try {
                httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("不支持的地址格式!");
            }
        }
    }
}

测试连接池归还情况:

yml:

#最大连接数
http:
  maxTotal: 1
  #并发数
  defaultMaxPerRoute: 1
  #创建连接的最长时间
  connectTimeout: 2000
  #从连接池中获取到连接的最长时间
  connectionRequestTimeout: 5000
  #数据传输的最长时间
  socketTimeout: 10000
  #提交请求前测试连接是否可用
  staleConnectionCheckEnabled: true

修改后的doget,不返还连接池,

执行EntityUtils.toString(responseEntity)方法中关闭连接,否则。
/**
     * Get请求
     * @param url 地址
     * @param headers 请求头
     * @param params 请求参数
     * @return
     */
    public String doGet(String url, Map<String, String> headers, Map<String, String> params) {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        // 创建访问的地址
        HttpGet httpGet=getUrlParam(url, params);
        httpGet.setConfig(config);
        packageHeader(headers, httpGet);

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
//            System.out.println("响应状态为:" + response.getStatusLine());
//            if (responseEntity != null) {
//                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
//                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
//            }
            return "123";
//            return EntityUtils.toString(responseEntity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.回收链接到连接池
//            if (null != response) {
//                try {
//                    EntityUtils.consume(response.getEntity());
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
        }
        return null;
    }

执行1次成功,第二次不成功。因为第一次还没返还连接池。

返还连接池源码片段:调用EntityUtils.toString(responseEntity)或者EntityUtils.consume(responseEntity),可以看出在这2个方法中都返还到了连接池

private static String toString(
            final HttpEntity entity,
            final ContentType contentType) throws IOException {
        final InputStream inStream = entity.getContent();
        if (inStream == null) {
            return null;
        }
        try {
            Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
                    "HTTP entity too large to be buffered in memory");
            int capacity = (int)entity.getContentLength();
            if (capacity < 0) {
                capacity = DEFAULT_BUFFER_SIZE;
            }
            Charset charset = null;
            if (contentType != null) {
                charset = contentType.getCharset();
                if (charset == null) {
                    final ContentType defaultContentType = ContentType.getByMimeType(contentType.getMimeType());
                    charset = defaultContentType != null ? defaultContentType.getCharset() : null;
                }
            }
            if (charset == null) {
                charset = HTTP.DEF_CONTENT_CHARSET;
            }
            final Reader reader = new InputStreamReader(inStream, charset);
            final CharArrayBuffer buffer = new CharArrayBuffer(capacity);
            final char[] tmp = new char[1024];
            int l;
            while((l = reader.read(tmp)) != -1) {
                buffer.append(tmp, 0, l);
            }
            return buffer.toString();
        } finally {
            inStream.close();
        }
    }
/**
     * Ensures that the entity content is fully consumed and the content stream, if exists,
     * is closed.
     *
     * @param entity the entity to consume.
     * @throws IOException if an error occurs reading the input stream
     *
     * @since 4.1
     */
    public static void consume(final HttpEntity entity) throws IOException {
        if (entity == null) {
            return;
        }
        if (entity.isStreaming()) {
            final InputStream inStream = entity.getContent();
            if (inStream != null) {
                inStream.close();
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值