Java服務調用RestTemplate與HttpClient的使用詳解

概述

常見的遠程調用方式有以下2種:

  • RPC: Remote Produce Call遠程過程調用,類似的還有RMI(remote method invoke)。自定義數據格式,基於原生TCP通信,速度快,效率高。早期的webservice,現在熱門的dubbo,都是RPC的典型代表。
  • Http: http其實是一種網絡傳輸協議,基於TCP,規定瞭數據傳輸的格式。 現在客戶端瀏覽器與服務端通信基本都是采用Http協議,也可以用來進行遠程服務調用。缺點是消息封裝臃腫,優勢是對服務的提供和調用方沒有任何技術限定,自由靈活,更符合微服務理念。現在熱門的Rest風格,就可以通過http協議來實現。

如果項目全部采用 Java技術棧,那麼使用Dubbo作為微服務架構是一個不錯的選擇。

如果項目的技術棧多樣化,主要采用瞭Spring和SpringBoot框架,那麼SpringCloud搭建微服務是不二之選,使用Http方式來實現服務間調用。

java開發中,使用http連接,訪問第三方網絡接口,通常使用的連接工具為RestTemplate、HttpClient和OKHttp。

RestTemplate

概述及依賴

HttpClient和OKHttp兩種連接工具,使用起來比較復雜,如果使用spring框架,可以使用restTemplate來進行http連接請求。

restTemplate默認的連接方式是java中的HttpConnection,可以使用ClientHttpRequestFactory指定不同的HTTP連接方式。

依賴

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.2.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.7</version>
</dependency>

配置類

基礎配置

@Configuration
public class RestTemplateConfig {
   @Bean
   public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
       return new RestTemplate(factory);
   }
   @Bean
   public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
       SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
       factory.setReadTimeout(150 * 1000); // ms
       factory.setConnectTimeout(150 * 1000); // ms
       return factory;
   }
}

進階配置

import org.apache.http.client.HttpClient;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
    /**
     * http連接管理器
     */
    @Bean
    public HttpClientConnectionManager poolingHttpClientConnectionManager() {
        /*// 註冊http和https請求
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();
        PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);*/
        PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
        // 最大連接數
        poolingHttpClientConnectionManager.setMaxTotal(500);
        // 同路由並發數(每個主機的並發)
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100);
        return poolingHttpClientConnectionManager;
    }
    /**
     * HttpClient
     * @param poolingHttpClientConnectionManager
     */
    @Bean
    public HttpClient httpClient(HttpClientConnectionManager poolingHttpClientConnectionManager) {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // 設置http連接管理器
        httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
        /*// 設置重試次數,默認是3次,沒有開啟
        httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true));*/
        // 設置默認請求頭
        /*List<Header> headers = new ArrayList<>();
        headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"));
        headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate"));
        headers.add(new BasicHeader("Accept-Language", "zh-CN"));
        headers.add(new BasicHeader("Connection", "Keep-Alive"));
        headers.add(new BasicHeader("Content-type", "application/json;charset=UTF-8"));
        httpClientBuilder.setDefaultHeaders(headers);*/
        return httpClientBuilder.build();
    }
    /**
     * 請求連接池配置
     * @param httpClient
     */
    @Bean
    public ClientHttpRequestFactory clientHttpRequestFactory(HttpClient httpClient) {
        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        // httpClient創建器
        clientHttpRequestFactory.setHttpClient(httpClient);
        // 連接超時時間/毫秒(連接上服務器(握手成功)的時間,超出拋出connect timeout)
        clientHttpRequestFactory.setConnectTimeout(5 * 1000);
        // 數據讀取超時時間(socketTimeout)/毫秒(服務器返回數據(response)的時間,超過拋出read timeout)
        clientHttpRequestFactory.setReadTimeout(10 * 1000);
        // 從連接池獲取請求連接的超時時間,不宜過長,必須設置/毫秒(超時間未拿到可用連接,會拋出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool)
        clientHttpRequestFactory.setConnectionRequestTimeout(10 * 1000);
        return clientHttpRequestFactory;
    }
    /**
     * rest模板
     */
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory) {
        // 配置請求工廠
        return new RestTemplate(clientHttpRequestFactory);
    }
}

使用

實體類

@Data
@Builder
@NoArgsConstrutor
@AllArgsConstrutor
public class BaseResponse<TempUser> implements Serializable {
    private static final long serialVersionUID  = 1L;
    private String responseCode;
    private String responseMessage;
    private List<TempUser> responseData;
}
@Data
@Builder
@NoArgsConstrutor
@AllArgsConstrutor
public class TempUser implements Serializable {
    private static final long serialVersionUID  = 1L;
    private String userName;   
    private Integer age;
}

GET請求

普通訪問

BaseResponse result = restTemplate.getForObject(
    "http://localhost:8080/cs-admin/rest/getUser?userName=張三&age=18", BaseResponse.class);

返回HTTP狀態

