[develop] set default connection lease request timeout and merge default config (#7906)
* set default connection lease request timeout and merge default config * remove useless code * correct comments of mergeDefaultConfig
This commit is contained in:
parent
4ceafbeb59
commit
bc40318c66
@ -18,6 +18,8 @@ package com.alibaba.nacos.common.http;
|
||||
|
||||
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
|
||||
import com.alibaba.nacos.common.http.client.request.DefaultHttpClientRequest;
|
||||
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.protocol.RequestContent;
|
||||
|
||||
@ -31,15 +33,16 @@ public abstract class AbstractApacheHttpClientFactory extends AbstractHttpClient
|
||||
@Override
|
||||
public final NacosRestTemplate createNacosRestTemplate() {
|
||||
final HttpClientConfig originalRequestConfig = buildHttpClientConfig();
|
||||
final RequestConfig defaultConfig = getRequestConfig();
|
||||
return new NacosRestTemplate(assignLogger(), new DefaultHttpClientRequest(
|
||||
HttpClients.custom()
|
||||
.addInterceptorLast(new RequestContent(true))
|
||||
.setDefaultRequestConfig(getRequestConfig())
|
||||
.setDefaultRequestConfig(defaultConfig)
|
||||
.setUserAgent(originalRequestConfig.getUserAgent())
|
||||
.setMaxConnTotal(originalRequestConfig.getMaxConnTotal())
|
||||
.setMaxConnPerRoute(originalRequestConfig.getMaxConnPerRoute())
|
||||
.setConnectionTimeToLive(originalRequestConfig.getConnTimeToLive(),
|
||||
originalRequestConfig.getConnTimeToLiveTimeUnit()).build()));
|
||||
originalRequestConfig.getConnTimeToLiveTimeUnit()).build(), defaultConfig));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -84,16 +84,17 @@ public abstract class AbstractHttpClientFactory implements HttpClientFactory {
|
||||
public NacosAsyncRestTemplate createNacosAsyncRestTemplate() {
|
||||
final HttpClientConfig originalRequestConfig = buildHttpClientConfig();
|
||||
final DefaultConnectingIOReactor ioreactor = getIoReactor();
|
||||
final RequestConfig defaultConfig = getRequestConfig();
|
||||
return new NacosAsyncRestTemplate(assignLogger(), new DefaultAsyncHttpClientRequest(
|
||||
HttpAsyncClients.custom()
|
||||
.addInterceptorLast(new RequestContent(true))
|
||||
.setDefaultIOReactorConfig(getIoReactorConfig())
|
||||
.setDefaultRequestConfig(getRequestConfig())
|
||||
.setDefaultRequestConfig(defaultConfig)
|
||||
.setMaxConnTotal(originalRequestConfig.getMaxConnTotal())
|
||||
.setMaxConnPerRoute(originalRequestConfig.getMaxConnPerRoute())
|
||||
.setUserAgent(originalRequestConfig.getUserAgent())
|
||||
.setConnectionManager(getConnectionManager(originalRequestConfig, ioreactor))
|
||||
.build(), ioreactor));
|
||||
.build(), ioreactor, defaultConfig));
|
||||
}
|
||||
|
||||
private DefaultConnectingIOReactor getIoReactor() {
|
||||
|
@ -156,7 +156,7 @@ public class HttpClientConfig {
|
||||
|
||||
private TimeUnit connTimeToLiveTimeUnit = TimeUnit.MILLISECONDS;
|
||||
|
||||
private int connectionRequestTimeout = -1;
|
||||
private int connectionRequestTimeout = 5000;
|
||||
|
||||
private int maxRedirects = 50;
|
||||
|
||||
|
@ -22,6 +22,7 @@ import com.alibaba.nacos.common.http.client.handler.ResponseHandler;
|
||||
import com.alibaba.nacos.common.http.client.response.DefaultClientHttpResponse;
|
||||
import com.alibaba.nacos.common.model.RequestHttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.client.utils.HttpClientUtils;
|
||||
import org.apache.http.concurrent.FutureCallback;
|
||||
@ -48,9 +49,12 @@ public class DefaultAsyncHttpClientRequest implements AsyncHttpClientRequest {
|
||||
|
||||
private final DefaultConnectingIOReactor ioreactor;
|
||||
|
||||
public DefaultAsyncHttpClientRequest(CloseableHttpAsyncClient asyncClient, DefaultConnectingIOReactor ioreactor) {
|
||||
private final RequestConfig defaultConfig;
|
||||
|
||||
public DefaultAsyncHttpClientRequest(CloseableHttpAsyncClient asyncClient, DefaultConnectingIOReactor ioreactor, RequestConfig defaultConfig) {
|
||||
this.asyncClient = asyncClient;
|
||||
this.ioreactor = ioreactor;
|
||||
this.defaultConfig = defaultConfig;
|
||||
if (!this.asyncClient.isRunning()) {
|
||||
this.asyncClient.start();
|
||||
}
|
||||
@ -59,7 +63,7 @@ public class DefaultAsyncHttpClientRequest implements AsyncHttpClientRequest {
|
||||
@Override
|
||||
public <T> void execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity,
|
||||
final ResponseHandler<T> responseHandler, final Callback<T> callback) throws Exception {
|
||||
HttpRequestBase httpRequestBase = DefaultHttpClientRequest.build(uri, httpMethod, requestHttpEntity);
|
||||
HttpRequestBase httpRequestBase = DefaultHttpClientRequest.build(uri, httpMethod, requestHttpEntity, defaultConfig);
|
||||
try {
|
||||
asyncClient.execute(httpRequestBase, new FutureCallback<HttpResponse>() {
|
||||
@Override
|
||||
|
@ -44,19 +44,22 @@ public class DefaultHttpClientRequest implements HttpClientRequest {
|
||||
|
||||
private final CloseableHttpClient client;
|
||||
|
||||
public DefaultHttpClientRequest(CloseableHttpClient client) {
|
||||
private final RequestConfig defaultConfig;
|
||||
|
||||
public DefaultHttpClientRequest(CloseableHttpClient client, RequestConfig defaultConfig) {
|
||||
this.client = client;
|
||||
this.defaultConfig = defaultConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity)
|
||||
throws Exception {
|
||||
HttpRequestBase request = build(uri, httpMethod, requestHttpEntity);
|
||||
HttpRequestBase request = build(uri, httpMethod, requestHttpEntity, defaultConfig);
|
||||
CloseableHttpResponse response = client.execute(request);
|
||||
return new DefaultClientHttpResponse(response);
|
||||
}
|
||||
|
||||
static HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity) throws Exception {
|
||||
static HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity, RequestConfig defaultConfig) throws Exception {
|
||||
final Header headers = requestHttpEntity.getHeaders();
|
||||
final BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method);
|
||||
final HttpRequestBase httpRequestBase = httpMethod.init(uri.toString());
|
||||
@ -67,21 +70,21 @@ public class DefaultHttpClientRequest implements HttpClientRequest {
|
||||
} else {
|
||||
HttpUtils.initRequestEntity(httpRequestBase, requestHttpEntity.getBody(), headers);
|
||||
}
|
||||
replaceDefaultConfig(httpRequestBase, requestHttpEntity.getHttpClientConfig());
|
||||
mergeDefaultConfig(httpRequestBase, requestHttpEntity.getHttpClientConfig(), defaultConfig);
|
||||
return httpRequestBase;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the HTTP config created by default with the HTTP config specified in the request.
|
||||
* Merge the HTTP config created by default with the HTTP config specified in the request.
|
||||
*
|
||||
* @param requestBase requestBase
|
||||
* @param httpClientConfig http config
|
||||
*/
|
||||
private static void replaceDefaultConfig(HttpRequestBase requestBase, HttpClientConfig httpClientConfig) {
|
||||
private static void mergeDefaultConfig(HttpRequestBase requestBase, HttpClientConfig httpClientConfig, RequestConfig defaultConfig) {
|
||||
if (httpClientConfig == null) {
|
||||
return;
|
||||
}
|
||||
requestBase.setConfig(RequestConfig.custom()
|
||||
requestBase.setConfig(RequestConfig.copy(defaultConfig)
|
||||
.setConnectTimeout(httpClientConfig.getConTimeOutMillis())
|
||||
.setSocketTimeout(httpClientConfig.getReadTimeOutMillis()).build());
|
||||
}
|
||||
|
@ -274,7 +274,7 @@ public class HttpClient {
|
||||
header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, encoding);
|
||||
AuthHeaderUtil.addIdentityToHeader(header);
|
||||
HttpClientConfig httpClientConfig = HttpClientConfig.builder().setConTimeOutMillis(5000)
|
||||
.setReadTimeOutMillis(5000).setConnectionRequestTimeout(5000).setMaxRedirects(5).build();
|
||||
.setReadTimeOutMillis(5000).build();
|
||||
return APACHE_SYNC_NACOS_REST_TEMPLATE.postForm(url, httpClientConfig, header, paramValues, String.class);
|
||||
} catch (Throwable e) {
|
||||
return RestResult.<String>builder().withCode(500).withMsg(e.toString()).build();
|
||||
|
Loading…
Reference in New Issue
Block a user