HttpClient—与SpringBoot整合

一、HttpClient

两个系统间如何互相访问?两个tomcat上的项目如何互相访问

采用HttpClient实现跨系统的接口调用。

1.2、 什么是HttpClient

HttpClient—与SpringBoot整合_第1张图片
官网:http://hc.apache.org/index.html
特点:

  1. HttpClient别名:HttpComponents

  2. HttpClient可以发送get、post、put、delete、…等请求

  3. 1.3、 HttpClient入门案例

    
        org.apache.httpcomponents
        httpclient
        4.4
    
    

1.3.1、 发起Get请求

1创建一个客户端 CloseableHttpClient

2创建一个get方法请求实例 HttpGet

3发送请求 execute

4获取响应的头信息

5获取响应的主题内容

6关闭响应对象

使用HttpClient发起Get请求的案例代码:
public class DoGET {

    public static void main(String[] args) throws Exception {
        // 创建Httpclient对象,相当于打开了浏览器
        CloseableHttpClient httpclient = HttpClients.createDefault();

        // 创建HttpGet请求,相当于在浏览器输入地址
        HttpGet httpGet = new HttpGet("http://www.baidu.com/");

        CloseableHttpResponse response = null;
        try {
            // 执行请求,相当于敲完地址后按下回车。获取响应
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                // 解析响应,获取数据
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                // 关闭资源
                response.close();
            }
            // 关闭浏览器
            httpclient.close();
        }

    }
}

1.3.2、 带参数的Get请求

1创建一个客户端 CloseableHttpClient

2 通过URIBuilder传递参数

3创建一个get方法请求实例 HttpGet

4发送请求 execute

5获取响应的头信息

6获取响应的主题内容

7关闭响应对象

访问网站的爬虫协议:

public class DoGETParam {

public static void main(String[] args) throws Exception {
    // 创建Httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();
    // 创建URI对象,并且设置请求参数
    URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
    
    // 创建http GET请求
    HttpGet httpGet = new HttpGet(uri);

    // HttpGet get = new HttpGet("http://www.baidu.com/s?wd=java");
    
    CloseableHttpResponse response = null;
    try {
        // 执行请求
        response = httpclient.execute(httpGet);
        // 判断返回状态是否为200
        if (response.getStatusLine().getStatusCode() == 200) {
            // 解析响应数据
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(content);
        }
    } finally {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    }
}

}

1.3.4、 带参数POST请求

/*
 * 演示:使用HttpClient发起带有参数的POST请求
 */
public class DoPOSTParam {

    public static void main(String[] args) throws Exception {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建http POST请求,访问开源中国
        HttpPost httpPost = new HttpPost("http://www.oschina.net/search");

        // 根据开源中国的请求需要,设置post请求参数
        List parameters = new ArrayList(0);
        parameters.add(new BasicNameValuePair("scope", "project"));
        parameters.add(new BasicNameValuePair("q", "java"));
        parameters.add(new BasicNameValuePair("fromerr", "8bDnUWwC"));
        // 构造一个form表单式的实体
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
        // 将请求实体设置到httpPost对象中
        httpPost.setEntity(formEntity);

        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpPost);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                // 解析响应体
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            // 关闭浏览器
            httpclient.close();
        }
    }
}

SpringBoot官方并没有对HttpClient的启动器。所以我们需要自己完成配置
不过,SpringBoot虽然没有提供启动器,但是却提供了一个对Restful服务进行调用的模板类:RestTemplate,底层可以使用HttpClient来实现。有了这个我们就无需自己定义APIService了。
1、导入maven坐标

	
    org.apache.httpcomponents
    httpclient

2、在application.properties添加如下配置:

#The config for HttpClient 
http.maxTotal=300
http.defaultMaxPerRoute=50
http.connectTimeout=1000
http.connectionRequestTimeout=500
http.socketTimeout=5000
http.staleConnectionCheckEnabled=true

创建HttpClientConfig类–类似util(配置,无需搞懂,拿来即用)

	/**
 * HttpClient的配置类
 *
 */
@Configuration
@ConfigurationProperties(prefix = "http", ignoreUnknownFields = true)
public class HttpClientConfig {

    private Integer maxTotal;// 最大连接

    private Integer defaultMaxPerRoute;// 每个host的最大连接

