SpringBoot 對RestTemplate的初始化

  • 代碼部分 
package com.http.config;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * Created by gao on 2018/12/10.
 * 聲明並初始化鏈接池用於調用外部服務接口
 */
@Configuration
public class RestTemplateConfig {
    @Autowired
    private HttpPoolConfig httpPoolProperties;

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

    @Bean
    public ClientHttpRequestFactory httpRequestFactory() {
        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(httpPoolProperties.maxTotal);
        connectionManager.setDefaultMaxPerRoute(httpPoolProperties.defaultMaxPerRoute);
        connectionManager.setValidateAfterInactivity(httpPoolProperties.validateAfterInactivity);
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(httpPoolProperties.socketTimeout) //服務器返回數據(response)的時間,超過拋出read timeout
                .setConnectTimeout(httpPoolProperties.connectTimeout) //鏈接上服務器(握手成功)的時間,超出拋出connect timeout
                .setConnectionRequestTimeout(httpPoolProperties.connectionRequestTimeout)//從鏈接池中獲取鏈接的超時時間,超時間未拿到可用鏈接,會拋出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
                .build();
        return HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .setConnectionManager(connectionManager)
                .build();
    }
}
  • 參數部分(application.yml文件)
httpclient:
  # 最大鏈接數
  maxtotal: 64
  # 每一個route默認的最大鏈接數
  defaultMaxPerRoute: 64
  # 鏈接上服務器(握手成功)的時間,超出拋出connect timeout
  connectTimeout: 10000
  # 從鏈接池中獲取鏈接的超時時間,超時間未拿到可用鏈接,會拋出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
  connectionRequestTimeout: 10000
  # 服務器返回數據(response)的時間,超過拋出read timeout
  socketTimeout: 10000
  # 存活時間
  validateAfterInactivity: 10000
  • 從yml中獲取參數(HttpPoolConfig.java)
package com.http.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * Created by gao on 2018/12/10.
 * 用於聲明鏈接池的相關配置信息
 */

@Configuration
public class HttpPoolConfig {
    @Value("${httpclient.maxtotal}")
    public Integer maxTotal;
    @Value("${httpclient.defaultMaxPerRoute}")
    public Integer defaultMaxPerRoute;
    /**
     * 鏈接上服務器(握手成功)的時間,超出拋出connect timeout
     */
    @Value("${httpclient.connectTimeout}")
    public Integer connectTimeout;
    /**
     * 從鏈接池中獲取鏈接的超時時間,超時間未拿到可用鏈接,會拋出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
     */
    @Value("${httpclient.connectionRequestTimeout}")
    public Integer connectionRequestTimeout;
    /**
     * 服務器返回數據(response)的時間,超過拋出read timeout
     */
    @Value("${httpclient.socketTimeout}")
    public Integer socketTimeout;
    @Value("${httpclient.validateAfterInactivity}")
    public Integer validateAfterInactivity;


}
相關文章
相關標籤/搜索