Authored by LUOXC

异步构建httpclient

package com.yohoufo.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
... ... @@ -34,162 +35,184 @@ import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Slf4j
@Component
public class HttpClient {
private Logger logger = LoggerFactory.getLogger(getClass());
protected static final int connectionRequestTimeout = 6000;
protected static final int connectionTimeout = 3000;
protected static final int socketTimeout = 2000;
//最大总数
private final int maxTotal = 30;
//默认并发数
private final int defaultMaxPerRoute = 10;
protected CloseableHttpClient httpClient;
@PostConstruct
public void init() throws Exception{
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,
new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
return true;
}
}).build();
b.setSSLContext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if
// you don't want to weaken
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened
// "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory> create()
.register("http",
PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
socketFactoryRegistry);
connMgr.setMaxTotal(maxTotal);
connMgr.setDefaultMaxPerRoute(defaultMaxPerRoute);
b.setConnectionManager(connMgr);
//request config
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(socketTimeout)
.build();
b.setDefaultRequestConfig(requestConfig);
// finally, build the HttpClient;
// -- done!
httpClient = b.build();
}
public void destroy() {
if(httpClient != null)
httpClient.getConnectionManager().shutdown();
}
/**
* Get请求
* @param url
* @return
* @throws Exception
*/
public String get(String url) throws Exception {
HttpGet httpget = new HttpGet(url);
private Logger logger = LoggerFactory.getLogger(getClass());
protected static final int connectionRequestTimeout = 6000;
protected static final int connectionTimeout = 3000;
protected static final int socketTimeout = 2000;
//最大总数
private final int maxTotal = 30;
//默认并发数
private final int defaultMaxPerRoute = 10;
private CloseableHttpClient httpClient;
@PostConstruct
public void init() {
new Thread(() -> {
while (Objects.isNull(httpClient)) {
try {
httpClient = this.buildHttpClient();
} catch (Exception e) {
log.warn("build http client fail {}", e.getMessage());
}
try {
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
}
}
}, "http-client-builder").start();
}
public CloseableHttpClient buildHttpClient() throws Exception {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,
new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
return true;
}
}).build();
b.setSSLContext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if
// you don't want to weaken
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened
// "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory>create()
.register("http",
PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
socketFactoryRegistry);
connMgr.setMaxTotal(maxTotal);
connMgr.setDefaultMaxPerRoute(defaultMaxPerRoute);
b.setConnectionManager(connMgr);
//request config
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(socketTimeout)
.build();
b.setDefaultRequestConfig(requestConfig);
// finally, build the HttpClient;
// -- done!
return b.build();
}
public void destroy() {
if (httpClient != null)
httpClient.getConnectionManager().shutdown();
}
/**
* Get请求
*
* @param url
* @return
* @throws Exception
*/
public String get(String url) throws Exception {
HttpGet httpget = new HttpGet(url);
return sendHttpRequest(httpget);
}
/**
* Post请求
* @param url
* @param body
* @return
* @throws Exception
*/
public String post(String url, String body) throws Exception {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setEntity(new StringEntity(body, "UTF-8"));
return sendHttpRequest(httpPost);
}
/**
* 提交form-data
* @param url
* @param formDataMap
* @return
* @throws Exception
*/
public String postFormData(String url, Map<String, String> formDataMap) throws Exception {
HttpPost httpPost = new HttpPost(url);
//httpPost.addHeader("Content-Type", "multipart/form-data;charset=UTF-8");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
if(formDataMap != null) {
for(Map.Entry<String, String> data : formDataMap.entrySet()) {
nvps.add(new BasicNameValuePair(data.getKey(), data.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
return sendHttpRequest(httpPost);
}
/**
* http发送请求
* @param httpRequest
* @return
* @throws Exception
*/
private String sendHttpRequest(HttpUriRequest httpRequest) throws Exception {
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpRequest);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
logger.error("http response code: {}, request: {}", statusCode, httpRequest.getURI());
throw new RuntimeException("http response status: " + statusCode);
}
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
finally {
if (response != null) {
try {
response.close();
} catch (Exception e) {
logger.error("pay httpclient resp close failed: {}", e.getMessage());
}
}
if(httpRequest != null) {
try {
httpRequest.abort();
} catch (Exception e) {
logger.error("pay httpclient req abort failed: {}", e.getMessage());
}
}
}
}
}
/**
* Post请求
*
* @param url
* @param body
* @return
* @throws Exception
*/
public String post(String url, String body) throws Exception {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setEntity(new StringEntity(body, "UTF-8"));
return sendHttpRequest(httpPost);
}
/**
* 提交form-data
*
* @param url
* @param formDataMap
* @return
* @throws Exception
*/
public String postFormData(String url, Map<String, String> formDataMap) throws Exception {
HttpPost httpPost = new HttpPost(url);
//httpPost.addHeader("Content-Type", "multipart/form-data;charset=UTF-8");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (formDataMap != null) {
for (Map.Entry<String, String> data : formDataMap.entrySet()) {
nvps.add(new BasicNameValuePair(data.getKey(), data.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
return sendHttpRequest(httpPost);
}
/**
* http发送请求
*
* @param httpRequest
* @return
* @throws Exception
*/
private String sendHttpRequest(HttpUriRequest httpRequest) throws Exception {
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpRequest);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
logger.error("http response code: {}, request: {}", statusCode, httpRequest.getURI());
throw new RuntimeException("http response status: " + statusCode);
}
return EntityUtils.toString(response.getEntity(), "UTF-8");
} finally {
if (response != null) {
try {
response.close();
} catch (Exception e) {
logger.error("pay httpclient resp close failed: {}", e.getMessage());
}
}
if (httpRequest != null) {
try {
httpRequest.abort();
} catch (Exception e) {
logger.error("pay httpclient req abort failed: {}", e.getMessage());
}
}
}
}
}
... ...
... ... @@ -9,6 +9,7 @@ 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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
... ... @@ -33,8 +34,7 @@ public abstract class HttpSslClientAbstract extends HttpClient {
@Override
@PostConstruct
public void init() throws Exception {
public CloseableHttpClient buildHttpClient() throws Exception {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
... ... @@ -82,7 +82,7 @@ public abstract class HttpSslClientAbstract extends HttpClient {
// finally, buildSellerBo the HttpClient;
// -- done!
httpClient = b.build();
return b.build();
}
private void loadKeyMaterial(SSLContextBuilder sslContextBuilder) throws Exception {
... ...