下午,经理找我说Php的curl的在高峰期经常出现连接超时的情况,Java会不会呢,让用httpclient来做一下测试。我就作了两个对比测试。一个是httpclient没用连接池,一个有用连接池。使用spring boot做web服务,选择百度、bing用jmeter web请求测试。为什么选择这两个,因为这两个不容易报错,其他搜索引擎多请求几次就报错了。
spirng boot web的控制器代码如下
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.Random;
@RestController
public class IndexController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private static String URLARRAY[] = {"https://www.baidu.com/s?wd=%s", "https://cn.bing.com/search?q=%s"};
@RequestMapping("/")
public String index(@RequestParam(value = "name", defaultValue = "helloworld") String name) throws IOException {
int i = new Random().nextInt(URLARRAY.length);
String url = URLARRAY[i];
url = String.format(url, name);
logger.debug("调用URL是{}", url);
try {
String value = HttpClientUtils.get(url);
} catch (Exception ex) {
logger.error("出错了", ex);
throw ex;
}
return "成功了" + url;
}
}
这个核心调用的是HttpClientUtils.get(url)来请求web。
首先贴上没有使用连接池的的httpClinetUtils代码
public class HttpClientUtils {
private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
private static RequestConfig REQUESTCONFIG = RequestConfig.custom()
.setSocketTimeout(1000)
.setConnectTimeout(1000)
.setConnectionRequestTimeout(1000)
.build();
public static String get(String url) throws IOException {
CloseableHttpClient httpclient = HttpClientBuilder.create().setRetryHandler((exception, executionCount, context) -> {
return false;
}).setDefaultRequestConfig(REQUESTCONFIG).build();
try {
HttpGet httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(
final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
logger.error("请求{},出现了错误{}", url, status);
return "出错了";
//throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
String responseBody = httpclient.execute(httpget, responseHandler);
return responseBody;
} finally {
httpclient.close();
}
}
}
可以看到没用连接池情况下,httpclient不能重复使用,使用完要及时关闭
Jmete50个线程下测试结果
可以看到并发数才90,而且出错率达到了11%
使用连接池的httpClinetUtils代码
package com.example.testhttpclient.utils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Map;
public class HttpClientUtils {
private static CloseableHttpClient HTTPCLIENT;
private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
private static RequestConfig REQUESTCONFIG = RequestConfig.custom()
.setSocketTimeout(1000)
.setConnectTimeout(1000)
.setConnectionRequestTimeout(1000)
.build();
static PoolingHttpClientConnectionManager cm;
static {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
HTTPCLIENT = HttpClientBuilder.create().setRetryHandler((exception, executionCount, context) -> {
return false;
}).setDefaultRequestConfig(REQUESTCONFIG).setConnectionManager(cm).build();
}
public static String get(String url) throws IOException {
try {
HttpGet httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(
final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
logger.error("请求{},出现了错误{}", url, status);
return "出错了";
//throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
String responseBody = HTTPCLIENT.execute(httpget, responseHandler);
return responseBody;
} finally {
}
}
}
jmeter50个线程测试结果
可以看到并发数达到了500,错误率也降到了0.19%,性能提高了5倍不止。
结论:
所以在高并发情况下,使用是http连接池很重要,不然并发数最高才90,而且还容易超时
下面是jmeter的脚本文件,供参考
链接: https://pan.baidu.com/s/144wbx3LGowHLkPcvsMRUZg 提取码: jhnc 复制这段内容后打开百度网盘手机App,操作更方便哦