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;
    }
}
发布了97 篇原创文章 · 获赞 44 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/wangh92/article/details/100521011
今日推荐