Skip to content

Commit

Permalink
[ISSUE alibaba#4225] Replace http client in HttpHealthCheckProcessor (a…
Browse files Browse the repository at this point in the history
…libaba#4244)

* for #42255, Replace http client in HttpHealthCheckProcessor.

* Restore the old version number.

* Removes temporary attributes from a method.

* fix Code style.

* Use the EnvUtil.getProperty() method to get the HTTP parameter, leaving it customizable.

* Fix error symbol.

* Remove excess package import.

* Rollback the code about the custom HTTP parameter.

* fix Code style.
  • Loading branch information
Maijh97 authored Nov 19, 2020
1 parent f00423f commit 6f4dbf0
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

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;

Expand All @@ -32,11 +31,10 @@ 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()
.addInterceptorLast(new RequestContent(true))
.setDefaultRequestConfig(requestConfig)
.setDefaultRequestConfig(getRequestConfig())
.setUserAgent(originalRequestConfig.getUserAgent())
.setMaxConnTotal(originalRequestConfig.getMaxConnTotal())
.setMaxConnPerRoute(originalRequestConfig.getMaxConnPerRoute())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.protocol.RequestContent;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.slf4j.Logger;

import javax.net.ssl.HostnameVerifier;
Expand Down Expand Up @@ -69,21 +70,27 @@ public void onChanged(String filePath) {
@Override
public NacosAsyncRestTemplate createNacosAsyncRestTemplate() {
final HttpClientConfig originalRequestConfig = buildHttpClientConfig();
final RequestConfig requestConfig = getRequestConfig();
return new NacosAsyncRestTemplate(assignLogger(), new DefaultAsyncHttpClientRequest(
HttpAsyncClients.custom()
.addInterceptorLast(new RequestContent(true))
.setDefaultRequestConfig(requestConfig)
.setDefaultIOReactorConfig(getIoReactorConfig())
.setDefaultRequestConfig(getRequestConfig())
.setMaxConnTotal(originalRequestConfig.getMaxConnTotal())
.setMaxConnPerRoute(originalRequestConfig.getMaxConnPerRoute())
.setUserAgent(originalRequestConfig.getUserAgent()).build()));
}

protected IOReactorConfig getIoReactorConfig() {
HttpClientConfig httpClientConfig = buildHttpClientConfig();
return IOReactorConfig.custom().setIoThreadCount(httpClientConfig.getIoThreadCount()).build();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,24 @@ public class HttpClientConfig {
*/
private final int maxConnPerRoute;

/**
* is HTTP compression enabled.
*/
private final boolean contentCompressionEnabled;

/**
* io thread count.
*/
private final int ioThreadCount;

/**
* 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) {
int connectionRequestTimeout, int maxRedirects, int maxConnTotal, int maxConnPerRoute,
boolean contentCompressionEnabled, int ioThreadCount, String userAgent) {
this.conTimeOutMillis = conTimeOutMillis;
this.readTimeOutMillis = readTimeOutMillis;
this.connTimeToLive = connTimeToLive;
Expand All @@ -80,6 +91,8 @@ public HttpClientConfig(int conTimeOutMillis, int readTimeOutMillis, long connTi
this.maxRedirects = maxRedirects;
this.maxConnTotal = maxConnTotal;
this.maxConnPerRoute = maxConnPerRoute;
this.contentCompressionEnabled = contentCompressionEnabled;
this.ioThreadCount = ioThreadCount;
this.userAgent = userAgent;
}

Expand Down Expand Up @@ -115,6 +128,14 @@ public int getMaxConnPerRoute() {
return maxConnPerRoute;
}

public boolean getContentCompressionEnabled() {
return contentCompressionEnabled;
}

public int getIoThreadCount() {
return ioThreadCount;
}

public String getUserAgent() {
return userAgent;
}
Expand All @@ -141,6 +162,10 @@ public static final class HttpClientConfigBuilder {

private int maxConnPerRoute = 0;

private boolean contentCompressionEnabled = true;

private int ioThreadCount = Runtime.getRuntime().availableProcessors();

private String userAgent;

public HttpClientConfigBuilder setConTimeOutMillis(int conTimeOutMillis) {
Expand All @@ -158,7 +183,7 @@ public HttpClientConfigBuilder setConnectionTimeToLive(long connTimeToLive, Time
this.connTimeToLiveTimeUnit = connTimeToLiveTimeUnit;
return this;
}

public HttpClientConfigBuilder setConnectionRequestTimeout(int connectionRequestTimeout) {
this.connectionRequestTimeout = connectionRequestTimeout;
return this;
Expand All @@ -179,14 +204,30 @@ public HttpClientConfigBuilder setMaxConnPerRoute(int maxConnPerRoute) {
return this;
}

public HttpClientConfigBuilder setContentCompressionEnabled(boolean contentCompressionEnabled) {
this.contentCompressionEnabled = contentCompressionEnabled;
return this;
}

public HttpClientConfigBuilder setIoThreadCount(int ioThreadCount) {
this.ioThreadCount = ioThreadCount;
return this;
}

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


/**
* build http client config.
*
* @return HttpClientConfig
*/
public HttpClientConfig build() {
return new HttpClientConfig(conTimeOutMillis, readTimeOutMillis, connTimeToLive, connTimeToLiveTimeUnit,
connectionRequestTimeout, maxRedirects, maxConnTotal, maxConnPerRoute, userAgent);
connectionRequestTimeout, maxRedirects, maxConnTotal, maxConnPerRoute, contentCompressionEnabled,
ioThreadCount, userAgent);
}
}
}
31 changes: 24 additions & 7 deletions common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
Expand All @@ -42,6 +44,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -58,7 +61,7 @@ public final class HttpUtils {
* Init http header.
*
* @param requestBase requestBase {@link HttpRequestBase}
* @param header header
* @param header header
*/
public static void initRequestHeader(HttpRequestBase requestBase, Header header) {
Iterator<Map.Entry<String, String>> iterator = header.iterator();
Expand All @@ -72,8 +75,8 @@ public static void initRequestHeader(HttpRequestBase requestBase, Header header)
* Init http entity.
*
* @param requestBase requestBase {@link HttpRequestBase}
* @param body body
* @param header request header
* @param body body
* @param header request header
* @throws Exception exception
*/
public static void initRequestEntity(HttpRequestBase requestBase, Object body, Header header) throws Exception {
Expand All @@ -88,7 +91,8 @@ public static void initRequestEntity(HttpRequestBase requestBase, Object body, H
if (body instanceof byte[]) {
entity = new ByteArrayEntity((byte[]) body, contentType);
} else {
entity = new StringEntity(body instanceof String ? (String) body : JacksonUtils.toJson(body), contentType);
entity = new StringEntity(body instanceof String ? (String) body : JacksonUtils.toJson(body),
contentType);
}
request.setEntity(entity);
}
Expand All @@ -98,11 +102,12 @@ public static void initRequestEntity(HttpRequestBase requestBase, Object body, H
* Init request from entity map.
*
* @param requestBase requestBase {@link HttpRequestBase}
* @param body body map
* @param charset charset of entity
* @param body body map
* @param charset charset of entity
* @throws Exception exception
*/
public static void initRequestFromEntity(HttpRequestBase requestBase, Map<String, String> body, String charset) throws Exception {
public static void initRequestFromEntity(HttpRequestBase requestBase, Map<String, String> body, String charset)
throws Exception {
if (body == null || body.isEmpty()) {
return;
}
Expand Down Expand Up @@ -244,6 +249,17 @@ public static URI buildUri(String url, Query query) throws URISyntaxException {
return new URI(url);
}

/**
* HTTP request exception is a timeout exception.
*
* @param throwable http request throwable
* @return boolean
*/
public static boolean isTimeoutException(Throwable throwable) {
return throwable instanceof SocketTimeoutException || throwable instanceof ConnectTimeoutException
|| throwable instanceof TimeoutException || throwable.getCause() instanceof TimeoutException;
}

private static String innerDecode(String pre, String now, String encode) throws UnsupportedEncodingException {
// Because the data may be encoded by the URL more than once,
// it needs to be decoded recursively until it is fully successful
Expand All @@ -254,4 +270,5 @@ private static String innerDecode(String pre, String now, String encode) throws
now = URLDecoder.decode(now, encode);
return innerDecode(pre, now, encode);
}

}
5 changes: 0 additions & 5 deletions naming/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,11 @@
<artifactId>nacos-api</artifactId>
</dependency>


<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>

<dependency>
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import com.alibaba.nacos.naming.consistency.ValueChangeEvent;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.pojo.Record;
import org.jboss.netty.util.internal.ConcurrentHashMap;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
Expand Down
Loading

0 comments on commit 6f4dbf0

Please sign in to comment.