    private Integer connectTimeout;// 连接超时时间

    private Integer connectionRequestTimeout;// 请求超时时间

    private Integer socketTimeout;// 响应超时时间

    /**
     * HttpClient连接池
     * @return
     */
    @Bean
    public HttpClientConnectionManager httpClientConnectionManager() {
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(maxTotal);
        connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
        return connectionManager;
    }

    /**
     * 注册RequestConfig
     * @return
     */
    @Bean
    public RequestConfig requestConfig() {
        return RequestConfig.custom().setConnectTimeout(connectTimeout)
            .setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout)
            .build();
    }

    /**
     * 注册HttpClient
     * @param manager
     * @param config
     * @return
     */
    @Bean
    public HttpClient httpClient(HttpClientConnectionManager manager, RequestConfig config) {
        return HttpClientBuilder.create().setConnectionManager(manager).setDefaultRequestConfig(config)
            .build();
    }
   /**
     * 使用连接池管理连接
     * @param httpClient
     * @return
     */
    @Bean
    public ClientHttpRequestFactory requestFactory(HttpClient httpClient) {
        return new HttpComponentsClientHttpRequestFactory(httpClient);
    }
    /**
     * 使用HttpClient来初始化一个RestTemplate
     * @param requestFactory
     * @return
     */
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory) {
        RestTemplate template = new RestTemplate(requestFactory);

        List> list = template.getMessageConverters();
        for (HttpMessageConverter mc : list) {
            if (mc instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) mc).setDefaultCharset(Charset.forName("UTF-8"));
            }
        }
        return template;
    }

    public Integer getMaxTotal() {
        return maxTotal;
    }

    public void setMaxTotal(Integer maxTotal) {
        this.maxTotal = maxTotal;
    }

    public Integer getDefaultMaxPerRoute() {
        return defaultMaxPerRoute;
    }

    public void setDefaultMaxPerRoute(Integer defaultMaxPerRoute) {
        this.defaultMaxPerRoute = defaultMaxPerRoute;
    }

    public Integer getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public Integer getConnectionRequestTimeout() {
        return connectionRequestTimeout;
    }

    public void setConnectionRequestTimeout(Integer connectionRequestTimeout) {
        this.connectionRequestTimeout = connectionRequestTimeout;
    }

    public Integer getSocketTimeout() {
        return socketTimeout;
    }

    public void setSocketTimeout(Integer socketTimeout) {
        this.socketTimeout = socketTimeout;
    }
}
  • 引入RestTemplate模板

    在FixedAreaController中,引入RestTemplate模板,发送rest请求

    @Autowired
    private RestTemplate restTemplate;
    
  • 编写FixedAreaController的方法

      // 查询未关联定区列表
      @GetMapping("/findNoAssociationCustomers")
      public ResponseEntity findNoAssociationCustomers() {
          // 使用HttpClient调用 远程接口
          String url = Constants.CRM_MANAGEMENT_HOST + "/customer/noAssociationCustomers";
          ResponseEntity result = restTemplate.getForEntity(url, String.class);
          HttpStatus statusCode = result.getStatusCode();
          String body = result.getBody();
          return new ResponseEntity<>(body,statusCode);
      }
    

1

	public class Constants {
   public static final String BOS_MANAGEMENT_HOST = "http://localhost:8088";
   public static final String CRM_MANAGEMENT_HOST = "http://localhost:8090";
   private static final String BOS_MANAGEMENT_CONTEXT = "/bos_management";
   private static final String CRM_MANAGEMENT_CONTEXT = "/crm_management";
}

1

	// 查询已关联到定区的用户列表
@GetMapping(value = "/findHasAssociationFixedAreaCustomers")
public ResponseEntity findHasAssociationFixedAreaCustomers(@RequestParam("id")String fixedAreaId) {
    // 使用HttpClient调用 接口
    String url = Constants.CRM_MANAGEMENT_HOST + "/customer/associationFixedAreaCustomers?fixedAreaId="+fixedAreaId;

    ResponseEntity result = restTemplate.getForEntity(url, String.class);

    //        return result;

    HttpStatus statusCode = result.getStatusCode();
    String body = result.getBody();
    return new ResponseEntity<>(body,statusCode);
}

你可能感兴趣的:(SpringBoot)