HttpClient优化思路:

1、池化

2、长连接

3、httpclient和httpget复用

4、合理的配置参数(最大并发请求数,各种超时时间,重试次数)

5、异步

6、多读源码

1.背景
我们有个业务,会调用其他部门提供的一个基于http的服务,日调用量在千万级别。使用了httpclient来完成业务。之前因为qps上不去,就看了一下业务代码,并做了一些优化,记录在这里。

先对比前后:优化之前,平均执行时间是250ms;优化之后,平均执行时间是80ms,降低了三分之二的消耗,容器不再动不动就报警线程耗尽了,清爽~

2.分析
项目的原实现比较粗略,就是每次请求时初始化一个httpclient,生成一个httpPost对象,执行,然后从返回结果取出entity,保存成一个字符串,最后显式关闭response和client。我们一点点分析和优化:

2.1 httpclient反复创建开销

httpclient是一个线程安全的类,没有必要由每个线程在每次使用时创建,全局保留一个即可。

2.2 反复创建tcp连接的开销

tcp的三次握手与四次挥手两大裹脚布过程,对于高频次的请求来说,消耗实在太大。试想如果每次请求我们需要花费5ms用于协商过程,那么对于qps为100的单系统,1秒钟我们就要花500ms用于握手和挥手。又不是高级领导,我们程序员就不要搞这么大做派了,改成keep alive方式以实现连接复用!

2.3 重复缓存entity的开销

原本的逻辑里,使用了如下代码:

HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity);
这里我们相当于额外复制了一份content到一个字符串里,而原本的httpResponse仍然保留了一份content,需要被consume掉,在高并发且content非常大的情况下,会消耗大量内存。并且,我们需要显式的关闭连接,ugly。

3.实现
按上面的分析,我们主要要做三件事:一是单例的client,二是缓存的保活连接,三是更好的处理返回结果。一就不说了,来说说二。

提到连接缓存,很容易联想到数据库连接池。httpclient4提供了一个PoolingHttpClientConnectionManager 作为连接池。接下来我们通过以下步骤来优化:

3.1 定义一个keep alive strategy

关于keep-alive,本文不展开说明,只提一点,是否使用keep-alive要根据业务情况来定,它并不是灵丹妙药。还有一点,keep-alive和time_wait/close_wait之间也有不少故事。

在本业务场景里,我们相当于有少数固定客户端,长时间极高频次的访问服务器,启用keep-alive非常合适

再多提一嘴,http的keep-alive 和tcp的KEEPALIVE不是一个东西。回到正文,定义一个strategy如下:

ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator
(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase
("timeout")) {
return Long.parseLong(value) * 1000;
}
}
return 60 * 1000;//如果没有约定,则默认定义时长为60s
}
};
3.2 配置一个PoolingHttpClientConnectionManager

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(500);
connectionManager.setDefaultMaxPerRoute(50);//例如默认每路由最高50并发,具体依据业务来定
也可以针对每个路由设置并发数。

3.3 生成httpclient

httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setKeepAliveStrategy(kaStrategy)
.setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build())
.build();
注意:使用setStaleConnectionCheckEnabled方法来逐出已被关闭的链接不被推荐。

更好的方式是手动启用一个线程,定时运行closeExpiredConnections 和closeIdleConnections方法,如下所示。

public static class IdleConnectionMonitorThread extends Thread {

private final HttpClientConnectionManager connMgr;
private volatile boolean shutdown;

public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
super();
this.connMgr = connMgr;
}

@Override
public void run() {
try {
while (!shutdown) {
synchronized (this) {
wait(5000);
// Close expired connections
connMgr.closeExpiredConnections();
// Optionally, close connections
// that have been idle longer than 30 sec
connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
}
}
} catch (InterruptedException ex) {
// terminate
}
}

public void shutdown() {
shutdown = true;
synchronized (this) {
notifyAll();
}
}

}
3.4 使用httpclient执行method时降低开销

这里要注意的是,不要关闭connection。

一种可行的获取内容的方式类似于,把entity里的东西复制一份:

res = EntityUtils.toString(response.getEntity(),"UTF-8");
EntityUtils.consume(response1.getEntity());
但是,更推荐的方式是定义一个ResponseHandler,方便你我他,不再自己catch异常和关闭流。在此我们可以看一下相关的源码:

