HTTP 四、HttpClient的使用

一、简单介绍

        1、简介

        HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。

         HTTP和浏览器有点像,但却不是浏览器。很多人觉得既然HttpClient是一个HTTP客户端编程工具,很多人把他当做浏览器来理解,但是其实HttpClient不是浏览器,它是一个HTTP通信库,因此它只提供一个通用浏览器应用程序所期望的功能子集,最根本的区别是HttpClient中没有用户界面,浏览器需要一个渲染引擎来显示页面,并解释用户输入,例如鼠标点击显示页面上的某处,有一个布局引擎,计算如何显示HTML页面,包括级联样式表和图像。javascript解释器运行嵌入HTML页面或从HTML页面引用的javascript代码。来自用户界面的事件被传递到javascript解释器进行处理。除此之外,还有用于插件的接口,可以处理Applet,嵌入式媒体对象(如pdf文件,Quicktime电影和Flash动画)或ActiveX控件(可以执行任何操作)。HttpClient只能以编程的方式通过其API用于传输和接受HTTP消息。

      2、特性:
  1. 基于标准、纯净的java语言。实现了Http1.0和Http1.1
  2. 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。
  3.  支持HTTPS协议。
  4. 通过Http代理建立透明的连接。
  5. 利用CONNECT方法通过Http代理建立隧道的https连接。
  6. Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos认证方案。
  7.  插件式的自定义认证方案。
  8. 便携可靠的套接字工厂使它更容易的使用第三方解决方案。
  9. 连接管理器支持多线程应用。支持设置最大连接数,同时支持设置每个主机的最大连接数,发现并关闭过期的连接。
  10.  自动处理Set-Cookie中的Cookie。
  11. 插件式的自定义Cookie策略。
  12. Request的输出流可以避免流中内容直接缓冲到socket服务器。
  13. Response的输入流可以有效的从socket服务器直接读取相应内容。
  14.  在http1.0和http1.1中利用KeepAlive保持持久连接。
  15.  直接获取服务器发送的response code和 headers。
  16.  设置连接超时的能力。
  17. 实验性的支持http1.1 response caching。
  18. 源代码基于Apache License 可免费获取。

二、简单使用

        1、引入依赖

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

        2、使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

   //1. 创建HttpClient对象。
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();


        //2. 创建请求方法的实例,并指定请求URL。
        // 如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
        HttpGet httpGet = new HttpGet("http://localhost:8090/emp/get");

        //3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HttpParams params)方法来添加请求参数;
        // 对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
        
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,
            // 该方法返回一个HttpResponse。
            response = httpClient.execute(httpGet);

            // 从响应模型中获取响应实体
            //5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;
            // 调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 6. 释放连接。无论执行方法是否成功,都必须释放连接
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

