1、pom添加內容
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpasyncclient</artifactId> <version>4.1</version> </dependency>
2、創建httpUtil
public class HttpUtil { private static int socketTimeout = 1000;// 設置等待數據超時時間5秒鍾 根據業務調整 private static int connectTimeout = 2000;// 連接超時 private static int poolSize = 100;// 連接池最大連接數 private static int maxPerRoute = 10;// 每個主機的並發最多10 /** * 創建非異步的可關閉的且跳過https 驗證的 httpClient * @return */ public static CloseableHttpClient createSSLClientDefault() { try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } return HttpClients.createDefault(); } /** * 創建異步的可關閉的跳過https驗證的 httpClient對象 * @param connManager 連接管理器 可以調用本類的getConnManager 生成 * @return */ public static CloseableHttpAsyncClient getClient(PoolingNHttpClientConnectionManager connManager) { if (null == connManager) { return null; } // 設置連接參數 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HttpUtil.socketTimeout).setConnectTimeout(HttpUtil.connectTimeout).build(); // 創建自定義的httpclient對象 CloseableHttpAsyncClient client = HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig) .setConnectionManager(connManager).disableCookieManagement().build(); return client; } /** * 創建異步的可關閉的跳過https驗證的 httpClient對象(綁定本地網卡) * @param connManager * @param localAddress * @return * @throws */ public static CloseableHttpAsyncClient getClient(PoolingNHttpClientConnectionManager connManager, String localAddress) throws UnknownHostException { if (null == connManager || null == localAddress) { return null;