目前(4.2.6.RELEASE)的 RestTemplate 主要有四种 ClientHttpRequestFactory 的实现,它们分别是:
基于 JDK HttpURLConnection 的 SimpleClientHttpRequestFactory 基于 Apache HttpComponents Client 的 HttpComponentsClientHttpRequestFactory 基于 OkHttp 2(OkHttp 最新版本为 3,有较大改动,包名有变动,不和老版本兼容)的 OkHttpClientHttpRequestFactory 基于 Netty4 的 Netty4ClientHttpRequestFactory 另外,还有用于提供拦截器功能的 InterceptingClientHttpRequestFactory。 具体代码如下:
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.Value; import org.springframework.boot.web.client.RestTemplateBuilder; 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;
import javax.annotation.Resource;
/**
-
@description: restTemplate配置(增加连接池) -
@fileName: RestTemplateConfig.java -
@author: Sure -
@createAt: 2022/2/25 14:12 -
@updateBy: Sure -
@remark: */ @Configuration public class RestTemplateConfig { /**
- 接口调用类
*/ @Resource private RestTemplateBuilder restTemplateBuilder; /**
- httpComponentsClientHttpRequestFactory连接超时时间
*/ @Value("${http.connect.timeout}") private int httpConnectTimeOut; /**
- httpComponentsClientHttpRequestFactory读取数据超时时间
*/ @Value("${http.read.timeout}") private int httpReadTimeOut; /**
- 服务器返回数据(response)的时间,超过该时间抛出read timeout
*/ @Value("${http.pool.socket.timeout}") private int poolSocketTimeOut; /**
- 从连接池中获取连接的超时时间
*/ @Value("${http.pool.request.timeout}") private int poolRequestTimeOut; /**
- 从连接池中获取连接的超时时间
*/ @Value("${http.pool.connect.timeout}") private int poolConnectTimeOut; /**
- 从连接池中获取连接的超时时间
*/ @Value("${http.pool.connect.maxsize}") private int poolMaxSize; /**
- 路由
*/ @Value("${http.pool.connect.maxperroute}") private int poolMaxPerRoute; /**
- 注入调用三方接口使用的RestTemplate
- @return RestTemplate
*/ @Bean public RestTemplate thirdReqRestTemplate() { //暂时先通过build获取 new出来的sMessageConverters缺少参数 RestTemplate buildTemplate = restTemplateBuilder.build(); buildTemplate.setRequestFactory(httpRequestFactory()); return buildTemplate; } /**
- 定义ClientHttpRequestFactory
- @return ClientHttpRequestFactory
*/ @Bean public ClientHttpRequestFactory httpRequestFactory() { HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient()); httpComponentsClientHttpRequestFactory.setConnectTimeout(httpConnectTimeOut); httpComponentsClientHttpRequestFactory.setReadTimeout(httpReadTimeOut); return httpComponentsClientHttpRequestFactory; } /**
- 设置一种http响应客户端
- @return
*/ @Bean public HttpClient httpClient() { Registry registry = RegistryBuilder.create() .register(“http”, PlainConnectionSocketFactory.getSocketFactory()) .register(“https”, SSLConnectionSocketFactory.getSocketFactory()) .build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); //设置整个连接池最大连接数 connectionManager.setMaxTotal(poolMaxSize); //路由是对maxTotal的细分 connectionManager.setDefaultMaxPerRoute(poolMaxPerRoute); RequestConfig requestConfig = RequestConfig.custom() // 服务器返回数据(response)的时间,超过该时间抛出read timeout .setSocketTimeout(poolSocketTimeOut) // 连接上服务器(握手成功)的时间,超出该时间抛出connect timeout .setConnectTimeout(poolConnectTimeOut) // 从连接池中获取连接的超时时间,超过该时间未拿到可用连接, // 会抛出org.apache.http.conn.ConnectionPoolTimeoutException: // Timeout waiting for connection from pool .setConnectionRequestTimeout(poolRequestTimeOut) .build(); return HttpClientBuilder.create() .setDefaultRequestConfig(requestConfig) .setConnectionManager(connectionManager) .build(); }
}
|