三、各种使用案例

        1、GET请求  没有参数
       //1、创建HttpClient 客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        //2、 创建请求方法的实例,并指定请求URL
        HttpGet httpGet = new HttpGet("http://localhost:8090/user/get");

        //响应模型
        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));
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        2、GET 请求 路径上拼接参数

        1)直接拼接参数


        String userId = "1";
        String userName = "张三";

        //1、创建HttpClient 客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        //参数 拼接
        StringBuffer params = new StringBuffer();
        try {
            // 字符数据最好encoding一下;这样一来,某些特殊字符才能传过去(如:某人的名字就是“&”,不encoding的话,传不过去)
            params.append("userId=" + URLEncoder.encode(userId, "utf-8"));
            params.append("&");
            params.append("userName="+ URLEncoder.encode(userName, "utf-8"));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }



        //2、 创建请求方法的实例,并指定请求URL
        HttpGet httpGet = new HttpGet("http://localhost:8090/user/get"+ "?" + params);

        //响应模型
        CloseableHttpResponse response = null;
        try {

            //添加配置信息
            RequestConfig requestConfig  = RequestConfig.custom()
                    //设置连接超时时间(单位毫秒)
                    .setConnectTimeout(5000)
                    //设置请求超时时间(单位毫秒)
                    .setConnectionRequestTimeout(5000)
                    //socket读写超时时间(单位毫秒)
                    .setSocketTimeout(50000)
                    //设置是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();

            // 将上面的配置信息,添加到Get请求中
            httpGet.setConfig(requestConfig);

            // 由客户端执行发送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));
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        2)使用URI获得HttpGet

   String userId = "1";
        String userName = "张三";

        //1、创建HttpClient 客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 使用URI 拼接好请求路径
        URI uri = null;
        try {
            // 将参数放入键值对类NameValuePair中,再放入集合中
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("userId", userId));
            params.add(new BasicNameValuePair("userName", userName));
            // 设置uri信息,并将参数集合放入uri;
            // 注:这里也支持一个键值对一个键值对地往里面放setParameter(String key, String value)
            uri = new URIBuilder().setScheme("http").setHost("localhost")
                    .setPort(8090).setPath("/user/get")
                    .setParameters(params).build();
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        }


        //2、 创建请求方法的实例,并指定请求URL
        HttpGet httpGet = new HttpGet(uri);

        //响应模型
        CloseableHttpResponse response = null;
        try {

            //添加配置信息
            RequestConfig requestConfig  = RequestConfig.custom()
                    //设置连接超时时间(单位毫秒)
                    .setConnectTimeout(5000)
                    //设置请求超时时间(单位毫秒)
                    .setConnectionRequestTimeout(5000)
                    //socket读写超时时间(单位毫秒)
                    .setSocketTimeout(50000)
                    //设置是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();

            // 将上面的配置信息,添加到Get请求中
            httpGet.setConfig(requestConfig);

            // 由客户端执行发送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));
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        3、POST请求 没有参数
         //1、创建HttpClient 客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();


        //2、 创建请求方法的实例,并指定请求URL
        HttpPost httpPost = new HttpPost("http://localhost:8090/user/get");

        //响应模型
        CloseableHttpResponse response = null;
        try {


            // 由客户端执行发送Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        4、POST请求 有参(对象参数 或 路径参数)

        1)路径拼接参数

        String userId = "1";
        String userName = "张三";

        //1、创建HttpClient 客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        //参数 拼接
        StringBuffer params = new StringBuffer();
        try {
            // 字符数据最好encoding一下;这样一来,某些特殊字符才能传过去(如:某人的名字就是“&”,不encoding的话,传不过去)
            params.append("userId=" + URLEncoder.encode(userId, "utf-8"));
            params.append("&");
            params.append("userName="+ URLEncoder.encode(userName, "utf-8"));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }



        //2、 创建请求方法的实例,并指定请求URL
        HttpPost httpPost = new HttpPost("http://localhost:8090/user/get"+"?"+params);

        // 设置ContentType(注:一般路径拼接参数请求头都是application/x-www-form-urlencoded)
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        
        //响应模型
        CloseableHttpResponse response = null;
        try {

            //添加配置信息
            RequestConfig requestConfig  = RequestConfig.custom()
                    //设置连接超时时间(单位毫秒)
                    .setConnectTimeout(5000)
                    //设置请求超时时间(单位毫秒)
                    .setConnectionRequestTimeout(5000)
                    //socket读写超时时间(单位毫秒)
                    .setSocketTimeout(50000)
                    //设置是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();

            // 将上面的配置信息,添加到Get请求中
            httpPost.setConfig(requestConfig);

            // 由客户端执行发送Get请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        2)对象参数


        //1、创建HttpClient 客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        //2、 创建请求方法的实例,并指定请求URL
        HttpPost httpPost = new HttpPost("http://localhost:8090/user/get");

        User user = new User();
        user.setId(1L);
        user.setName("张三");
        // 我这里利用阿里的fastjson,将Object转换为json字符串;
        // (需要导入com.alibaba.fastjson.JSON包)
        String jsonString = JSON.toJSONString(user);

        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        StringEntity entity = new StringEntity(jsonString, "UTF-8");
        httpPost.setEntity(entity);
        // json参数需要设置请求头为 application/json
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        //响应模型
        CloseableHttpResponse response = null;
        try {

            //添加配置信息
            RequestConfig requestConfig  = RequestConfig.custom()
                    //设置连接超时时间(单位毫秒)
                    .setConnectTimeout(5000)
                    //设置请求超时时间(单位毫秒)
                    .setConnectionRequestTimeout(5000)
                    //socket读写超时时间(单位毫秒)
                    .setSocketTimeout(50000)
                    //设置是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();

            // 将上面的配置信息,添加到Get请求中
            httpPost.setConfig(requestConfig);

            // 由客户端执行发送Get请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        3)(普通参数 + 对象参数)


        String userId = "1";
        String userName = "张三";

        //1、创建HttpClient 客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();


        //参数 拼接
        StringBuffer params = new StringBuffer();
        try {
            // 字符数据最好encoding一下;这样一来,某些特殊字符才能传过去(如:某人的名字就是“&”,不encoding的话,传不过去)
            params.append("userId=" + URLEncoder.encode(userId, "utf-8"));
            params.append("&");
            params.append("userName="+ URLEncoder.encode(userName, "utf-8"));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }



        //2、 创建请求方法的实例,并指定请求URL
        HttpPost httpPost = new HttpPost("http://localhost:8090/user/get"+"?"+params);

        User user = new User();
        user.setId(1L);
        user.setName("张三");
        // 我这里利用阿里的fastjson,将Object转换为json字符串;
        // (需要导入com.alibaba.fastjson.JSON包)
        String jsonString = JSON.toJSONString(user);

        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        StringEntity entity = new StringEntity(jsonString, "UTF-8");
        httpPost.setEntity(entity);
        // json参数需要设置请求头为 application/json
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        //响应模型
        CloseableHttpResponse response = null;
        try {

            //添加配置信息
            RequestConfig requestConfig  = RequestConfig.custom()
                    //设置连接超时时间(单位毫秒)
                    .setConnectTimeout(5000)
                    //设置请求超时时间(单位毫秒)
                    .setConnectionRequestTimeout(5000)
                    //socket读写超时时间(单位毫秒)
                    .setSocketTimeout(50000)
                    //设置是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();

            // 将上面的配置信息,添加到Get请求中
            httpPost.setConfig(requestConfig);

            // 由客户端执行发送Get请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        

        5、使用httpClient下载文件

        使用httpclient下载文件,其实在调用上跟上面没有区别,唯一需要注意的是,再获取响应实体时,容易出现 Attempted read from closed stream尝试读取关闭的流 异常,我们可以用BufferedHttpEntity 的方式获取流:


        //1、创建HttpClient 客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        //2、 创建请求方法的实例,并指定请求URL
        HttpPost httpPost = new HttpPost("http://localhost:8090/user/get");

        //响应模型
        CloseableHttpResponse response = null;
        try {

            //添加配置信息
            RequestConfig requestConfig  = RequestConfig.custom()
                    //设置连接超时时间(单位毫秒)
                    .setConnectTimeout(5000)
                    //设置请求超时时间(单位毫秒)
                    .setConnectionRequestTimeout(5000)
                    //socket读写超时时间(单位毫秒)
                    .setSocketTimeout(50000)
                    //设置是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();

            // 将上面的配置信息,添加到Get请求中
            httpPost.setConfig(requestConfig);

            // 由客户端执行发送Get请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            //entity实体流保存缓冲区,否则只能操作一次流就会关闭 ,BufferedHttpEntity可以多次读取流
            responseEntity = new BufferedHttpEntity(responseEntity);
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
                try(FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\新建文件夹 (2)\\测试.docx")){
                    responseEntity.writeTo(fileOutputStream);
                }
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        6、关于https的调用

        目前还没使用到,等使用到再补充。

        7、发送参数携带文件,并包含对象参数。响应中也包含文件

            //1、创建HttpClient 客户端
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();

            //2、 创建请求方法的实例,并指定请求URL
            HttpPost httpPost = new HttpPost("请求地址");

            BottomTextWatermarkDTO user = new BottomTextWatermarkDTO();
            user.setWatermark("张三张三张三张三张三张三张三张三");
            String jsonString = JSON.toJSONString(user);


            File file = new File("文件url");
            HttpEntity file1 = MultipartEntityBuilder.create()
                .addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName())
                // 如果有其他表单字段,可以添加 json字段记得选择json类型
                .addTextBody("bottomTextWatermarkDTO", jsonString, ContentType.APPLICATION_JSON)
                .build();

            httpPost.setEntity(file1);
            // 设置请求头
            httpPost.setHeader("PUBLIC-AUTH", "请求头");

            //响应模型
            CloseableHttpResponse response = null;
            try {

                //添加配置信息
                RequestConfig requestConfig  = RequestConfig.custom()
                        //设置连接超时时间(单位毫秒)
                        .setConnectTimeout(5000)
                        //设置请求超时时间(单位毫秒)
                        .setConnectionRequestTimeout(5000)
                        //socket读写超时时间(单位毫秒)
                        .setSocketTimeout(50000)
                        //设置是否允许重定向(默认为true)
                        .setRedirectsEnabled(true).build();

                // 将上面的配置信息,添加到Get请求中
                httpPost.setConfig(requestConfig);

                // 由客户端执行发送Get请求
                response = httpClient.execute(httpPost);
                // 从响应模型中获取响应实体
                HttpEntity responseEntity = response.getEntity();
                responseEntity = new BufferedHttpEntity(responseEntity);
                System.out.println("响应状态为:" + response.getStatusLine());
                if (responseEntity != null) {
//                    System.out.println("响应内容长度为:" + responseEntity.getContentLength());
//                    System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
                    try(FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\统一消息盒子接口 v1.8.0\\测试.pdf")){
                        responseEntity.writeTo(fileOutputStream);
                    }
                }

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    // 释放资源
                    if (httpClient != null) {
                        httpClient.close();
                    }
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        @RequestPart  在同时含有上传文件,有含有json参数时,使用这个注解进行接收!

    @PostMapping("/addBottomTextWatermark")
    public void addBottomTextWatermark(@RequestParam("file") MultipartFile file,
                                        @RequestPart BottomTextWatermarkDTO bottomTextWatermarkDTO,
                                            @RequestAttribute("systemId") Long systemId, HttpServletResponse response){
}

四、HttpClient连接池

        事实上对于httpClient 连接池以及相关的配置大有学问,针对高并发的场景有很多解决策略,这里只先简单介绍一下如何配置连接池以及怎么使用,复杂的业务范围可自行查询。

   HttpClient连接池是用于优化HTTP请求性能的一个关键机制,特别是在处理大量HTTP请求时,通过重用现有连接来减少每次新建连接的开销。连接池的主要优势包括减少建立连接的时间、减少资源消耗、提高请求的并发处理能力。

        以下是HttpClient连接池的几个核心概念和配置:

        

        1. 连接池的作用

        每次HTTP请求都需要进行TCP握手,特别是对于HTTPS,还需要进行TLS握手,这个过程非常耗时。连接池通过复用连接(连接保持在一个"池"中)避免了为每次请求都新建连接,从而大大提高性能。

        2、配置HttpClient连接池

        在Apache HttpClient中,可以通过PoolingHttpClientConnectionManager类来配置连接池。以下是一个基本的配置示例:

// 创建连接池管理器
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// 设置最大连接数
cm.setMaxTotal(100);
// 设置每个路由的最大连接数
cm.setDefaultMaxPerRoute(20);
// 创建HttpClient实例
CloseableHttpClient client = HttpClients.custom()
   .setConnectionManager(connectionManager)
   .setConnectionManagerShared(true)
   .evictIdleConnections(30, TimeUnit.SECONDS)  // 超过30秒的闲置连接会被清除
   .build();

最大连接数(MaxTotal): 池中允许的最大连接数。默认值较小,通常需要根据实际情况调大。

每个路由的最大连接数(DefaultMaxPerRoute): 每个目标主机(路由)允许的最大并发连接数。不同的主机或API服务器通常会有各自的并发限制。

保持连接活动时间(ConnectionKeepAlive): 指定一个连接在闲置后可以保持多久。短时间的保活可以避免不必要的重建连接,但太长时间可能导致连接闲置过多,浪费资源。

        注意事项

  • 连接泄漏: 如果不正确管理连接(如未关闭),可能会导致连接泄漏,从而耗尽连接池中的可用连接,影响系统的稳定性。
  • 线程安全: PoolingHttpClientConnectionManager是线程安全的,可以在多个线程中共享同一个HttpClient实例。

        3、连接池的原理

        如果没有使用连接池的话,发送http的步骤是这样的,首先创建 CloseableHttpClient 对象,然后封装 相关方法和参数。然后就是发送http请求,实际上耗时的操作就在发送http请求时,发送http时每次都会创建一个新的连接,创建新连接需要耗费时间和资源(例如 TCP 握手等),而且每次都要重新建立连接,这在高并发情况下会导致性能下降。

        所以使用连接池的目的其实就是为了尽可能的复用连接,使用了PoolingHttpClientConnectionManager,它会复用现有的 HTTP 连接,避免每次请求都创建新的连接。它会管理连接的生命周期,允许多个请求复用已经打开的连接。这样,性能会显著提升,因为复用连接减少了创建和销毁连接的开销。

        而且,实际上连接池对频繁的访问同一个地址的情况效果是最好的,因为连接池主要是帮我们做连接的复用等等,如果每次都是请求的不同的地址,其实效果就会减弱很多,相当于连接池作用只有 限制最大连接数、超时时间等等作用。连接池的主要作用是复用已有的连接,减少连接的创建和销毁开销。如果你频繁访问同一个主机或者相同的路由,连接池能够显著提高性能,因为它可以重复使用已有的连接。

        配置连接池的方式有两种,一种是使用静态类创建,一种是使用@Bean的方式创建。

        创建示例:

@Configuration
public class HttpClientConfig {


    @Bean
    public PoolingHttpClientConnectionManager poolingConnectionManager() {
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(100);
        connectionManager.setDefaultMaxPerRoute(20);
        return connectionManager;
    }

    @Bean
    public RequestConfig requestConfig() {
        return RequestConfig.custom()
                .setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000)
                .setSocketTimeout(5000)
                .build();
    }

    @Bean
    public CloseableHttpClient httpClient(PoolingHttpClientConnectionManager connectionManager, RequestConfig requestConfig) {
        return HttpClients.custom()
                .setConnectionManager(connectionManager)
                .setDefaultRequestConfig(requestConfig)
                .evictIdleConnections(30, TimeUnit.SECONDS)
                .evictExpiredConnections()
                .build();
    }


//    private static CloseableHttpClient httpClientInstance;
//
//    // 配置HttpClient为静态对象
//    static {
//        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
//        connectionManager.setMaxTotal(100);
//        connectionManager.setDefaultMaxPerRoute(20);
//
//        RequestConfig requestConfig = RequestConfig.custom()
//                .setConnectTimeout(5000)
//                .setConnectionRequestTimeout(5000)
//                .setSocketTimeout(5000)
//                .build();
//
//        httpClientInstance = HttpClients.custom()
//                .setConnectionManager(connectionManager)
//                .setDefaultRequestConfig(requestConfig)
//                .evictIdleConnections(30, TimeUnit.SECONDS)
//                .evictExpiredConnections()
//                .build();
//    }
//
//    // 静态方法,获取HttpClient实例
//    public static CloseableHttpClient getHttpClient() {
//        return httpClientInstance;
//    }


高并发场景下的 HttpClient 优化方案,QPS 大大提升!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值