實(shí)戰(zhàn)代碼(十):Springboot RestTemplate連接池

一、理論基礎(chǔ)

RestTemplate是Springboot中內(nèi)置的Http請(qǐng)求工具,可以直接注入使用。

兩個(gè)常用的構(gòu)造方法如下圖 (點(diǎn)擊進(jìn)入文檔地址)

image.png

從上面可以看出,最簡(jiǎn)單的方式就是使用默認(rèn)配置,不需要額外的配置,便可以直接使用該工具。對(duì)于請(qǐng)求頻次比較低的情況下,默認(rèn)配置完全夠用。

如果需要大量的請(qǐng)求,可以自定義配置,使用HttpClient的連接池。

二、實(shí)戰(zhàn)代碼

2.1 依賴引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- 將tomcat替換為undertow -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

<!-- 自定義的元數(shù)據(jù)依賴 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

<!-- httpclient,為了使用其連接池 -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.12</version>
</dependency>

<!-- for fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
</dependency>

<!-- lombok,減少代碼量,idea需要安裝lombok插件 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

2.2 配置項(xiàng)

2.2.1 配置

http:
  pool:
    # 連接超時(shí)
    connectTimeout: 5000
    connectionRequestTimeout: 5000
    defaultMaxPerRoute: 20
    # 最大連接數(shù)
    maxTotal: 100
    # 服務(wù)器返回?cái)?shù)據(jù)(response)的時(shí)間
    socketTimeout: 5000
    validateAfterInactivity: 30000

2.2.2 讀取配置文件

@Component
@ConfigurationProperties(prefix = "http.pool")
@Data
public class HttpClientProperties {


    /**
     * 最大連接數(shù)
     */
    private Integer maxTotal;
    /**
     * 路由是對(duì)最大連接數(shù)的細(xì)分
     * 每個(gè)路由基礎(chǔ)的連接數(shù)
     */
    private Integer defaultMaxPerRoute;
    /**
     * 連接超時(shí)時(shí)間
     */
    private Integer connectTimeout;
    /**
     * 從連接池中獲取連接的超時(shí)時(shí)間
     */
    private Integer connectionRequestTimeout;
    /**
     * 服務(wù)器返回?cái)?shù)據(jù)(response)的時(shí)間
     */
    private Integer socketTimeout;
    /**
     * 可用空閑連接過(guò)期時(shí)間
     * 重用空閑連接時(shí)會(huì)先檢查是否空閑時(shí)間超過(guò)這個(gè)時(shí)間,如果超過(guò),釋放socket重新建立
     */
    private Integer validateAfterInactivity;
}

2.3 配置文件

@Configuration
public class RestTemplateConfig {


    private final HttpClientProperties httpClientProperties;

    public RestTemplateConfig(HttpClientProperties httpClientProperties) {
        this.httpClientProperties = httpClientProperties;
    }


    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }


    @Bean
    public ClientHttpRequestFactory clientHttpRequestFactory(){
        return new HttpComponentsClientHttpRequestFactory(httpClient());
    }


    @Bean
    public HttpClient httpClient() {
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
        connectionManager.setMaxTotal(httpClientProperties.getMaxTotal());
        connectionManager.setDefaultMaxPerRoute(httpClientProperties.getDefaultMaxPerRoute());
        connectionManager.setValidateAfterInactivity(httpClientProperties.getValidateAfterInactivity());
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(httpClientProperties.getSocketTimeout())
                .setConnectTimeout(httpClientProperties.getConnectTimeout())
                .setConnectionRequestTimeout(httpClientProperties.getConnectionRequestTimeout())
                .build();
        return HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .setConnectionManager(connectionManager)
                .build();
    }
}

2.4 簡(jiǎn)單的工具類(lèi)

@Component
@Slf4j
public class HttpClientUtils {

    private final RestTemplate restTemplate;

    public HttpClientUtils(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * Get
     */
    public String get(String uri, Map<String, String> param, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("token", token);
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        StringBuilder url = new StringBuilder();
        url.append(uri).append("?");
        param.forEach((k, v) -> url.append(k).append("=").append(v).append("&"));
        String getUrl = url.substring(0, url.length() - 1);
        ResponseEntity<CodeResult> response = restTemplate.exchange(getUrl, HttpMethod.GET, requestEntity, CodeResult.class);
        return parseCodeResult(response);
    }

    /**
     * post方式請(qǐng)求接口
     * - 參數(shù)形式:form-data
     */
    public String postFormData(String url, MultiValueMap<String, Object> paramMap, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("token", token);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        //用HttpEntity封裝整個(gè)請(qǐng)求報(bào)文
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(paramMap, headers);
        ResponseEntity<CodeResult> response = restTemplate.postForEntity(url, files, CodeResult.class);
        return parseCodeResult(response);
    }

    /**
     * post方式請(qǐng)求接口
     * - 參數(shù)形式:json
     */
    public String postJson(String url, String param, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("token", token);
        HttpEntity<String> entity = new HttpEntity<>(param, headers);
        ResponseEntity<CodeResult> response = restTemplate.postForEntity(url, entity, CodeResult.class);
        return parseCodeResult(response);
    }

    /**
     * 解析云端服務(wù)接口返回值
     */
    public String parseCodeResult(ResponseEntity<CodeResult> response) {
        if (response.getStatusCodeValue() != HttpStatus.OK.value()) {
            throw new RuntimeException("網(wǎng)絡(luò)請(qǐng)求失敗,狀態(tài)碼:" + response.getStatusCodeValue());
        }
        CodeResult codeResult = response.getBody();
        assert codeResult != null;
        if (!codeResult.getSuccess()) {
            log.error("調(diào)用后臺(tái)服務(wù)接口錯(cuò)誤,返回值:{}", JSON.toJSON(codeResult));
            throw new RuntimeException("調(diào)用后臺(tái)服務(wù)接口錯(cuò)誤, 錯(cuò)誤信息:" + codeResult.getMsg());
        }
        return JSON.toJSONString(codeResult.getData());
    }
}

三、源碼地址

https://github.com/lysmile/spring-boot-demo/tree/master/spring-boot-resttemplate-demo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容