public <T> T execute(final HttpHost target, final HttpRequest request,
final ResponseHandler<? extends T> responseHandler, final HttpContext context)
throws IOException, ClientProtocolException {
Args.notNull(responseHandler, "Response handler");

final HttpResponse response = execute(target, request, context);

final T result;
try {
result = responseHandler.handleResponse(response);
} catch (final Exception t) {
final HttpEntity entity = response.getEntity();
try {
EntityUtils.consume(entity);
} catch (final Exception t2) {
// Log this exception. The original exception is more
// important and will be thrown to the caller.
this.log.warn("Error consuming content after an exception.", t2);
}
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
if (t instanceof IOException) {
throw (IOException) t;
}
throw new UndeclaredThrowableException(t);
}

// Handling the response was successful. Ensure that the content has
// been fully consumed.
final HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);//看这里看这里
return result;
}
可以看到,如果我们使用resultHandler执行execute方法,会最终自动调用consume方法,而这个consume方法如下所示:

public static void consume(final HttpEntity entity) throws IOException {
if (entity == null) {
return;
}
if (entity.isStreaming()) {
final InputStream instream = entity.getContent();
if (instream != null) {
instream.close();
}
}
}
可以看到最终它关闭了输入流。

4.其他
通过以上步骤,基本就完成了一个支持高并发的httpclient的写法,下面是一些额外的配置和提醒:

4.1 httpclient的一些超时配置

CONNECTION_TIMEOUT是连接超时时间,SO_TIMEOUT是socket超时时间,这两者是不同的。连接超时时间是发起请求前的等待时间;socket超时时间是等待数据的超时时间。

HttpParams params = new BasicHttpParams();
//设置连接超时时间
Integer CONNECTION_TIMEOUT = 2 * 1000; //设置请求超时2秒钟 根据业务调整
Integer SO_TIMEOUT = 2 * 1000; //设置等待数据超时时间2秒钟 根据业务调整

//定义了当从ClientConnectionManager中检索ManagedClientConnection实例时使用的毫秒级的超时时间
//这个参数期望得到一个java.lang.Long类型的值。如果这个参数没有被设置,默认等于CONNECTION_TIMEOUT,因此一定要设置。
Long CONN_MANAGER_TIMEOUT = 500L; //在httpclient4.2.3中我记得它被改成了一个对象导致直接用long会报错,后来又改回来了

params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT);
params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT);
//在提交请求之前 测试连接是否可用
params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);

//另外设置http client的重试次数,默认是3次;当前是禁用掉(如果项目量不到,这个默认即可)
httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
4.2 如果配置了nginx的话,nginx也要设置面向两端的keep-alive

现在的业务里,没有nginx的情况反而比较稀少。nginx默认和client端打开长连接而和server端使用短链接。注意client端的keepalive_timeout和keepalive_requests参数,以及upstream端的keepalive参数设置,这三个参数的意义在此也不再赘述。

以上就是我的全部设置。通过这些设置,成功地将原本每次请求250ms的耗时降低到了80左右,效果显著。

JAR包如下:

<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
代码如下:

//Basic认证
private static final CredentialsProvider credsProvider = new BasicCredentialsProvider();
//httpClient
private static final CloseableHttpClient httpclient;
//httpGet方法
private static final HttpGet httpget;
//
private static final RequestConfig reqestConfig;
//响应处理器
private static final ResponseHandler<String> responseHandler;
//jackson解析工具
private static final ObjectMapper mapper = new ObjectMapper();
static {
System.setProperty("http.maxConnections","50");
System.setProperty("http.keepAlive", "true");
//设置basic校验
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
new UsernamePasswordCredentials("", ""));
//创建http客户端
httpclient = HttpClients.custom()
.useSystemProperties()
.setRetryHandler(new DefaultHttpRequestRetryHandler(3,true))
.setDefaultCredentialsProvider(credsProvider)
.build();

//初始化httpGet
httpget = new HttpGet();

//初始化HTTP请求配置
reqestConfig = RequestConfig.custom()
.setContentCompressionEnabled(true)
.setSocketTimeout(100)
.setAuthenticationEnabled(true)
.setConnectionRequestTimeout(100)
.setConnectTimeout(100).build();
httpget.setConfig(reqestConfig);
//初始化response解析器
responseHandler = new BasicResponseHandler();
}

/*
* 功能:返回响应
*/
public static String getResponse(String url) throws IOException {
HttpGet get = new HttpGet(url);
String response = httpclient.execute(get,responseHandler);
return response;
}