ResponseEntity<BaseResponse> responseEntity = restTemplate.getForEntity(
    "http://localhost:8080/cs-admin/rest/getUser?userName=張三&age=18", TempUser.class);
// 獲取狀態對象
HttpStatus httpStatus = responseEntity.getStatusCode();
// 獲取狀態碼
int statusCodeValue = responseEntity.getStatusCodeValue();
// 獲取headers
HttpHeaders httpHeaders = responseEntity.getHeaders();
// 獲取body
BaseResponse result = responseEntity.getBody();

映射請求參數

Map<String, Object> paramMap = new HashMap<>();
paramMap.put("userName", "張三");
paramMap.put("age", 18);
BaseResponse result = restTemplate.getForObject(
    "http://localhost:8080/cs-admin/rest/getUser?userName={userName}&age={age}", 
    BaseResponse.class, paramMap);

POST請求

普通訪問接口

TempUser param = new TempUser();
param.setUserName("張三");
param.setAge(18);
BaseResponse result = restTemplate.postForObject("http://localhost:8080/cs-admin/rest/getPostUser", 
                                             param, BaseResponse.class);

帶HEAD訪問接口

// 請求頭信息
HttpHeaders headers = new HttpHeaders();
//headers.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//headers.add("headParam1", "headParamValue");
// 請求體內容
TempUser param = new TempUser();
param.setUserName("張三");
param.setAge(18);
// 組裝請求信息
HttpEntity<TempUser> httpEntity=new HttpEntity<>(param, headers);
BaseResponse result = restTemplate.postForObject("http://localhost:8080/cs-admin/rest/getPostUser", 
                                             httpEntity, BaseResponse.class);

無請求體的訪問:僅method為post,傳參方式仍然為get的param方式

Map<String, Object> paramMap = new HashMap<>();
paramMap.put("userName", "張三");
paramMap.put("age", 18);
BaseResponse result = restTemplate.postForObject(
    "http://localhost:8080/cs-admin/rest/getPostUserNoBody?userName={userName}&age={age}", 
    null, BaseResponse.class, paramMap);
System.out.println(result);

上傳文件

後臺接口代碼:

@RequestMapping("uploadFile")
public TempUser uploadFile(HttpServletRequest request, TempUser form) {
    MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
    //獲取文件信息
    MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
    TempUser tempUser = new TempUser();
    if (multipartFile != null) {
        tempUser.setUserName(form.getUserName() + " " + multipartFile.getOriginalFilename());
    }
    if(form!=null){
        tempUser.setAge(form.getAge());
    }
    return tempUser;
}

訪問方式:

// 文件
FileSystemResource file=new FileSystemResource("D:\\Elasticsearch權威指南(中文版).pdf");
// 設置請求內容
MultiValueMap<String, Object> param=new LinkedMultiValueMap<>();
param.add("file", file);
// 其他參數
param.add("userName", "張三");
param.add("age", 18);
// 組裝請求信息
HttpEntity<MultiValueMap<String, Object>> httpEntity=new HttpEntity<>(param);
// 發送請求
TempUser result = restTemplate.postForObject("http://localhost:8080/cs-admin/rest/uploadFile", 
                                             httpEntity, TempUser.class);

HttpClient

概述

HttpClient 通過連接池創建連接:

  • 管理連接的基本單位是Route(路由),每個路由上都會維護一定數量的HTTP連接
  • 每次調用後必須執行 releaseConnection
  • 路由可以理解為客戶端機器到目標機器的一條線路
  • 如果不給 httpclient配置指定的連接管理器,httpclient會自動使用PoolingHttpClientConnectionManager作為連接管理器。
  • PoolingHttpClientConnectionManager默認的maxConnPerRoute和maxConnTotal分別是是2和20。也就是對於每個服務器最多隻會維護2個連接,看起來有點少。所以,在日常使用時我們盡量使用自己配置的連接管理器比較好。

連接池:

  • 連接池技術作為創建和管理連接的緩沖池技術。
  • 連接池管理的對象是長連接
  • 有長連接的優勢

**長連接:**是指客戶端與服務器端一旦建立連接以後,可以進行多次數據傳輸而不需重新建立連接,

優勢:

  • 省去瞭每次數據傳輸連接建立的時間開銷
  • 資源的訪問控制

**短連接:**每次數據傳輸都需要客戶端和服務器端建立一次連接

使用

使用HttpClient發送請求、接收響應很簡單,一般需要如下幾步即可:

  • 創建HttpClient對象
  • 創建請求方法的實例,並指定請求URL。如果需要發送GET請求,創建HttpGet對象;如果需要發送POST請求,創建HttpPost對象。
  • 如果需要發送請求參數,可調用HttpGet、HttpPost共同的setParams(HetpParams params)方法來添加請求參數;對於HttpPost對象而言,也可調用setEntity(HttpEntity entity)方法來設置請求參數,參數則必須用NameValuePair[]數組存儲
  • 調用HttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個HttpResponse
  • 調用HttpResponse的getAllHeaders()getHeaders(String name)等方法可獲取服務器的響應頭;調用HttpResponsegetEntity()方法可獲取HttpEntity對象,該對象包裝瞭服務器的響應內容。程序可通過該對象獲取服務器的響應內容
  • 釋放連接。無論執行方法是否成功,都必須釋放連接

