Skip to content

Commit

Permalink
[ISSUE#3192] naming module replace http client (alibaba#3763)
Browse files Browse the repository at this point in the history
* naming module replace http client

* refactor: naming module replace http client.

* refactor: naming module replace http client.

* refactor: Add apache http client Factory.

* refactor: naming module replace http client.

* fix code style

* refactor: Add http client config

* refactor: naming module HttpClientManager change

* refactor: naming module HttpClientManager change

* refactor: naming module replace http client.

* fix code style

* refactor: fix JDK http client Use error problem.

* refactor: Query And Header entity init Add non-empty judgment

* Enhance the asynchronous http delete request method to support body passing parameters.

* refactor: apache http client set MaxConnTotal and maxConnPerRoute.
  • Loading branch information
Maijh97 authored Sep 15, 2020
1 parent dc5cba2 commit 5e4429f
Show file tree
Hide file tree
Showing 27 changed files with 764 additions and 648 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ public abstract class AbstractApacheHttpClientFactory extends AbstractHttpClient

@Override
public final NacosRestTemplate createNacosRestTemplate() {
final HttpClientConfig originalRequestConfig = buildHttpClientConfig();
final RequestConfig requestConfig = getRequestConfig();
return new NacosRestTemplate(assignLogger(),
new DefaultHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(requestConfig).build()));
return new NacosRestTemplate(assignLogger(), new DefaultHttpClientRequest(
HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setUserAgent(originalRequestConfig.getUserAgent())
.setMaxConnTotal(originalRequestConfig.getMaxConnTotal())
.setMaxConnPerRoute(originalRequestConfig.getMaxConnPerRoute())
.setConnectionTimeToLive(originalRequestConfig.getConnTimeToLive(),
originalRequestConfig.getConnTimeToLiveTimeUnit()).build()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@ public void onChanged(String filePath) {

@Override
public NacosAsyncRestTemplate createNacosAsyncRestTemplate() {
RequestConfig requestConfig = getRequestConfig();
final HttpClientConfig originalRequestConfig = buildHttpClientConfig();
final RequestConfig requestConfig = getRequestConfig();
return new NacosAsyncRestTemplate(assignLogger(), new DefaultAsyncHttpClientRequest(
HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build()));
HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig)
.setMaxConnTotal(originalRequestConfig.getMaxConnTotal())
.setMaxConnPerRoute(originalRequestConfig.getMaxConnPerRoute())
.setUserAgent(originalRequestConfig.getUserAgent()).build()));
}

protected RequestConfig getRequestConfig() {
HttpClientConfig httpClientConfig = buildHttpClientConfig();
return RequestConfig.custom().setConnectTimeout(httpClientConfig.getConTimeOutMillis())
.setSocketTimeout(httpClientConfig.getReadTimeOutMillis())
.setConnectionRequestTimeout(httpClientConfig.getConnectionRequestTimeout())
.setMaxRedirects(httpClientConfig.getMaxRedirects()).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.alibaba.nacos.common.http;

import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.handler.ResponseHandler;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
Expand Down Expand Up @@ -103,7 +102,7 @@ protected HttpRequestBase build(String url, Header header, Object body, String m
final BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method);
final HttpRequestBase httpRequestBase = httpMethod.init(url);
HttpUtils.initRequestHeader(httpRequestBase, header);
HttpUtils.initRequestEntity(httpRequestBase, body, header.getValue(HttpHeaderConsts.CONTENT_TYPE));
HttpUtils.initRequestEntity(httpRequestBase, body, header);
return httpRequestBase;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ protected HttpRequestBase createRequest(String url) {
}
},

/**
* delete Large request.
*/
DELETE_LARGE(HttpMethod.DELETE_LARGE) {
@Override
protected HttpRequestBase createRequest(String url) {
return new HttpDeleteWithEntity(url);
}
},

/**
* head request.
*/
Expand Down Expand Up @@ -155,6 +165,10 @@ public static BaseHttpMethod sourceOf(String name) {

/**
* get Large implemented.
* <p>
* Mainly used for GET request parameters are relatively large, can not be placed on the URL, so it needs to be
* placed in the body.
* </p>
*/
public static class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {

Expand All @@ -171,4 +185,26 @@ public String getMethod() {
}
}

/**
* delete Large implemented.
* <p>
* Mainly used for DELETE request parameters are relatively large, can not be placed on the URL, so it needs to be
* placed in the body.
* </p>
*/
public static class HttpDeleteWithEntity extends HttpEntityEnclosingRequestBase {

public static final String METHOD_NAME = "DELETE";

public HttpDeleteWithEntity(String url) {
super();
setURI(URI.create(url));
}

@Override
public String getMethod() {
return METHOD_NAME;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,71 @@

package com.alibaba.nacos.common.http;

import java.util.concurrent.TimeUnit;

/**
* http client config build.
*
* @author mai.jh
*/
public class HttpClientConfig {

/**
* connect time out.
*/
private final int conTimeOutMillis;

/**
* read time out.
*/
private final int readTimeOutMillis;

/**
* connTimeToLive.
*/
private final long connTimeToLive;

/**
* connTimeToLiveTimeUnit.
*/
private final TimeUnit connTimeToLiveTimeUnit;

/**
* connectionRequestTimeout.
*/
private final int connectionRequestTimeout;

/**
* max redirect.
*/
private final int maxRedirects;

public HttpClientConfig(int conTimeOutMillis, int readTimeOutMillis, int maxRedirects) {
/**
* max connect total.
*/
private final int maxConnTotal;

/**
* Assigns maximum connection per route value.
*/
private final int maxConnPerRoute;

/**
* user agent.
*/
private final String userAgent;

public HttpClientConfig(int conTimeOutMillis, int readTimeOutMillis, long connTimeToLive, TimeUnit timeUnit,
int connectionRequestTimeout, int maxRedirects, int maxConnTotal, int maxConnPerRoute, String userAgent) {
this.conTimeOutMillis = conTimeOutMillis;
this.readTimeOutMillis = readTimeOutMillis;
this.connTimeToLive = connTimeToLive;
this.connTimeToLiveTimeUnit = timeUnit;
this.connectionRequestTimeout = connectionRequestTimeout;
this.maxRedirects = maxRedirects;
this.maxConnTotal = maxConnTotal;
this.maxConnPerRoute = maxConnPerRoute;
this.userAgent = userAgent;
}

public int getConTimeOutMillis() {
Expand All @@ -43,10 +91,34 @@ public int getReadTimeOutMillis() {
return readTimeOutMillis;
}

public long getConnTimeToLive() {
return connTimeToLive;
}

public TimeUnit getConnTimeToLiveTimeUnit() {
return connTimeToLiveTimeUnit;
}

public int getConnectionRequestTimeout() {
return connectionRequestTimeout;
}

public int getMaxRedirects() {
return maxRedirects;
}

public int getMaxConnTotal() {
return maxConnTotal;
}

public int getMaxConnPerRoute() {
return maxConnPerRoute;
}

public String getUserAgent() {
return userAgent;
}

public static HttpClientConfigBuilder builder() {
return new HttpClientConfigBuilder();
}
Expand All @@ -57,8 +129,20 @@ public static final class HttpClientConfigBuilder {

private int readTimeOutMillis = -1;

private long connTimeToLive = -1;

private TimeUnit connTimeToLiveTimeUnit = TimeUnit.MILLISECONDS;

private int connectionRequestTimeout = -1;

private int maxRedirects = 50;

private int maxConnTotal = 0;

private int maxConnPerRoute = 0;

private String userAgent;

public HttpClientConfigBuilder setConTimeOutMillis(int conTimeOutMillis) {
this.conTimeOutMillis = conTimeOutMillis;
return this;
Expand All @@ -69,13 +153,40 @@ public HttpClientConfigBuilder setReadTimeOutMillis(int readTimeOutMillis) {
return this;
}

public HttpClientConfigBuilder setConnectionTimeToLive(long connTimeToLive, TimeUnit connTimeToLiveTimeUnit) {
this.connTimeToLive = connTimeToLive;
this.connTimeToLiveTimeUnit = connTimeToLiveTimeUnit;
return this;
}

public HttpClientConfigBuilder setConnectionRequestTimeout(int connectionRequestTimeout) {
this.connectionRequestTimeout = connectionRequestTimeout;
return this;
}

public HttpClientConfigBuilder setMaxRedirects(int maxRedirects) {
this.maxRedirects = maxRedirects;
return this;
}

public HttpClientConfigBuilder setMaxConnTotal(int maxConnTotal) {
this.maxConnTotal = maxConnTotal;
return this;
}

public HttpClientConfigBuilder setMaxConnPerRoute(int maxConnPerRoute) {
this.maxConnPerRoute = maxConnPerRoute;
return this;
}

public HttpClientConfigBuilder setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}

public HttpClientConfig build() {
return new HttpClientConfig(conTimeOutMillis, readTimeOutMillis, maxRedirects);
return new HttpClientConfig(conTimeOutMillis, readTimeOutMillis, connTimeToLive, connTimeToLiveTimeUnit,
connectionRequestTimeout, maxRedirects, maxConnTotal, maxConnPerRoute, userAgent);
}
}
}
17 changes: 12 additions & 5 deletions common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@

package com.alibaba.nacos.common.http;

import com.alibaba.nacos.common.http.handler.RequestHandler;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
Expand Down Expand Up @@ -70,17 +72,22 @@ public static void initRequestHeader(HttpRequestBase requestBase, Header header)
*
* @param requestBase requestBase {@link HttpRequestBase}
* @param body body
* @param mediaType mediaType {@link ContentType}
* @param header request header
* @throws Exception exception
*/
public static void initRequestEntity(HttpRequestBase requestBase, Object body, String mediaType) throws Exception {
public static void initRequestEntity(HttpRequestBase requestBase, Object body, Header header) throws Exception {
if (body == null) {
return;
}
if (requestBase instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase;
ContentType contentType = ContentType.create(mediaType);
StringEntity entity = new StringEntity(RequestHandler.parse(body), contentType);
ContentType contentType = ContentType.create(header.getValue(HttpHeaderConsts.CONTENT_TYPE), header.getCharset());
HttpEntity entity;
if (body instanceof byte[]) {
entity = new ByteArrayEntity((byte[]) body, contentType);
} else {
entity = new StringEntity(body instanceof String ? (String) body : JacksonUtils.toJson(body), contentType);
}
request.setEntity(entity);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ public <T> void delete(String url, Header header, Query query, Type responseType
execute(url, HttpMethod.DELETE, new RequestHttpEntity(header, query), responseType, callback);
}

/**
* async http delete large request, when the parameter exceeds the URL limit, you can use this method to put the
* parameter into the body pass.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type
*
* <p>{@code callback} Result callback execution,
* if you need response headers, you can convert the received RestResult to HttpRestResult.
*
* @param url url
* @param header http header param
* @param body body
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void delete(String url, Header header, String body, Type responseType, Callback<T> callback) {
execute(url, HttpMethod.DELETE_LARGE,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON), Query.EMPTY, body),
responseType, callback);
}

/**
* async http put Create a new resource by PUTting the given body to http request.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,26 @@ public <T> HttpRestResult<T> exchangeForm(String url, Header header, Query query
return execute(url, httpMethod, requestHttpEntity, responseType);
}

/**
* Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns
* the response as {@link HttpRestResult}.
*
* @param url url
* @param config HttpClientConfig
* @param header http header param
* @param query http query param
* @param body http body param
* @param httpMethod http method
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> exchange(String url, HttpClientConfig config, Header header, Query query,
Object body, String httpMethod, Type responseType) throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(config, header, query, body);
return execute(url, httpMethod, requestHttpEntity, responseType);
}

/**
* Set the request interceptors that this accessor should use.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHt
&& requestHttpEntity.getBody() instanceof Map) {
HttpUtils.initRequestFromEntity(httpRequestBase, (Map<String, String>) requestHttpEntity.getBody(), headers.getCharset());
} else {
HttpUtils.initRequestEntity(httpRequestBase, requestHttpEntity.getBody(), headers.getValue(HttpHeaderConsts.CONTENT_TYPE));
HttpUtils.initRequestEntity(httpRequestBase, requestHttpEntity.getBody(), headers);
}
replaceDefaultConfig(httpRequestBase, requestHttpEntity.getHttpClientConfig());
return httpRequestBase;
Expand Down
Loading

0 comments on commit 5e4429f

Please sign in to comment.