高并发下http优化

该博客探讨了在使用JMeter进行HTTPS压力测试时遇到的SocketOut和ThreadPoolShutdown问题。解决方案在于复用连接和资源,通过HTTP线程池管理来提高效率。代码示例展示了如何创建SSL客户端、配置连接池,并确保请求的可重用性,以避免资源泄露并提升压测性能。
摘要由CSDN通过智能技术生成

使用jmeter进行https压测,会造成socket out 和Thread Pool Shut Down ,造成这些原因是由于每次关闭此流并释放所有相关的系统资源。那么我们必须要复用连接和资源,主要针对此解决,让线程进行重复利用,利用http的线程池进行管理,从而达到高效!!!

 public static String doHttpsPost(String url, String jsonStr, Map<String,String> headerPram) throws ClientProtocolException, IOException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
        HttpPost httpPost =null ;
        try {
            httpClient = SSLClient.createSSLClientDefault();
            httpPost= new HttpPost(url);
            if(headerPram != null && !headerPram.isEmpty()) {
            	for(Map.Entry<String, String> entry:headerPram.entrySet()){
            		httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            StringEntity se = new StringEntity(jsonStr);
            se.setContentType("text/json");
            se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
            httpPost.setEntity(se);
            
            response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    return EntityUtils.toString(resEntity, charSet);

                }
            }
        }finally {

         /*   if (response.getEntity() != null&&response.getEntity().getContent() != null) {
                response.getEntity().getContent().close();
            }
        */
           if(httpPost!=null){
              //重置请求的内部状态,使其可重用。
               httpPost.releaseConnection();
           }
         /*   if(httpClient != null){
                 httpClient.close();
            }*/
            if(response != null){
               
                  response.close();
            }

        }
        return null;
    }
public class SSLClient {
    public static CloseableHttpClient createSSLClientDefault() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException{
        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).setConnectionManager(poolingHttpClientConnectionManager()).setConnectionManagerShared(true).setDefaultRequestConfig(defaultRequestConfig()).build();
    }

    
    /**
     * @description: 
     * @param:超时时间设置
     * @return: org.apache.http.client.config.RequestConfig
     * @author: miaodapeng
     * @time: 2021/12/9 9:34
     */    
    public static RequestConfig defaultRequestConfig(){

        return RequestConfig.custom()
                .setSocketTimeout(100000)
                .setConnectTimeout(100000)
                .setConnectionRequestTimeout(100000)
                .build();
    }

    /**
     * @description: 设置连接池
     * @param:[]
     * @return: org.apache.http.impl.conn.PoolingHttpClientConnectionManager
     * @author: miaodapeng
     * @time: 2021/12/9 9:34
     */    
    public static PoolingHttpClientConnectionManager poolingHttpClientConnectionManager(){
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(200);
        cm.setDefaultMaxPerRoute(20);
        return cm;
    }```

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

缪先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值