update resttemplate config

This commit is contained in:
Xiong Neng 2018-03-20 20:08:33 +08:00
parent 2890b40b4f
commit 03cb51fd4a
2 changed files with 51 additions and 27 deletions

View File

@ -43,7 +43,7 @@
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>4.3.6</version> <version>4.5.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -6,16 +6,31 @@ import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder; import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/** /**
* RestTemplate客户端连接池配置 * RestTemplate客户端连接池配置
* *
@ -26,34 +41,43 @@ import org.springframework.web.client.RestTemplate;
@Configuration @Configuration
public class RestClientConfig { public class RestClientConfig {
@Bean @Bean
public ClientHttpRequestFactory httpRequestFactory() { public RestTemplate restTemplate() {
return new HttpComponentsClientHttpRequestFactory(httpClient()); RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(clientHttpRequestFactory());
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
return restTemplate;
} }
@Bean @Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) { public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() {
return new RestTemplate(httpRequestFactory()); try {
} HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Bean public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
public HttpClient httpClient() { return true;
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() }
.register("http", PlainConnectionSocketFactory.getSocketFactory()) }).build();
.register("https", SSLConnectionSocketFactory.getSocketFactory()) httpClientBuilder.setSSLContext(sslContext);
.build(); HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
connectionManager.setMaxTotal(5); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
connectionManager.setDefaultMaxPerRoute(5); .register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslConnectionSocketFactory).build();// 注册http和https请求
RequestConfig requestConfig = RequestConfig.custom() // 开始设置连接池
.setSocketTimeout(8000) PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
.setConnectTimeout(8000) poolingHttpClientConnectionManager.setMaxTotal(500); // 最大连接数500
.setConnectionRequestTimeout(8000) poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100); // 同路由并发数100
.build(); httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); // 重试次数
return HttpClientBuilder.create() HttpClient httpClient = httpClientBuilder.build();
.setDefaultRequestConfig(requestConfig) HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); // httpClient连接配置
.setConnectionManager(connectionManager) clientHttpRequestFactory.setConnectTimeout(20000); // 连接超时
.build(); clientHttpRequestFactory.setReadTimeout(30000); // 数据读取超时时间
clientHttpRequestFactory.setConnectionRequestTimeout(20000); // 连接不够用的等待时间
return clientHttpRequestFactory;
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
log.error("初始化HTTP连接池出错", e);
}
return null;
} }
} }