|
|
@@ -0,0 +1,112 @@
|
|
|
+package com.uas.ps.inquiry.config;
|
|
|
+
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
+import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
|
|
+import org.apache.http.impl.nio.client.HttpAsyncClients;
|
|
|
+import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
|
|
|
+import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
|
|
|
+import org.apache.http.impl.nio.reactor.IOReactorConfig;
|
|
|
+import org.apache.http.nio.reactor.ConnectingIOReactor;
|
|
|
+import org.apache.http.nio.reactor.IOReactorException;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author liuam
|
|
|
+ * @since 2018/8/27 0027 下午 15:58
|
|
|
+ */
|
|
|
+public class HttpClientSpringFactory {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(HttpClientSpringFactory.class);
|
|
|
+
|
|
|
+ private Integer asyncConnectTimeout;
|
|
|
+
|
|
|
+ private Integer asyncSocketTimeout;
|
|
|
+
|
|
|
+ private Integer asyncConnectionRequestTimeout;
|
|
|
+
|
|
|
+ private Boolean asyncSoKeepAlive;
|
|
|
+
|
|
|
+ private Integer asyncConnectNum;
|
|
|
+
|
|
|
+ private Integer asyncConnectPerRoute;
|
|
|
+
|
|
|
+ private Integer asyncIoThreadCount;
|
|
|
+
|
|
|
+ private Integer syncConnectTimeout;
|
|
|
+
|
|
|
+ private Integer syncSocketTimeout;
|
|
|
+
|
|
|
+ private Integer syncConnectionRequestTimeout;
|
|
|
+
|
|
|
+ private Integer syncConnectPerRoute;
|
|
|
+
|
|
|
+ private Integer syncConnectNum;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步 http 连接
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public CloseableHttpAsyncClient httpAsyncClient() {
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom()
|
|
|
+ .setConnectionRequestTimeout(asyncConnectionRequestTimeout)
|
|
|
+ .setConnectTimeout(asyncConnectTimeout)
|
|
|
+ .setSocketTimeout(asyncSocketTimeout)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ //配置io线程
|
|
|
+ IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
|
|
|
+ .setIoThreadCount(asyncIoThreadCount)
|
|
|
+ .setSoKeepAlive(true)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ ConnectingIOReactor ioReactor = null;
|
|
|
+ try {
|
|
|
+ ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
|
|
|
+ } catch (IOReactorException e) {
|
|
|
+ logger.error("配置 io 线程失败,error:{}", e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
|
|
|
+ connManager.setMaxTotal(asyncConnectNum);
|
|
|
+ connManager.setDefaultMaxPerRoute(asyncConnectPerRoute);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
|
|
|
+ .setConnectionManager(connManager)
|
|
|
+ .setDefaultRequestConfig(requestConfig)
|
|
|
+ .build();
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步 http 连接
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public CloseableHttpClient httpSyncClient() {
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom()
|
|
|
+ .setConnectionRequestTimeout(syncConnectionRequestTimeout)
|
|
|
+ .setConnectTimeout(syncConnectTimeout)
|
|
|
+ .setSocketTimeout(syncSocketTimeout)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
|
|
|
+ cm.setDefaultMaxPerRoute(syncConnectPerRoute);
|
|
|
+ cm.setMaxTotal(syncConnectNum);
|
|
|
+
|
|
|
+ CloseableHttpClient httpClient = HttpClients.custom()
|
|
|
+ .setConnectionManager(cm)
|
|
|
+ .setDefaultRequestConfig(requestConfig)
|
|
|
+ .build();
|
|
|
+ return httpClient;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|