HTTP 连接池的使用

一、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);



    }
}

结论:使用连接池的方式可以提升整体的性能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值