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;
}
String[] ipStrArr = localAddress.split("\\.");
if(ipStrArr.length != 4){
return null;
}
byte[] ip = new byte[]{ (byte)(Integer.parseInt(ipStrArr[0])), (byte) (Integer.parseInt(ipStrArr[1])), (byte) (Integer.parseInt(ipStrArr[2])), (byte) (Integer.parseInt(ipStrArr[3])) };
// 设置连接参数
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HttpUtil.socketTimeout).setConnectTimeout(HttpUtil.connectTimeout).setLocalAddress(InetAddress.getByAddress(ip)).build();
// 创建自定义的httpclient对象
CloseableHttpAsyncClient client = HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig)
.setConnectionManager(connManager).disableCookieManagement().build();
return client;
}
/**
* 初始化 连接管理器
* @return
*/
public static PoolingNHttpClientConnectionManager getConnManager() {
try {
// 绕过证书验证,处理https请求
SSLContext sslcontext = createIgnoreVerifySSL();
// 设置协议http和https对应的处理socket链接工厂的对象
Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder
.<SchemeIOSessionStrategy> create().register("http", NoopIOSessionStrategy.INSTANCE)
.register("https", new SSLIOSessionStrategy(sslcontext, new AllowAllHostnameVerifier())).build();
// 配置io线程
IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setConnectTimeout(HttpUtil.socketTimeout).setSoTimeout(HttpUtil.connectTimeout)
.setRcvBufSize(8192).setSndBufSize(8192).build();
// 设置连接池大小
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor, null,
sessionStrategyRegistry, null);
connManager.setDefaultMaxPerRoute(maxPerRoute);
connManager.setMaxTotal(poolSize);
return connManager;
} catch (IOReactorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 绕过验证
*
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static SSLContext createIgnoreVerifySSL() {
SSLContext sc = null;
try {
sc = SSLContext.getInstance("TLS");
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) {
}
@Override
public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sc.init(null, new TrustManager[] { trustManager }, null);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sc;
}
/**
* 从 request请求对象 中获取ip地址
* @param request
* @return
*/
public static String getIpAddress(HttpServletRequest request) {
if(request==null){
return null;
}
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
3、创建一个提供外部获取httpClient接口
/**
* AsynHttpClient工厂类
*/
@Component
public class AsynHttpClientFactory {
private static Logger logger = LoggerFactory.getLogger(AsynHttpClientFactory.class);
private static CloseableHttpAsyncClient httpclient;
private PoolingNHttpClientConnectionManager connManager;
public AsynHttpClientFactory() {
connManager = HttpUtil.getConnManager();
httpclient = HttpUtil.getClient(connManager);
httpclient.start();
logger.info("异步httpClient启动完成");
}
public static CloseableHttpAsyncClient getCloseableHttpAsyncClient(){
return httpclient;
}
}
4、测试方法
单个url发送
@Test
public void run(){
CloseableHttpAsyncClient httpClient =AsynHttpClientFactory.getCloseableHttpAsyncClient();
long startTime = System.currentTimeMillis();
final HttpGet request = new HttpGet("http://www.apache.org");
final Future future = httpClient.execute(request, null);
try {
HttpResponse response = (HttpResponse) future.get();
System.out.println("Response:" + request.getURI()+"; "+ response.getStatusLine());
System.out.println("Shutting down");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}finally{
try {
httpClient.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
long endTime = System.currentTimeMillis();
System.out.println("执行完毕, 耗时:"+(endTime-startTime)/1000+" 秒");
}
多条url发送
@Test
public void moreRequest(){
CloseableHttpAsyncClient httpClient =AsynHttpClientFactory.getCloseableHttpAsyncClient();
long startTime = System.currentTimeMillis();
final HttpGet[] requests = new HttpGet[10];
for (int i = 0; i < requests.length; i++) {
requests[i] = new HttpGet("http://www.apache.org/");
}
final CountDownLatch latch = new CountDownLatch(requests.length);
for(final HttpGet request: requests){
httpClient.execute(request, new FutureCallback(){
@Override
public void completed(Object obj) {
final HttpResponse response = (HttpResponse)obj;
latch.countDown();
System.out.println(request.getURI() + "->" + response.getStatusLine());
}
@Override
public void failed(Exception excptn) {
latch.countDown();
System.out.println(request.getURI() + "->" + excptn);
}
@Override
public void cancelled() {
latch.countDown();
System.out.println(request.getURI() + "cancelled");
}
});
}
try {
latch.await();
System.out.println("Shutting Down");
} catch (InterruptedException ex) {
System.out.println(ex.getMessage());
}finally{
try {
httpClient.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
long endTime = System.currentTimeMillis();
System.out.println("执行完毕, 耗时:"+(endTime-startTime)/1000+" 秒");
}
网友评论