java httpclient cpu_httpclient并发导致溢出,cpu飙升等问题,然后自定义CloseableHttpClient连接池...

import com.alibaba.fastjson.JSONObject;

import fangrong.com.cn.constant.ResultCode;

import lombok.extern.slf4j.Slf4j;

import org.apache.http.*;

import org.apache.http.client.HttpRequestRetryHandler;

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.protocol.HttpClientContext;

import org.apache.http.conn.ConnectTimeoutException;

import org.apache.http.conn.ConnectionKeepAliveStrategy;

import org.apache.http.entity.StringEntity;

import org.apache.http.entity.mime.MultipartEntityBuilder;

import org.apache.http.entity.mime.content.FileBody;

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.protocol.BasicHttpContext;

import org.apache.http.protocol.HttpContext;

import org.apache.http.util.EntityUtils;

import java.io.File;

import java.io.IOException;

import java.io.InterruptedIOException;

import java.net.UnknownHostException;

import java.nio.charset.Charset;

import java.nio.charset.StandardCharsets;

import java.util.ArrayList;

import java.util.List;

@Slf4j

public class HttpUtil {

private HttpUtil() {

}

private final static PoolingHttpClientConnectionManager POOLCONNMANAGER = new PoolingHttpClientConnectionManager();

private final static HttpRequestRetryHandler HTTPREQUESTRETRYHANDLER = (exception, executionCount, context) -> {

if (executionCount >= 3) {

return false;

}

if (exception instanceof NoHttpResponseException) {

return true;

}

if (exception instanceof InterruptedIOException) {

return false;

}

if (exception instanceof UnknownHostException) {

return false;

}

HttpClientContext clientContext = HttpClientContext.adapt(context);

HttpRequest request = clientContext.getRequest();

return !(request instanceof HttpEntityEnclosingRequest);

};

static { //类加载的时候 设置最大连接数 和 每个路由的最大连接数

POOLCONNMANAGER.setMaxTotal(500);

POOLCONNMANAGER.setDefaultMaxPerRoute(100);

}

private static RequestConfig getRequestConfig() {

return RequestConfig.custom()

.setSocketTimeout(10 * 1000)

.setConnectTimeout(10 * 1000)

.setConnectionRequestTimeout(10 * 1000)

.build();

}

private static CloseableHttpClient getCloseableHttpClient() {

ConnectionKeepAliveStrategy connectionKeepAliveStrategy = (httpResponse, httpContext) -> {

return 20 * 1000; // tomcat默认keepAliveTimeout为20s

};

return HttpClients.custom()

.setDefaultRequestConfig(getRequestConfig())

.setConnectionManager(POOLCONNMANAGER)

.setRetryHandler(HTTPREQUESTRETRYHANDLER)

.setKeepAliveStrategy(connectionKeepAliveStrategy)

.build();

}

/**

* get

*

* @param url String

* @return String

*/

public static String get(String url) {

CloseableHttpClient httpClient = getCloseableHttpClient();

CloseableHttpResponse response = null;

try {

Charset charset = StandardCharsets.UTF_8;

HttpGet httpget = new HttpGet(url);

response = httpClient.execute(httpget);

HttpEntity entity = response.getEntity();

return EntityUtils.toString(entity, charset);

} catch (Exception e) {

log.error("请求异常:{}", e);

} finally {

if (null != response) {

try {

response.close();

} catch (IOException e) {

log.error("关闭资源失败", e);

}

}

}

return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值