使用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;
}```