One, background
Business development, often encounter downstream services by sending a request to http / https. Repeat-create the wheel every time a logical write HttpClient, and the performance, functionality uneven. Here sharing a high-performance, general-purpose tool belt Http client connection pool.
Please respect the author fruits of labor, reproduced, please indicate the original link: https://www.cnblogs.com/waterystone/p/11551280.html
Second, characteristics
- Apache performance based Http client org.apache.http.client.HttpClient;
- The maximum number of default connection pool 20, 200 can be specified by the system variable = -Dadu.common.http.max.total;
- The maximum number of connections per connection pool default routing is 2, -Dadu.common.http.max.per.route = 10 can be specified by the system variables;
- Timeout can be set, by httpOptions The set;
- Retry, set by HttpOptions.
Third, the source
Reference: https://github.com/waterystone/adu-test/blob/master/src/main/java/com/adu/utils/HttpClientUtil.java
package com.adu.utils; import com.adu.Constants; import com.adu.handler.HttpRequestRetryHandler; import com.adu.model.HttpOptions; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URLEncodedUtils; 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.entity.StringEntity; 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.message.BasicNameValuePair; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.TrustStrategy; import org.apache.http.util.EntityUtils; org.slf4j.Logger Import; * <Li> import org.slf4j.LoggerFactory; javax.net.ssl.SSLContext Import; Import java.nio.charset.StandardCharsets; Import java.security.cert.CertificateException; Import of java.security.cert.X509Certificate; Import of java.util.ArrayList; Import java.util.List; a java.util.Map Import; Import java.util.Objects; Import java.util.stream.Collectors; / ** * tools with the Http client connection pool. Has the following characteristics: * <OL> * <Li> {@link org.apache.http.client.HttpClient} apache based performance Http client; </ Li> * <Li> The maximum number of default connection pool 20, can -Dzzarch.common.http.max.total = 200 specified by the system variable; </ Li> * <Li> the maximum number of connections per connection pool default routing is 2, the system can be variable by -Dzzarch.common Specifies .http.max.per.route = 10; </ Li> * <Li> retries, set by {@link HttpOptions}; </ li > * @author duyunjie * @date 2019-09-18 16:33 */ public class HttpClientUtil { private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); /** * HttpClient 连接池 */ private static PoolingHttpClientConnectionManager CONNECTION_MANAGER = initPoolingHttpClientConnectionManager(); public static String httpGet(String url) throws Exception { return httpGet(url, null, null, null); } public static String httpGet(String url, HttpOptions httpOptions) throws Exception { return httpGet(url, null, null, httpOptions); } public static String HttpGet (String URL, the Map <String,?> the params) throws Exception { return HttpGet (URL, null, the params, null); } public static String HttpGet (String URL, the Map <? String,> the params, httpOptions The httpOptions The ) throws Exception { return HttpGet (URL, null, the params, httpOptions The); } public static String HttpGet (String URL, the Map <? String,> headers, the Map <? String,> the params) throws Exception { return HttpGet (URL, headers , the params, null); } / ** * send an HTTP GET request * * @param URL * @param request header headers * @param params request parameter * @param httpOptions configuration parameters, such as the number of retries, the timeout time. @Return * * @throws Exception * / public static String HttpGet (String URL, the Map <String,?> Headers, the Map <String,?> The params, httpOptions The httpOptions The) throws Exception { // load request address and parameter URIBuilder ub = new UriBuilder (); ub.setPath (URL); // convert request parameter List <of NameValuePairs> = convertParams2NVPS pairs (the params); IF {(pairs.isEmpty ()!) ub.setParameters (pairs); } HttpGet HttpGet = new new HttpGet (ub.build ()); // set request header IF (Objects.nonNull (headers)) { for (of Map.Entry <String,?> param: headers.entrySet ()) { httpGet.addHeader (param.getKey (), String.valueOf (param.getValue ())); } } return doHttp(httpGet, httpOptions); } public static String httpPost(String url, Map<String, ?> params) throws Exception { return httpPost(url, null, params, null); } public static String httpPost(String url, Map<String, ?> params, HttpOptions httpOptions) throws Exception { return httpPost(url, null, params, httpOptions); } public static String httpPost(String url, Map<String, ?> headers, Map<String, ?> params) throws Exception { return httpPost(url, headers, params, null); } /** * Sending HTTP POST request * * @param URL if (Objects.nonNull (headers)) { * @param headers 请求头 * @param params 请求参数 * @Param httpOptions configuration parameters, such as the number of retries, the timeout time. @Return * * @throws Exception * / public static String HttpPost (String url, the Map <? String,> headers, the Map <? String,> params, httpOptions The httpOptions The) throws Exception { HttpPost HttpPost = new new HttpPost (url); // conversion request parameter List <of NameValuePairs> = convertParams2NVPS pairs (the params); IF {(pairs.isEmpty ()!) httpPost.setEntity (new new UrlEncodedFormEntity (pairs, StandardCharsets.UTF_8.name ())); } // set the request header for (Map.Entry <String,?> param: headers.entrySet ()) { httpPost.addHeader (param.getKey (), String.valueOf (param.getValue ())); } } (HttpPost, httpOptions The) doHttp return; } / ** * transmitted HTTP POST request, in the format JSON * <P> request parameters are JSON format, the data is encoded. 8-UTF </ P> * * @param URL * @param param * @return * @throws Exception * / public static String httpPostJson (URL String, String param, httpOptions The httpOptions The) throws Exception { HttpPost HttpPost = HttpPost new new (URL); // set request header httpPost.addHeader ( "the Content-the Type", "file application / JSON; charset = UTF-. 8"); // set the request parameter httpPost.setEntity (new new StringEntity (param, StandardCharsets.UTF_8.name ())); return doHttp (HttpPost, httpOptions The); } / ** * transmitted HTTP POST request, in the format XML * <P> request parameter is in XML format, . 8-data encoding is UTF </ P> * * @param URL * @param param * @return * @throws Exception * / public static String httpPostXml (URL String, String param, httpOptions The httpOptions The) throws Exception { HttpPost HttpPost = new new HttpPost (URL); // set request header httpPost.addHeader ( "the Content-the Type", "file application / XML; charset = UTF-. 8"); // set the request parameter httpPost.setEntity (new new StringEntity (param, StandardCharsets.UTF_8.name ())); return doHttp (HttpPost, httpOptions The); } /** * conversion request parameters, the key-value pair stitched Map QueryString string * * @param the params * @ return * / public static String convertParams2QueryStr (the Map <String,?> the params) { List <of NameValuePairs> = convertParams2NVPS pairs (the params); return URLEncodedUtils.format (pairs, StandardCharsets.UTF_8.name ()); } / ** * conversion request parameter * * @param params * @return * / public static List <NameValuePair> convertParams2NVPS (the Map <String,?> params) { IF (the params == null) { return new new the ArrayList <> (); } return params.entrySet () Stream () Map (param -> new new BasicNameValuePair (param.getKey (), String.valueOf (param.getValue (.. .)))) the collect (Collectors.toList ()); } / ** * HTTP request * * @param request * @return * @throws Exception * / Private static String doHttp (HttpRequestBase request, httpOptions The httpOptions The) throws Exception { if (Objects.isNull (httpOptions)) { // If blank, use default. = new new httpOptions The httpOptions The (); } // set timeout IF (Objects.nonNull (httpOptions.getTimeoutMs ())) { request.setConfig (RequestConfig.custom () setSocketTimeout (httpOptions.getTimeoutMs ()) Build ()..); } // set the retry strategy HttpRequestRetryHandler httpRequestRetryHandler = null; if (Objects.nonNull(httpOptions.getRetryCount())) { httpRequestRetryHandler = new HttpRequestRetryHandler ( httpOptions.getRetryCount ()); } // objects acquired by the connection pool CloseableHttpClient httpClient = HttpClients.custom () setConnectionManager (CONNECTION_MANAGER) .setRetryHandler (httpRequestRetryHandler) .build ();. return a doRequest (httpClient, Request); } / * * * processing Http / Https request, and returns the result of the request * <p> NOTE: default request. 8-encoding UTF </ P> * * @param httpClient * @param request @Return * * @throws Exception * / Private static a doRequest String (httpClient CloseableHttpClient, HttpRequestBase Request) throws Exception { String Result = null; CloseableHttpResponse Response = null; the try { // Get request results Response = httpClient.execute (Request); // resolution request result the HttpEntity Entity response.getEntity = (); // conversion result result = EntityUtils.toString (Entity, StandardCharsets.UTF_8.name ()); // Close IO stream EntityUtils.consume (Entity); } the finally { IF (null = Response!) { response.close (); } } return Result; } / ** * initialize the connection pool * * @return * / static PoolingHttpClientConnectionManager initPoolingHttpClientConnectionManager Private () { // initialize the connection pool may be used to request HTTP / HTTPS (all trusted certificates) PoolingHttpClientConnectionManager ConnectionManager = new new PoolingHttpClientConnectionManager (getRegistry ()); // Maximum number of connections throughout the connection pool String maxTotal = System.getProperty (Constants.SYSTEM_PROPERTY_KEY_HTTP_MAX_TOTAL); IF (Objects.nonNull (maxTotal)) { connectionManager.setMaxTotal (Integer.valueOf (maxTotal)); } // maximum number of connections for each route String maxPerRoute = System.getProperty (Constants.SYSTEM_PROPERTY_KEY_HTTP_MAX_PER_ROUTE); IF (Objects.nonNull (maxPerRoute)) { connectionManager.setDefaultMaxPerRoute (Integer.valueOf (maxPerRoute)); } logger.info("[ZZARCH_COMMON_SUCCESS_initPoolingHttpClientConnectionManager]maxTotal={},maxPerRoute={}", maxTotal, maxPerRoute); return connectionManager; } /** * 获取 HTTPClient注册器 * * @return * @throws Exception */ private static Registry<ConnectionSocketFactory> getRegistry() { try { return RegistryBuilder.<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory()).register("https", getSSLFactory()).build(); } catch (Exception e) { logger.error ( "[ERROR_getRegistry]", E); } return null; } /** * Get HTTPS SSL connection factory * <p> skip certificate verification, i.e. trust all certificates </ P> * * @return * @throws Exception * / Private static SSLConnectionSocketFactory getSSLFactory () throws Exception { // set HTTPS SSL certificate information, certificate verification is skipped, i.e. trust all certificate requests the HTTPS SSLContextBuilder sslBuilder new new SSLContextBuilder = (). loadTrustMaterial (null, new new TrustStrategy () { @Override Boolean IsTrusted public (the X509Certificate [] catena alberghiera, the authType String) throws a CertificateException { return to true; } }); // Get the SSL certificate HTTPS connection context the SSLContext sslBuilder.build SSLContext = (); // get HTTPS connection factory SSLConnectionSocketFactory sslCsf = new SSLConnectionSocketFactory (sslContext, new String [] { "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1 .2 "}, null, NoopHostnameVerifier.INSTANCE); return sslCsf; } }
end