依賴:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient-cache</artifactId>
    <version>4.5.2</version>
</dependency>
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.2</version>
</dependency>

java工具類

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * HttpClient 工具類
 */
@Slf4j
public class HttpClientUtil {
    public static final String APPLICATION_JSON_VALUE = "application/json";
    private static final Logger logger = log;
    private static final Integer CONN_TIME_OUT = 3000;// 超時時間豪秒
    private static final Integer SOCKET_TIME_OUT = 10000;
    /** 每個路由的最大請求數,默認2 */
    private static final Integer DEFAULT_MAX_PER_ROUTE = 40;
    /** 最大連接數,默認20 */
    private static final Integer MAX_TOTAL = 400;
    private static HttpClient httpClient;
    static {
        // 請求配置
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(CONN_TIME_OUT)
                .setConnectionRequestTimeout(CONN_TIME_OUT)
                .setSocketTimeout(SOCKET_TIME_OUT)
                .build();
        // 管理 http連接池
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
        cm.setMaxTotal(MAX_TOTAL);
        httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .setDefaultRequestConfig(requestConfig)
            .build();
    }
    /**
     * Get請求
     */
    public static String requestGet(String url, Map<String, String> paramsMap) throws Exception {
        logger.info("GET request  url:{} params:{}", url, paramsMap);
        Long start = System.currentTimeMillis();
        List<NameValuePair> params = initParams(paramsMap);
        // Get請求
        HttpGet httpGet = new HttpGet(url);
        try {
            // 設置參數
            String str = EntityUtils.toString(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));
            String uriStr = StringUtils.isEmpty(str) ? 
                httpGet.getURI().toString() : httpGet.getURI().toString() + "?" + str;
            httpGet.setURI(new URI(uriStr));
            // 發送請求
            HttpResponse response = httpClient.execute(httpGet);
            logger.info("GET request  url:{} response:{} time:{}",
                    url, response, System.currentTimeMillis() - start);
            // 獲取返回數據
            return getSuccessRetFromResp(response, url, JSON.toJSONString(paramsMap));
        } finally {
            // 必須釋放連接,不然連接用完後會阻塞
            httpGet.releaseConnection();
        }
    }
    /**
     * Post請求,Map格式數據
     */
    public static String requestPost(String url, Map<String, String> paramsMap) throws Exception {
        logger.info("POST request  url:{} params:{}", url, paramsMap);
        Long start = System.currentTimeMillis();
        List<NameValuePair> params = initParams(paramsMap);
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
            HttpResponse response = httpClient.execute(httpPost);
            logger.info("POST request  url:{} response:{}  time:{}",
                    url, response, System.currentTimeMillis() - start);
            String retStr = getSuccessRetFromResp(response, url, JSON.toJSONString(paramsMap));
            return retStr;
        } finally {
            httpPost.releaseConnection();
        }
    }
    /**
     * Post請求,json格式數據
     *
     */
    public static String requestPostJsonStr(String url, String json) throws Exception {
        logger.info("POST request  url:{} params:{}", url, json);
        long start = System.currentTimeMillis();
        HttpPost httpPost = new HttpPost(url);
        try {
            StringEntity entity = new StringEntity(json, Consts.UTF_8);
            entity.setContentType(APPLICATION_JSON_VALUE);
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);
            logger.info("POST request  url:{} response:{}  time:{}", 
                        url, response, System.currentTimeMillis() - start);
            return getSuccessRetFromResp(response, url, json);
        } finally {
            // 資源釋放
            httpPost.releaseConnection();
        }
    }
    /**
     * post Object格式數據
     */
    public static String requestPostJson(String url, Object obj) throws Exception {
        String params = JSON.toJSONString(obj);
        return requestPostJsonStr(url, params);
    }
    private static String getSuccessRetFromResp(HttpResponse response, String url, String params) throws Exception {
        String retStr = "";
        // 檢驗狀態碼,如果成功接收數據
        int code = response.getStatusLine().getStatusCode();
        if (code == 200) {
            retStr = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
        } else {
            throw new RuntimeException(String.format("Http request error:%s, url:%s, params:%s", response, url, params));
        }
        logger.info("Http request retStr:{}. url:{}", retStr, url);
        return retStr;
    }
    private static List<NameValuePair> initParams(Map<String, String> paramsMap) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        if (paramsMap == null)
            return params;
        for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
            params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        return params;
    }
}

到此這篇關於Java服務調用RestTemplate與HttpClient的使用詳解的文章就介紹到這瞭,更多相關Java RestTemplate與HttpClient內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: