一、HTTP 的长短连接问题
HTTP1.0 当时用的还是短连接的方式,就是每次的请求都要建立一次TCP连接,创建TCP连接和关闭TCP的连接都是耗时的过程。
HTTP1.1 则对HTTP1.0做了很大的改进,默认使用的是长连接的方式。减少了建立连接和关闭连接的消耗。
二、httpClient 的使用
后端发送HTTP请求,一般使用的是apache里面的这个jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
不使用连接池的方式
import ch.qos.logback.core.encoder.EchoEncoder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Test;
public class HttpClientWithoutPoolTest extends BaseHttpClientTest {
@Test
public void test() throws Exception {
startUpAllThread(getRunThreads(new HttpTread()));
// 等待线程运行
for (;;){}
}
private class HttpTread implements Runnable {
@Override
public void run() {
// HttpClient 是线程安全的,正常使用应当用全局变量,但是一旦全局共用一个,httpclient内部构建的时候后new一个连接池。
// 这个是故意这个使用保证每次new 一个新的连接,不用线程池。
CloseableHttpClient httpClient = HttpClients.custom().build();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
long startTime = System.currentTimeMillis();
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response != null) {
response.close();
}
} catch (Exception e) {
}finally {
addCost(System.currentTimeMillis() - startTime);
if (NOW_COUNT.incrementAndGet() == REQUEST_COUNT) {
System.out.println(EVERY_REQ_COST.toString());
}
}
}
}
}
使用连接池的方式
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.http.HttpHost;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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.HttpClients;
import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.junit.Before;
import org.junit.Test;
public class HttpClientWithPoolTest extends BaseHttpClientTest {
private CloseableHttpClient httpClient = null;
@Before
public void before() {
initHttpClient();
}
@Test
public void test() throws Exception {
startUpAllThread(getRunThreads(new HttpTread()));
// 等待线程运行
for (;;){}
}
private class HttpTread implements Runnable {
@Override
public void run() {
HttpGet httpGet = new HttpGet("http://www.baidu.com");
long startTime = System.currentTimeMillis();
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response != null) {
response.close();
}
} catch (Exception e) {
}finally {
addCost(System.currentTimeMillis() - startTime);
if (NOW_COUNT.incrementAndGet() == REQUEST_COUNT) {
System.out.println(EVERY_REQ_COST.toString());
}
}
}
}
private void initHttpClient() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
//总连接池数
connectionManager.setMaxTotal(1);
//给每个域名设置单独的连接池数量
connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("www.baidu.com")), 1);
//setConnectTimeout 设置连接超时时间
//setConnectionRequestTimeout 从连接池中拿连接的等待超时时间
//setSocketTimeout 发出请求后等待端应答的超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(1000)
.setConnectionRequestTimeout(2000)
.setSocketTimeout(3000).build();
//重试机制
HttpRequestRetryHandler retryHandler = new StandardHttpRequestRetryHandler();
httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
.setRetryHandler(retryHandler)
.build();
/**
* 服务器端假设关闭了连接,对客户端是不透明的,httpClient为了解决这个问题,在某个连接使用之前会检测这个连接是否超时,如果过时则失效
* 这个做法会给每个请求增加一定的开销,因此有个定时器任务专门回收不活动且失效的连接,可以一定程度上解决这个问题。
*/
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1,
new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
// 关闭失效的链接并从连接池中移除
connectionManager.closeExpiredConnections();
//关闭20秒内不活动的连接并从连接池中移除
connectionManager.closeIdleConnections(20,TimeUnit.SECONDS);
} catch (Throwable e) {
e.printStackTrace();
}
}
},0,5, TimeUnit.SECONDS);
}
}
结论:使用连接池的方式可以提升整体的性能。