zoukankan      html  css  js  c++  java
  • spring boot 整合 HttpClient

    第一步:引入HttpClient 的jar包

      1、httpClient 5.0 开始支持异步(Async)请求;

      2、httpclient 版本过低上传文件会出,原因是 org.apache.http.entity.ContentType 没有方法 withParameters(final NameValuePair... params);

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>
    <!-- httpClient文件上传需要 -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
    </dependency>

      

    第二步:创建一个httpClient的配置类

      1 package com.123.lenovo.adapter.se.config;
      2 
      3 import java.security.KeyStore;
      4 import javax.net.ssl.SSLContext;
      5 
      6 import org.apache.http.HeaderElement;
      7 import org.apache.http.HeaderElementIterator;
      8 import org.apache.http.HttpResponse;
      9 import org.apache.http.client.config.RequestConfig;
     10 import org.apache.http.conn.ConnectionKeepAliveStrategy;
     11 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
     12 import org.apache.http.conn.ssl.TrustAllStrategy;
     13 import org.apache.http.impl.client.CloseableHttpClient;
     14 import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
     15 import org.apache.http.impl.client.HttpClientBuilder;
     16 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
     17 import org.apache.http.message.BasicHeaderElementIterator;
     18 import org.apache.http.protocol.HTTP;
     19 import org.apache.http.protocol.HttpContext;
     20 import org.apache.http.ssl.SSLContexts;
     21 import org.elasticsearch.Build;
     22 import org.springframework.beans.factory.annotation.Value;
     23 import org.springframework.context.annotation.Bean;
     24 import org.springframework.context.annotation.Configuration;
     25 
     26 import com.123.lenovo.adapter.se.common.IdleConnectionEvictor;
     27 
     28 @Configuration
     29 public class HttpClientConfig {
     30 
     31     @Value("${http_max_total}")
     32     private int maxTotal = 800;
     33     
     34     @Value("${http_default_max_perRoute}")
     35     private int defaultMaxPerRoute = 80;
     36     
     37     @Value("${http_validate_after_inactivity}")
     38     private int validateAfterInactivity = 1000;
     39     
     40     @Value("${http_connection_request_timeout}")
     41     private int connectionRequestTimeout = 5000;
     42     
     43     @Value("${http_connection_timeout}")
     44     private int connectTimeout = 10000;
     45     
     46     @Value("${http_socket_timeout}")
     47     private int socketTimeout = 20000;
     48     
     49     @Value("${waitTime}")
     50     private int waitTime = 30000;
     51     
     52     @Value("${idleConTime}")
     53     private int idleConTime = 3;
     54     
     55     @Value("${retryCount}")
     56     private int retryCount = 3;
     57     
     58     @Bean
     59     public PoolingHttpClientConnectionManager createPoolingHttpClientConnectionManager() {
     60         PoolingHttpClientConnectionManager poolmanager = new PoolingHttpClientConnectionManager();
     61         poolmanager.setMaxTotal(maxTotal);
     62         poolmanager.setDefaultMaxPerRoute(defaultMaxPerRoute);
     63         poolmanager.setValidateAfterInactivity(validateAfterInactivity);
     64         return poolmanager;
     65     }
     66     
     67     @Bean
     68     public CloseableHttpClient createHttpClient(PoolingHttpClientConnectionManager poolManager) {
     69         HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setConnectionManager(poolManager);
     70         httpClientBuilder.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
     71             
     72             @Override
     73             public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
     74                 HeaderElementIterator iterator = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
     75                 while (iterator.hasNext()) {
     76                     HeaderElement headerElement = iterator.nextElement();
     77                     String param = headerElement.getName();
     78                     String value = headerElement.getValue();
     79                     if (null != value && param.equalsIgnoreCase("timeout")) {
     80                         return Long.parseLong(value) * 1000;
     81                     }
     82                 }
     83                 return 30 * 1000;
     84             }
     85         });
     86         httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false));
     87         return httpClientBuilder.build();
     88     }102     
    103     @Bean
    104     public RequestConfig createRequestConfig() {
    105         return RequestConfig.custom()
    106                 .setConnectionRequestTimeout(connectionRequestTimeout)     // 从连接池中取连接的超时时间
    107                 .setConnectTimeout(connectTimeout)                        // 连接超时时间
    108                 .setSocketTimeout(socketTimeout)                        // 请求超时时间
    109                 .build();
    110     }
    111     
    112     @Bean
    113     public IdleConnectionEvictor createIdleConnectionEvictor(PoolingHttpClientConnectionManager poolManager) {
    114         IdleConnectionEvictor idleConnectionEvictor = new IdleConnectionEvictor(poolManager, waitTime, idleConTime);
    115         return idleConnectionEvictor;
    116     }
    117     
    118 }

    第三步:创建httpClient的工具类

      1 @Component
      2 public class HttpClientHelper {
      3     
      4     private Logger LOGGER = LoggerFactory.getLogger(HttpClientHelper.class);
      5     
      6     @Autowired
      7     private CloseableHttpClient httpClient;
      8     
      9     @Autowired
     10     private RequestConfig requestConfig;
     11     
     12     public String get(String url, HashMap<String, Object> paramMap, HashMap<String, Object> header) {
     13         String result = null;
     14         if ("".equals(url)) {
     15             return result;
     16         }
     17         // 创建一个request对象
     18         HttpGet httpGet = new HttpGet(url);
     19         CloseableHttpResponse response = null;
     20         try {
     21             // 配置连接参数
     22             httpGet.setConfig(requestConfig);
     23             //设置参数  
     24             if (paramMap != null && paramMap.size() > 0) {
     25                 List<NameValuePair> params = new ArrayList<>();
     26                 for (Entry<String, Object> entry : paramMap.entrySet()) {
     27                     params.add(new BasicNameValuePair(entry.getKey(), URLEncoder.encode(entry.getValue().toString(), "UTF-8")));
     28                 }
     29                 String strParams = EntityUtils.toString(new UrlEncodedFormEntity(params));
     30                 // 防止多参数时,分隔符","被转义
     31                 String realParams = URLDecoder.decode(strParams, "UTF-8");
     32                 httpGet.setURI(new URI(httpGet.getURI().toString().indexOf("?") > 0 ? httpGet.getURI().toString() + "&" + realParams : httpGet.getURI().toString() + "?" + realParams));
     33             }
     34             // 设置头
     35             if (header != null && header.size() > 0) {
     36                 for (Entry<String, Object> entry : header.entrySet()) {
     37                     httpGet.addHeader(entry.getKey(), entry.getValue().toString());
     38                 }
     39             }
     40             // 执行request请求
     41             response = httpClient.execute(httpGet);
     42             result = parseResponse(response);
     43             
     44         } catch (Exception e) {
     45             LOGGER.error("url : "+ url +", msg : " + e.getMessage());
     46             httpGet.abort();
     47         } finally {
     48             try {
     49                 if (response != null) {
     50                     response.close();
     51                 }
     52             } catch (IOException e) {
     53                 e.printStackTrace();
     54             }
     55         }
     56         return result;
     57     }
     58     
     59     public String post(String url, HashMap<String, Object> paramMap, HashMap<String, Object> header) {
     60         String result = null;
     61         if ("".equals(url)) {
     62             return result;
     63         }
     64         // 创建一个request对象
     65         HttpPost httpPost = new HttpPost(url);
     66         CloseableHttpResponse response = null;
     67         try {
     68             // 配置连接参数
     69             httpPost.setConfig(requestConfig);
     70             // 设置参数
     71             if (paramMap != null && paramMap.size() > 0) {
     72                 List<NameValuePair> params = new ArrayList<>();
     73                 for (Entry<String, Object> entry : paramMap.entrySet()) {
     74                     params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
     75                 }
     76                 HttpEntity entity = new UrlEncodedFormEntity(params);
     77                 httpPost.setEntity(entity);
     78             }
     79             // 设置头
     80             if (header != null && header.size() > 0) {
     81                 for (Entry<String, Object> entry : header.entrySet()) {
     82                     httpPost.addHeader(entry.getKey(), entry.getValue().toString());
     83                 }
     84             }
     85             // 执行request请求
     86             response = httpClient.execute(httpPost);
     87             result = reponseHandle(response);
     88         } catch (Exception e) {
     89             LOGGER.error("url : "+ url +", msg : " + e.getMessage());
     90             httpPost.abort();
     91         } finally {
     92             try {
     93                 if (response != null) {
     94                     response.close();
     95                 }
     96             } catch (IOException e) {
     97                 e.printStackTrace();
     98             }
     99         }
    100         return result;
    101     }
    102     
    103     public String postJSON(String url, String json_str, HashMap<String, Object> header) {
    104         String result = null;
    105         if ("".equals(url)) {
    106             return result;
    107         }
    108         // 创建一个request对象
    109         HttpPost httpPost = new HttpPost(url);
    110         CloseableHttpResponse response = null;
    111         try {
    112             // 配置连接参数
    113             httpPost.setConfig(requestConfig);
    114             // 设置参数
    115             if (json_str != null && !"".equals(json_str)) {
    116                 StringEntity entity = new StringEntity(json_str, ContentType.APPLICATION_JSON);
    117                 entity.setContentEncoding("UTF-8");    
    118                 entity.setContentType("application/json");
    119                 httpPost.setEntity(entity);
    120             }
    121             // 设置头
    122             if (header != null && header.size() > 0) {
    123                 for (Entry<String, Object> entry : header.entrySet()) {
    124                     httpPost.addHeader(entry.getKey(), entry.getValue().toString());
    125                 }
    126             }
    127             // 执行request请求
    128             response = httpClient.execute(httpPost);
    129             result = reponseHandle(response);
    130             
    131         } catch (Exception e) {
    132             LOGGER.error("url : "+ url +", msg : " + e.getMessage()+", param : " +json_str);
    133             httpPost.abort();
    134         } finally {
    135             try {
    136                 if (response != null) {
    137                     response.close();
    138                 }
    139             } catch (IOException e) {
    140                 e.printStackTrace();
    141             }
    142         }
    143         return result;
    144     }
    145     
    146     public String uploadFile(String url, String filePath, String fileParam, Map<String, Object> params) {
    147         File file = new File(filePath);
    148         if (!(file.exists() && file.isFile())) {
    149             throw new RuntimeException("file : file is null");
    150         }
    151         String result = null;
    152         if ("".equals(url)) {
    153             return result;
    154         }
    155         // 创建一个request对象
    156         HttpPost httpPost = new HttpPost(url);
    157         CloseableHttpResponse response = null;
    158         try {
    159             // 配置连接参数
    160             httpPost.setConfig(requestConfig);
    161             MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    162             builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    163             builder.addBinaryBody(fileParam, file, ContentType.DEFAULT_BINARY, file.getName());
    164             if (params != null && params.size() > 0) {
    165                 for (Entry<String, Object> entry : params.entrySet()) {
    166                     builder.addTextBody(entry.getKey(), entry.getValue().toString(), ContentType.create("text/plain", Consts.UTF_8));
    167                 }
    168             }
    169             HttpEntity requestEntity = builder.build();
    170             httpPost.setEntity(requestEntity);
    171             // 执行request请求
    172             response = httpClient.execute(httpPost);
    173             result = reponseHandle(response);
    174             
    175         } catch (Exception e) {
    176             httpPost.abort();
    177         } finally {
    178             try {
    179                 if (response != null) {
    180                     response.close();
    181                 }
    182             } catch (IOException e) {
    183                 e.printStackTrace();
    184             }
    185         }
    186         return result;
    187     }
    188     
    189     /**
    190      * 解析 response数据
    191      * @description
    192      * @param response
    193      * @return
    194      * @author tangjingjing
    195      * @date 2018年10月12日
    196      */
    197     private String parseResponse(CloseableHttpResponse response) {
    198         String result = "";
    199         // 获取响应体
    200         HttpEntity httpEntity = null;
    201         InputStream inputStream = null;
    202         try {
    203             // 获取响应状态
    204             int statusCode = response.getStatusLine().getStatusCode();
    205             // 没有正常响应
    206             if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
    207                 throw new RuntimeException("statusCode : " + statusCode);
    208             }
    209             // 获取响应体
    210             httpEntity = response.getEntity();
    211             if (httpEntity != null) {
    212                 inputStream = httpEntity.getContent();
    213                 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
    214                 StringBuffer sb = new StringBuffer();
    215                 String line = "";
    216                 while((line=reader.readLine())!=null){
    217                     sb.append(line);
    218                 }
    219                 reader.close();
    220                 result = sb.toString();
    221             }
    222 
    223         } catch (Exception e) {
    224             LOGGER.error("HttpClientHelper parseResponse error", e);
    225         } finally {
    226             if (inputStream != null) {
    227                 try {
    228                     inputStream.close();
    229                 } catch (IOException e) {
    230                     e.printStackTrace();
    231                 }
    232             }
    233             // 如果httpEntity没有被完全消耗,那么连接无法安全重复使用,将被关闭并丢弃
    234             try {
    235                 EntityUtils.consume(httpEntity);
    236             } catch (IOException e) {
    237                 e.printStackTrace();
    238             }
    239         }
    240         return result;
    241     }
    242     
    243     private String reponseHandle(CloseableHttpResponse response) {
    244         String result = "";
    245         // 获取响应体
    246         HttpEntity httpEntity = null;
    247         try {
    248             // 获取响应状态
    249             int statusCode = response.getStatusLine().getStatusCode();
    250             // 没有正常响应
    251             if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
    252                 throw new RuntimeException("statusCode : " + statusCode);
    253             }
    254             // 获取响应体
    255             httpEntity = response.getEntity();
    256             if (httpEntity !=null) {
    257                 result = EntityUtils.toString(httpEntity);
    258             }
    259             
    260         } catch (Exception e) {
    261             LOGGER.error("HttpClientHelper reponseHandle error", e);
    262         } finally {
    263             // 如果httpEntity没有被完全消耗,那么连接无法安全重复使用,将被关闭并丢弃
    264             try {
    265                 EntityUtils.consume(httpEntity);
    266             } catch (IOException e) {
    267                 e.printStackTrace();
    268             }
    269         }
    270         return result;
    271     }
    272     
    273 }

    第四步:创建HttpClient无效连接清除类

     1 /**
     2  * 定期清理无效的http连接
     3  * @author viruser
     4  *
     5  */
     6 package com.123.lenovo.adapter.se.common;
     7 
     8 import java.util.concurrent.TimeUnit;
     9 
    10 import javax.annotation.PreDestroy;
    11 
    12 import org.apache.http.conn.HttpClientConnectionManager;
    13 
    14 /**
    15  * 定期清理无效的http连接
    16  * @author viruser
    17  *
    18  */
    19 public class IdleConnectionEvictor extends Thread {
    20 
    21     private final HttpClientConnectionManager manager;
    22     
    23     private Integer waitTime;
    24     
    25     private Integer idleConTime;
    26     
    27     private volatile boolean shutdown = true;
    28     
    29     public IdleConnectionEvictor(HttpClientConnectionManager manager, Integer waitTime, Integer idleConTime) {
    30         this.manager = manager;
    31         this.waitTime = waitTime;
    32         this.idleConTime = idleConTime;
    33         this.start();
    34     }
    35     
    36     @Override
    37     public void run() {
    38         try {
    39             if (shutdown) {
    40                 synchronized (this) {
    41                     wait(waitTime);
    42                     manager.closeIdleConnections(idleConTime, TimeUnit.SECONDS);
    43                     // 关闭失效的连接
    44                     manager.closeExpiredConnections();
    45                 }
    46             }
    47         } catch (Exception e) {
    48             
    49         }
    50     }
    51     
    52     @PreDestroy
    53     public void shutdown() {
    54         shutdown = false;
    55         synchronized (this) {
    56             notifyAll();
    57         }
    58     }
    59     
    60 }
  • 相关阅读:
    Version
    Windows Server Protocols (WSPP)
    Tomcat启动问题jvm访问拒绝的解决方法
    vue3中使用draggable插件实现元素的拖拽,排序,克隆
    消息队列的思考
    jenkins_ssh
    jenkins_构建配置
    minikube清理sh
    stream源码导读
    源码rabbit_3_消息链路追踪
  • 原文地址:https://www.cnblogs.com/super-jing/p/10794564.html
Copyright © 2011-2022 走看看