/*
* 功能:发送http请求,并用net.sf.json工具解析
*/
public static JSONObject getUrl(String url) throws Exception{
try {
httpget.setURI(URI.create(url));
String response = httpclient.execute(httpget,responseHandler);
JSONObject json = JSONObject.fromObject(response);
return json;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/*
* 功能:发送http请求,并用jackson工具解析
*/
public static JsonNode getUrl2(String url){
try {
httpget.setURI(URI.create(url));
String response = httpclient.execute(httpget,responseHandler);
JsonNode node = mapper.readTree(response);
return node;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/*
* 功能:发送http请求,并用fastjson工具解析
*/
public static com.alibaba.fastjson.JSONObject getUrl3(String url){
try {
httpget.setURI(URI.create(url));
String response = httpclient.execute(httpget,responseHandler);
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response);
return jsonObject;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

HttpClient优化思路1、池化 2、长连接 3、httpclient和httpget复用 4、合理的配置参数(最大并发请求数,各种超时时间,重试次数)5、异步 6、多读源码
1.背景我们有个业务,会调用其他部门提供的一个基于http的服务,日调用量在千万级别。使用了httpclient来完成业务。之前因为qps上不去,就看了一下业务代码,并做了一些优化,记录在这里。
先对比前后:优化之前,平均执行时间是250ms;优化之后,平均执行时间是80ms,降低了三分之二的消耗,容器不再动不动就报警线程耗尽了,清爽~
2.分析项目的原实现比较粗略,就是每次请求时初始化一个httpclient,生成一个httpPost对象,执行,然后从返回结果取出entity,保存成一个字符串,最后显式关闭response和client。我们一点点分析和优化:
2.1 httpclient反复创建开销
httpclient是一个线程安全的类,没有必要由每个线程在每次使用时创建,全局保留一个即可。
2.2 反复创建tcp连接的开销
tcp的三次握手与四次挥手两大裹脚布过程,对于高频次的请求来说,消耗实在太大。试想如果每次请求我们需要花费5ms用于协商过程,那么对于qps为100的单系统,1秒钟我们就要花500ms用于握手和挥手。又不是高级领导,我们程序员就不要搞这么大做派了,改成keep alive方式以实现连接复用!
2.3 重复缓存entity的开销
原本的逻辑里,使用了如下代码:
HttpEntity entity = httpResponse.getEntity();String response = EntityUtils.toString(entity);这里我们相当于额外复制了一份content到一个字符串里,而原本的httpResponse仍然保留了一份content,需要被consume掉,在高并发且content非常大的情况下,会消耗大量内存。并且,我们需要显式的关闭连接,ugly。
3.实现按上面的分析,我们主要要做三件事:一是单例的client,二是缓存的保活连接,三是更好的处理返回结果。一就不说了,来说说二。
提到连接缓存,很容易联想到数据库连接池。httpclient4提供了一个PoolingHttpClientConnectionManager 作为连接池。接下来我们通过以下步骤来优化:
3.1 定义一个keep alive strategy
关于keep-alive,本文不展开说明,只提一点,是否使用keep-alive要根据业务情况来定,它并不是灵丹妙药。还有一点,keep-alive和time_wait/close_wait之间也有不少故事。
在本业务场景里,我们相当于有少数固定客户端,长时间极高频次的访问服务器,启用keep-alive非常合适
再多提一嘴,http的keep-alive 和tcp的KEEPALIVE不是一个东西。回到正文,定义一个strategy如下:
ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {    @Override    public long getKeepAliveDuration(HttpResponse response, HttpContext context) {        HeaderElementIterator it = new BasicHeaderElementIterator            (response.headerIterator(HTTP.CONN_KEEP_ALIVE));        while (it.hasNext()) {            HeaderElement he = it.nextElement();            String param = he.getName();            String value = he.getValue();            if (value != null && param.equalsIgnoreCase               ("timeout")) {                return Long.parseLong(value) * 1000;            }        }        return 60 * 1000;//如果没有约定,则默认定义时长为60s    }};3.2 配置一个PoolingHttpClientConnectionManager
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();connectionManager.setMaxTotal(500);connectionManager.setDefaultMaxPerRoute(50);//例如默认每路由最高50并发,具体依据业务来定也可以针对每个路由设置并发数。
3.3 生成httpclient
httpClient = HttpClients.custom()                .setConnectionManager(connectionManager)                .setKeepAliveStrategy(kaStrategy)                .setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build())                .build();注意:使用setStaleConnectionCheckEnabled方法来逐出已被关闭的链接不被推荐。更好的方式是手动启用一个线程,定时运行closeExpiredConnections 和closeIdleConnections方法,如下所示。
public static class IdleConnectionMonitorThread extends Thread {        private final HttpClientConnectionManager connMgr;    private volatile boolean shutdown;        public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {        super();        this.connMgr = connMgr;    }     @Override    public void run() {        try {            while (!shutdown) {                synchronized (this) {                    wait(5000);                    // Close expired connections                    connMgr.closeExpiredConnections();                    // Optionally, close connections                    // that have been idle longer than 30 sec                    connMgr.closeIdleConnections(30, TimeUnit.SECONDS);                }            }        } catch (InterruptedException ex) {            // terminate        }    }        public void shutdown() {        shutdown = true;        synchronized (this) {            notifyAll();        }    }    }3.4 使用httpclient执行method时降低开销
这里要注意的是,不要关闭connection。
一种可行的获取内容的方式类似于,把entity里的东西复制一份:
res = EntityUtils.toString(response.getEntity(),"UTF-8");EntityUtils.consume(response1.getEntity());但是,更推荐的方式是定义一个ResponseHandler,方便你我他,不再自己catch异常和关闭流。在此我们可以看一下相关的源码:
public <T> T execute(final HttpHost target, final HttpRequest request,            final ResponseHandler<? extends T> responseHandler, final HttpContext context)            throws IOException, ClientProtocolException {        Args.notNull(responseHandler, "Response handler");         final HttpResponse response = execute(target, request, context);         final T result;        try {            result = responseHandler.handleResponse(response);        } catch (final Exception t) {            final HttpEntity entity = response.getEntity();            try {                EntityUtils.consume(entity);            } catch (final Exception t2) {                // Log this exception. The original exception is more                // important and will be thrown to the caller.                this.log.warn("Error consuming content after an exception.", t2);            }            if (t instanceof RuntimeException) {                throw (RuntimeException) t;            }            if (t instanceof IOException) {                throw (IOException) t;            }            throw new UndeclaredThrowableException(t);        }         // Handling the response was successful. Ensure that the content has        // been fully consumed.        final HttpEntity entity = response.getEntity();        EntityUtils.consume(entity);//看这里看这里        return result;    }可以看到,如果我们使用resultHandler执行execute方法,会最终自动调用consume方法,而这个consume方法如下所示:
public static void consume(final HttpEntity entity) throws IOException {        if (entity == null) {            return;        }        if (entity.isStreaming()) {            final InputStream instream = entity.getContent();            if (instream != null) {                instream.close();            }        }    }可以看到最终它关闭了输入流。
4.其他通过以上步骤,基本就完成了一个支持高并发的httpclient的写法,下面是一些额外的配置和提醒:
4.1 httpclient的一些超时配置
CONNECTION_TIMEOUT是连接超时时间,SO_TIMEOUT是socket超时时间,这两者是不同的。连接超时时间是发起请求前的等待时间;socket超时时间是等待数据的超时时间。
HttpParams params = new BasicHttpParams();//设置连接超时时间Integer CONNECTION_TIMEOUT = 2 * 1000; //设置请求超时2秒钟 根据业务调整Integer SO_TIMEOUT = 2 * 1000; //设置等待数据超时时间2秒钟 根据业务调整 //定义了当从ClientConnectionManager中检索ManagedClientConnection实例时使用的毫秒级的超时时间//这个参数期望得到一个java.lang.Long类型的值。如果这个参数没有被设置,默认等于CONNECTION_TIMEOUT,因此一定要设置。Long CONN_MANAGER_TIMEOUT = 500L; //在httpclient4.2.3中我记得它被改成了一个对象导致直接用long会报错,后来又改回来了 params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT);params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT);//在提交请求之前 测试连接是否可用params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true); //另外设置http client的重试次数,默认是3次;当前是禁用掉(如果项目量不到,这个默认即可)httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));4.2 如果配置了nginx的话,nginx也要设置面向两端的keep-alive
现在的业务里,没有nginx的情况反而比较稀少。nginx默认和client端打开长连接而和server端使用短链接。注意client端的keepalive_timeout和keepalive_requests参数,以及upstream端的keepalive参数设置,这三个参数的意义在此也不再赘述。
以上就是我的全部设置。通过这些设置,成功地将原本每次请求250ms的耗时降低到了80左右,效果显著。
JAR包如下:
<!-- httpclient --><dependency>    <groupId>org.apache.httpcomponents</groupId>    <artifactId>httpclient</artifactId>    <version>4.5.6</version></dependency>代码如下:
//Basic认证private static final CredentialsProvider credsProvider = new BasicCredentialsProvider();//httpClientprivate static final CloseableHttpClient httpclient;//httpGet方法private static final HttpGet httpget;//private static final RequestConfig reqestConfig;//响应处理器private static final ResponseHandler<String> responseHandler;//jackson解析工具private static final ObjectMapper mapper = new ObjectMapper();static {    System.setProperty("http.maxConnections","50");    System.setProperty("http.keepAlive", "true");    //设置basic校验    credsProvider.setCredentials(            new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),            new UsernamePasswordCredentials("", ""));    //创建http客户端    httpclient = HttpClients.custom()            .useSystemProperties()            .setRetryHandler(new DefaultHttpRequestRetryHandler(3,true))            .setDefaultCredentialsProvider(credsProvider)            .build();    //初始化httpGet    httpget = new HttpGet();    //初始化HTTP请求配置    reqestConfig = RequestConfig.custom()            .setContentCompressionEnabled(true)            .setSocketTimeout(100)            .setAuthenticationEnabled(true)            .setConnectionRequestTimeout(100)            .setConnectTimeout(100).build();    httpget.setConfig(reqestConfig);    //初始化response解析器    responseHandler = new BasicResponseHandler();}/* * 功能:返回响应 * @author zhangdaquan * @date 2019/1/3 上午11:19 * @param [url] * @return org.apache.http.client.methods.CloseableHttpResponse * @exception */public static String getResponse(String url) throws IOException {    HttpGet get = new HttpGet(url);    String response = httpclient.execute(get,responseHandler);    return response;} /* * 功能:发送http请求,并用net.sf.json工具解析 * @author zhangdaquan * @date 2018/8/15 下午2:21 * @param [url] * @return org.json.JSONObject * @exception */public static JSONObject getUrl(String url) throws Exception{    try {        httpget.setURI(URI.create(url));        String response = httpclient.execute(httpget,responseHandler);        JSONObject json = JSONObject.fromObject(response);        return json;    } catch (IOException e) {        e.printStackTrace();    }    return null;}/* * 功能:发送http请求,并用jackson工具解析 * @author zhangdaquan * @date 2018/12/24 下午2:58 * @param [url] * @return com.fasterxml.jackson.databind.JsonNode * @exception */public static JsonNode getUrl2(String url){    try {        httpget.setURI(URI.create(url));        String response = httpclient.execute(httpget,responseHandler);        JsonNode node = mapper.readTree(response);        return node;    } catch (IOException e) {        e.printStackTrace();    }    return null;}/* * 功能:发送http请求,并用fastjson工具解析 * @author zhangdaquan * @date 2018/12/24 下午2:58 * @param [url] * @return com.fasterxml.jackson.databind.JsonNode * @exception */public static com.alibaba.fastjson.JSONObject getUrl3(String url){    try {        httpget.setURI(URI.create(url));        String response = httpclient.execute(httpget,responseHandler);        com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response);        return jsonObject;    } catch (IOException e) {        e.printStackTrace();    }    return null;}
--------------------- 作者:仰望星空的尘埃 来源:CSDN 原文:https://blog.csdn.net/u010285974/article/details/85696239 版权声明:本文为博主原创文章,转载请附上博文链接!

HttpClient优化的更多相关文章

  1. 高并发场景下的httpClient优化使用

    1.背景 我们有个业务,会调用其他部门提供的一个基于http的服务,日调用量在千万级别.使用了httpclient来完成业务.之前因为qps上不去,就看了一下业务代码,并做了一些优化,记录在这里. 先 ...

  2. httpclient 优化

    (1)采用单例模式(重用HttpClient实例)    对于一个通信单元甚至是整个应用程序,Apache强烈推荐只使用一个HttpClient的实例.例如: private static HttpC ...

  3. Using HttpClient properly to avoid CLOSE_WAIT TCP connections

    Apache的HttpComponent组件,用的人不在少数.但是能用好的人,却微乎其微,为什么?很简单,TCP里面的细节实现不是每个人都能捕获到的(细节是魔鬼),像并发请求控制&资源释放,N ...

  4. .NET性能优化小技巧

    .NET 性能优化小技巧 Intro 之前做了短信发送速度的提升,在大师的指导下,发送短信的速度有了极大的提升,学到了一些提升 .NET 性能的一些小技巧 HttpClient 优化 关于使用 Htt ...

  5. Java学习资源 - 其他

    http请求HttpServletRequest详解 HttpServletRequest请求转发 高并发场景下的httpClient优化使用 HttpClien高并发请求连接池 - PoolingH ...

  6. HttpClient Fluent API 高并发优化

    apache的httpcomponents-client 4.2之后提供了一套易于使用的facade API称为Fluent API,对于一般使用场景来说,使用起来非常简便,且性能也有一定保证,因为其 ...

  7. 基于httpclient的效率优化

    1.背景 我们有个业务,会调用其他部门提供的一个基于http的服务,日调用量在千万级别.使用了httpclient来完成业务.之前因为qps上不去,就看了一下业务代码,并做了一些优化,记录在这里. 先 ...

  8. HttpClient在高并发场景下的优化实战

    在项目中使用HttpClient可能是很普遍,尤其在当下微服务大火形势下,如果服务之间是http调用就少不了跟http客户端找交道.由于项目用户规模不同以及应用场景不同,很多时候可能不需要特别处理也. ...

  9. 在ASP.NET Core中用HttpClient(四)——提高性能和优化内存

    到目前为止,我们一直在使用字符串创建请求体,并读取响应的内容.但是我们可以通过使用流提高性能和优化内存.因此,在本文中,我们将学习如何在请求和响应中使用HttpClient流. 什么是流 流是以文件. ...

随机推荐

  1. SSIS 数据类型 第二篇:变量的数据类型

    变量(Variable)用于存储在Package运行时用到的值,集成服务支持两种类型的变量:用户自定义的变量和系统变量,自定义的变量由用户来定义,系统变量由集成服务来定义. 变量的用途十分广泛,用于容 ...

  2. 【JVM】垃圾回收的四大算法

    GC垃圾回收 JVM大部分时候回收的都是新生代(伊甸区+幸存0区+幸存1区).按照回收的区域可以分成两种类型:Minor GC和Full GC(MajorGC). Minor GC:只针对新生代区域的 ...

  3. Parrot os配置源更新

    每次都是忘了怎么配置,去官网查文档,这记一下 一.源文件配置注意 首先要注意Parrot官方软件库的默认更新源文件不在 /etc/apt/sources.list 而是 /etc/apt/source ...

  4. matlab单目相机标定——标定步骤以及参数含义

    参考博客园的一篇文章: https://www.cnblogs.com/flyinggod/p/8470407.html#commentform

  5. Autofac依赖注入

    简介 Autofac 是一款超赞的.NET IoC 容器 . 它管理类之间的依赖关系, 从而使 应用在规模及复杂性增长的情况下依然可以轻易地修改 .它的实现方式是将常规的.net类当做 组件 处理. ...

  6. PAT 1036 Boys vs Girls (25分) 比大小而已

    题目 This time you are asked to tell the difference between the lowest grade of all the male students ...

  7. Java实现 LeetCode 668 乘法表中第k小的数(二分)

    668. 乘法表中第k小的数 几乎每一个人都用 乘法表.但是你能在乘法表中快速找到第k小的数字吗? 给定高度m .宽度n 的一张 m * n的乘法表,以及正整数k,你需要返回表中第k 小的数字. 例 ...

  8. Java实现 蓝桥杯VIP 算法提高 十进制转八进制数

    import java.util.Scanner; public class 十进制转八进制 { public static void main(String[] args) { Scanner sc ...

  9. Java实现 洛谷 P1738 洛谷的文件夹

    题目描述 kkksc03是个非凡的空想家!在短时间内他设想了大量网页,然后总是交给可怜的lzn去实现. 洛谷的网页端,有很多文件夹,文件夹还套着文件夹. 例如:/luogu/application/c ...

  10. Java中数组二分法查找

    算法:当数组的数据量很大适宜采用该方法.采用二分法查找时,数据需是有序不重复的,如果是无序的也可通过选择排序.冒泡排序等数组排序方法进行排序之后,就可以使用二分法查找. 基本思想:假设数据是按升序排序 ...