From 03cb51fd4acaa447819c44dcd185210a778ff68a Mon Sep 17 00:00:00 2001 From: Xiong Neng Date: Tue, 20 Mar 2018 20:08:33 +0800 Subject: [PATCH] update resttemplate config --- springboot-resttemplate/pom.xml | 2 +- .../xncoding/pos/config/RestClientConfig.java | 76 ++++++++++++------- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/springboot-resttemplate/pom.xml b/springboot-resttemplate/pom.xml index 259184d..9673794 100644 --- a/springboot-resttemplate/pom.xml +++ b/springboot-resttemplate/pom.xml @@ -43,7 +43,7 @@ org.apache.httpcomponents httpclient - 4.3.6 + 4.5.3 org.springframework.boot diff --git a/springboot-resttemplate/src/main/java/com/xncoding/pos/config/RestClientConfig.java b/springboot-resttemplate/src/main/java/com/xncoding/pos/config/RestClientConfig.java index 5606245..1b3728b 100644 --- a/springboot-resttemplate/src/main/java/com/xncoding/pos/config/RestClientConfig.java +++ b/springboot-resttemplate/src/main/java/com/xncoding/pos/config/RestClientConfig.java @@ -6,16 +6,31 @@ 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.NoopHostnameVerifier; 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.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.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.DefaultResponseErrorHandler; 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客户端连接池配置 * @@ -26,34 +41,43 @@ import org.springframework.web.client.RestTemplate; @Configuration public class RestClientConfig { @Bean - public ClientHttpRequestFactory httpRequestFactory() { - return new HttpComponentsClientHttpRequestFactory(httpClient()); + public RestTemplate restTemplate() { + RestTemplate restTemplate = new RestTemplate(); + restTemplate.setRequestFactory(clientHttpRequestFactory()); + restTemplate.setErrorHandler(new DefaultResponseErrorHandler()); + return restTemplate; } @Bean - public RestTemplate restTemplate(RestTemplateBuilder builder) { - return new RestTemplate(httpRequestFactory()); - } - - @Bean - public HttpClient httpClient() { - Registry registry = RegistryBuilder.create() - .register("http", PlainConnectionSocketFactory.getSocketFactory()) - .register("https", SSLConnectionSocketFactory.getSocketFactory()) - .build(); - PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); - connectionManager.setMaxTotal(5); - connectionManager.setDefaultMaxPerRoute(5); - - RequestConfig requestConfig = RequestConfig.custom() - .setSocketTimeout(8000) - .setConnectTimeout(8000) - .setConnectionRequestTimeout(8000) - .build(); - - return HttpClientBuilder.create() - .setDefaultRequestConfig(requestConfig) - .setConnectionManager(connectionManager) - .build(); + public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() { + try { + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); + SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { + public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + return true; + } + }).build(); + httpClientBuilder.setSSLContext(sslContext); + HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; + SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); + Registry socketFactoryRegistry = RegistryBuilder.create() + .register("http", PlainConnectionSocketFactory.getSocketFactory()) + .register("https", sslConnectionSocketFactory).build();// 注册http和https请求 + // 开始设置连接池 + PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); + poolingHttpClientConnectionManager.setMaxTotal(500); // 最大连接数500 + poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100); // 同路由并发数100 + httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager); + httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); // 重试次数 + HttpClient httpClient = httpClientBuilder.build(); + HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); // httpClient连接配置 + clientHttpRequestFactory.setConnectTimeout(20000); // 连接超时 + clientHttpRequestFactory.setReadTimeout(30000); // 数据读取超时时间 + clientHttpRequestFactory.setConnectionRequestTimeout(20000); // 连接不够用的等待时间 + return clientHttpRequestFactory; + } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { + log.error("初始化HTTP连接池出错", e); + } + return null; } }