[ISSUE#3192] naming module replace http client (#3763)

* 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.
This commit is contained in:
mai.jh 2020-09-15 09:18:20 +08:00 committed by GitHub
parent dc5cba2e69
commit 5e4429f0e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 764 additions and 648 deletions

View File

@ -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()));
}
}

View File

@ -67,15 +67,20 @@ public abstract class AbstractHttpClientFactory implements HttpClientFactory {
@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();
}

View File

@ -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;
@ -103,7 +102,7 @@ public abstract class BaseHttpClient {
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;
}

View File

@ -84,6 +84,16 @@ public enum BaseHttpMethod {
}
},
/**
* delete Large request.
*/
DELETE_LARGE(HttpMethod.DELETE_LARGE) {
@Override
protected HttpRequestBase createRequest(String url) {
return new HttpDeleteWithEntity(url);
}
},
/**
* head request.
*/
@ -155,6 +165,10 @@ public enum BaseHttpMethod {
/**
* 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 {
@ -171,4 +185,26 @@ public enum BaseHttpMethod {
}
}
/**
* 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;
}
}
}

View File

@ -16,6 +16,8 @@
package com.alibaba.nacos.common.http;
import java.util.concurrent.TimeUnit;
/**
* http client config build.
*
@ -23,16 +25,62 @@ package com.alibaba.nacos.common.http;
*/
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() {
@ -43,10 +91,34 @@ public class HttpClientConfig {
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();
}
@ -57,8 +129,20 @@ public class HttpClientConfig {
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;
@ -69,13 +153,40 @@ public class HttpClientConfig {
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);
}
}
}

View File

@ -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;
@ -70,17 +72,22 @@ public final class HttpUtils {
*
* @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);
}
}

View File

@ -105,6 +105,27 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {
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.
*

View File

@ -427,6 +427,26 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
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.
*

View File

@ -65,7 +65,7 @@ public class DefaultHttpClientRequest implements HttpClientRequest {
&& 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;

View File

@ -90,7 +90,7 @@ public class JdkHttpClientRequest implements HttpClientRequest {
conn.setConnectTimeout(this.httpClientConfig.getConTimeOutMillis());
conn.setReadTimeout(this.httpClientConfig.getReadTimeOutMillis());
conn.setRequestMethod(httpMethod);
if (body != null) {
if (body != null && !"".equals(body)) {
String contentType = headers.getValue(HttpHeaderConsts.CONTENT_TYPE);
String bodyStr = JacksonUtils.toJson(body);
if (MediaType.APPLICATION_FORM_URLENCODED.equals(contentType)) {

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.common.http.param;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.utils.MapUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import java.util.ArrayList;
@ -129,8 +130,10 @@ public class Header {
* @param params parameters
*/
public void addAll(Map<String, String> params) {
for (Map.Entry<String, String> entry : params.entrySet()) {
addParam(entry.getKey(), entry.getValue());
if (MapUtils.isNotEmpty(params)) {
for (Map.Entry<String, String> entry : params.entrySet()) {
addParam(entry.getKey(), entry.getValue());
}
}
}
@ -142,9 +145,11 @@ public class Header {
* @param headers original response header
*/
public void setOriginalResponseHeader(Map<String, List<String>> headers) {
this.originalResponseHeader.putAll(headers);
for (Map.Entry<String, List<String>> entry : this.originalResponseHeader.entrySet()) {
addParam(entry.getKey(), entry.getValue().get(0));
if (MapUtils.isNotEmpty(headers)) {
this.originalResponseHeader.putAll(headers);
for (Map.Entry<String, List<String>> entry : this.originalResponseHeader.entrySet()) {
addParam(entry.getKey(), entry.getValue().get(0));
}
}
}

View File

@ -16,6 +16,8 @@
package com.alibaba.nacos.common.http.param;
import com.alibaba.nacos.common.utils.MapUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
@ -68,8 +70,10 @@ public class Query {
* @return this query
*/
public Query initParams(Map<String, String> params) {
for (Map.Entry<String, String> entry : params.entrySet()) {
addParam(entry.getKey(), entry.getValue());
if (MapUtils.isNotEmpty(params)) {
for (Map.Entry<String, String> entry : params.entrySet()) {
addParam(entry.getKey(), entry.getValue());
}
}
return this;
}

View File

@ -26,7 +26,6 @@ import java.util.Map;
* Represents an HTTP request , consisting of headers and body.
*
* @author mai.jh
* @date 2020/5/23
*/
public class RequestHttpEntity {
@ -36,7 +35,7 @@ public class RequestHttpEntity {
private final Query query;
private Object body;
private final Object body;
public RequestHttpEntity(Header header, Query query) {
this(null, header, query);

View File

@ -26,8 +26,10 @@ public class HttpMethod {
public static final String GET = "GET";
// this is only use in nacos, Custom request type, essentially a get request
/**
* this is only use in nacos, Custom request type, essentially a GET request, 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.
*/
public static final String GET_LARGE = "GET-LARGE";
public static final String HEAD = "HEAD";
@ -40,6 +42,12 @@ public class HttpMethod {
public static final String DELETE = "DELETE";
/**
* this is only use in nacos, Custom request type, essentially a DELETE request, 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.
*/
public static final String DELETE_LARGE = "DELETE_LARGE";
public static final String OPTIONS = "OPTIONS";
public static final String TRACE = "TRACE";

View File

@ -109,7 +109,7 @@ public class DistroLoadDataTask implements Runnable {
return true;
}
} catch (Exception e) {
Loggers.DISTRO.error("[DISTRO-INIT] load snapshot {} from {} failed.", resourceType, each.getAddress());
Loggers.DISTRO.error("[DISTRO-INIT] load snapshot {} from {} failed.", resourceType, each.getAddress(), e);
}
}
return false;

View File

@ -18,11 +18,13 @@ package com.alibaba.nacos.naming.consistency.persistent.raft;
import com.alibaba.nacos.common.executor.ExecutorFactory;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.http.Callback;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.consistency.DataOperation;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.core.utils.ClassUtils;
import com.alibaba.nacos.naming.NamingApp;
import com.alibaba.nacos.consistency.DataOperation;
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.KeyBuilder;
import com.alibaba.nacos.naming.consistency.RecordListener;
@ -41,8 +43,6 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.Response;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.javatuples.Pair;
@ -55,7 +55,6 @@ import javax.annotation.PostConstruct;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@ -223,25 +222,28 @@ public class RaftCore {
continue;
}
final String url = buildUrl(server, API_ON_PUB);
HttpClient.asyncHttpPostLarge(url, Arrays.asList("key=" + key), content,
new AsyncCompletionHandler<Integer>() {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
Loggers.RAFT
.warn("[RAFT] failed to publish data to peer, datumId={}, peer={}, http code={}",
datum.key, server, response.getStatusCode());
return 1;
}
latch.countDown();
return 0;
}
@Override
public STATE onContentWriteCompleted() {
return STATE.CONTINUE;
}
});
HttpClient.asyncHttpPostLarge(url, Arrays.asList("key", key), content, new Callback<String>() {
@Override
public void onReceive(RestResult<String> result) {
if (!result.ok()) {
Loggers.RAFT
.warn("[RAFT] failed to publish data to peer, datumId={}, peer={}, http code={}",
datum.key, server, result.getCode());
return;
}
latch.countDown();
}
@Override
public void onError(Throwable throwable) {
Loggers.RAFT.error("[RAFT] failed to publish data to peer", throwable);
}
@Override
public void onCancel() {
}
});
}
@ -287,21 +289,29 @@ public class RaftCore {
for (final String server : peers.allServersWithoutMySelf()) {
String url = buildUrl(server, API_ON_DEL);
HttpClient.asyncHttpDeleteLarge(url, null, json.toString(), new AsyncCompletionHandler<Integer>() {
HttpClient.asyncHttpDeleteLarge(url, null, json.toString(), new Callback<String>() {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
public void onReceive(RestResult<String> result) {
if (!result.ok()) {
Loggers.RAFT
.warn("[RAFT] failed to delete data from peer, datumId={}, peer={}, http code={}",
key, server, response.getStatusCode());
return 1;
key, server, result.getCode());
return;
}
RaftPeer local = peers.local();
local.resetLeaderDue();
return 0;
}
@Override
public void onError(Throwable throwable) {
Loggers.RAFT.error("[RAFT] failed to delete data from peer", throwable);
}
@Override
public void onCancel() {
}
});
}
@ -458,22 +468,30 @@ public class RaftCore {
for (final String server : peers.allServersWithoutMySelf()) {
final String url = buildUrl(server, API_VOTE);
try {
HttpClient.asyncHttpPost(url, null, params, new AsyncCompletionHandler<Integer>() {
HttpClient.asyncHttpPost(url, null, params, new Callback<String>() {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
Loggers.RAFT
.error("NACOS-RAFT vote failed: {}, url: {}", response.getResponseBody(), url);
return 1;
public void onReceive(RestResult<String> result) {
if (!result.ok()) {
Loggers.RAFT.error("NACOS-RAFT vote failed: {}, url: {}", result.getCode(), url);
return;
}
RaftPeer peer = JacksonUtils.toObj(response.getResponseBody(), RaftPeer.class);
RaftPeer peer = JacksonUtils.toObj(result.getData(), RaftPeer.class);
Loggers.RAFT.info("received approve from peer: {}", JacksonUtils.toJson(peer));
peers.decideLeader(peer);
return 0;
}
@Override
public void onError(Throwable throwable) {
Loggers.RAFT.error("error while sending vote to server: {}", server, throwable);
}
@Override
public void onCancel() {
}
});
} catch (Exception e) {
@ -605,28 +623,32 @@ public class RaftCore {
if (Loggers.RAFT.isDebugEnabled()) {
Loggers.RAFT.debug("send beat to server " + server);
}
HttpClient.asyncHttpPostLarge(url, null, compressedBytes, new AsyncCompletionHandler<Integer>() {
HttpClient.asyncHttpPostLarge(url, null, compressedBytes, new Callback<String>() {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
Loggers.RAFT.error("NACOS-RAFT beat failed: {}, peer: {}", response.getResponseBody(),
server);
public void onReceive(RestResult<String> result) {
if (!result.ok()) {
Loggers.RAFT.error("NACOS-RAFT beat failed: {}, peer: {}", result.getCode(), server);
MetricsMonitor.getLeaderSendBeatFailedException().increment();
return 1;
return;
}
peers.update(JacksonUtils.toObj(response.getResponseBody(), RaftPeer.class));
peers.update(JacksonUtils.toObj(result.getData(), RaftPeer.class));
if (Loggers.RAFT.isDebugEnabled()) {
Loggers.RAFT.debug("receive beat response from: {}", url);
}
return 0;
}
@Override
public void onThrowable(Throwable t) {
Loggers.RAFT.error("NACOS-RAFT error while sending heart-beat to peer: {} {}", server, t);
public void onError(Throwable throwable) {
Loggers.RAFT.error("NACOS-RAFT error while sending heart-beat to peer: {} {}", server,
throwable);
MetricsMonitor.getLeaderSendBeatFailedException().increment();
}
@Override
public void onCancel() {
}
});
} catch (Exception e) {
Loggers.RAFT.error("error while sending heart-beat to peer: {} {}", server, e);
@ -746,15 +768,15 @@ public class RaftCore {
// update datum entry
String url = buildUrl(remote.ip, API_GET) + "?keys=" + URLEncoder.encode(keys, "UTF-8");
HttpClient.asyncHttpGet(url, null, null, new AsyncCompletionHandler<Integer>() {
HttpClient.asyncHttpGet(url, null, null, new Callback<String>() {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
return 1;
public void onReceive(RestResult<String> result) {
if (!result.ok()) {
return;
}
List<JsonNode> datumList = JacksonUtils
.toObj(response.getResponseBody(), new TypeReference<List<JsonNode>>() {
.toObj(result.getData(), new TypeReference<List<JsonNode>>() {
});
for (JsonNode datumJson : datumList) {
@ -823,9 +845,24 @@ public class RaftCore {
OPERATE_LOCK.unlock();
}
}
TimeUnit.MILLISECONDS.sleep(200);
return 0;
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (InterruptedException e) {
Loggers.RAFT.error("[RAFT-BEAT] Interrupted error ", e);
}
return;
}
@Override
public void onError(Throwable throwable) {
Loggers.RAFT.error("[RAFT-BEAT] failed to sync datum from leader", throwable);
}
@Override
public void onCancel() {
}
});
batch.clear();

View File

@ -16,6 +16,8 @@
package com.alibaba.nacos.naming.consistency.persistent.raft;
import com.alibaba.nacos.common.http.Callback;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.cluster.Member;
@ -26,8 +28,6 @@ import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.naming.misc.HttpClient;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.NetUtils;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.Response;
import org.apache.commons.collections.SortedBag;
import org.apache.commons.collections.bag.TreeBag;
import org.apache.commons.lang3.StringUtils;
@ -35,7 +35,6 @@ import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.net.HttpURLConnection;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
@ -219,20 +218,28 @@ public class RaftPeerSet extends MemberChangeListener {
if (!Objects.equals(peer, candidate) && peer.state == RaftPeer.State.LEADER) {
try {
String url = RaftCore.buildUrl(peer.ip, RaftCore.API_GET_PEER);
HttpClient.asyncHttpGet(url, null, params, new AsyncCompletionHandler<Integer>() {
HttpClient.asyncHttpGet(url, null, params, new Callback<String>() {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
public void onReceive(RestResult<String> result) {
if (!result.ok()) {
Loggers.RAFT
.error("[NACOS-RAFT] get peer failed: {}, peer: {}", response.getResponseBody(),
.error("[NACOS-RAFT] get peer failed: {}, peer: {}", result.getCode(),
peer.ip);
peer.state = RaftPeer.State.FOLLOWER;
return 1;
return;
}
update(JacksonUtils.toObj(response.getResponseBody(), RaftPeer.class));
return 0;
update(JacksonUtils.toObj(result.getData(), RaftPeer.class));
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCancel() {
}
});
} catch (Exception e) {

View File

@ -16,13 +16,13 @@
package com.alibaba.nacos.naming.consistency.persistent.raft;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.naming.misc.HttpClient;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import java.net.HttpURLConnection;
import java.util.Map;
/**
@ -48,9 +48,9 @@ public class RaftProxy {
}
String url = "http://" + server + ApplicationUtils.getContextPath() + api;
HttpClient.HttpResult result = HttpClient.httpGet(url, null, params);
if (result.code != HttpURLConnection.HTTP_OK) {
throw new IllegalStateException("leader failed, caused by: " + result.content);
RestResult<String> result = HttpClient.httpGet(url, null, params);
if (!result.ok()) {
throw new IllegalStateException("leader failed, caused by: " + result.getMessage());
}
}
@ -69,7 +69,7 @@ public class RaftProxy {
server = server + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort();
}
String url = "http://" + server + ApplicationUtils.getContextPath() + api;
HttpClient.HttpResult result;
RestResult<String> result;
switch (method) {
case GET:
result = HttpClient.httpGet(url, null, params);
@ -84,8 +84,8 @@ public class RaftProxy {
throw new RuntimeException("unsupported method:" + method);
}
if (result.code != HttpURLConnection.HTTP_OK) {
throw new IllegalStateException("leader failed, caused by: " + result.content);
if (!result.ok()) {
throw new IllegalStateException("leader failed, caused by: " + result.getMessage());
}
}
@ -106,9 +106,9 @@ public class RaftProxy {
}
String url = "http://" + server + ApplicationUtils.getContextPath() + api;
HttpClient.HttpResult result = HttpClient.httpPostLarge(url, headers, content);
if (result.code != HttpURLConnection.HTTP_OK) {
throw new IllegalStateException("leader failed, caused by: " + result.content);
RestResult<String> result = HttpClient.httpPostLarge(url, headers, content);
if (!result.ok()) {
throw new IllegalStateException("leader failed, caused by: " + result.getMessage());
}
}
}

View File

@ -17,6 +17,7 @@
package com.alibaba.nacos.naming.core;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
@ -31,7 +32,6 @@ import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -97,13 +97,13 @@ public class SubscribeManager {
continue;
}
HttpClient.HttpResult result = HttpClient.httpGet(
RestResult<String> result = HttpClient.httpGet(
"http://" + server.getAddress() + ApplicationUtils.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(),
paramValues);
if (HttpURLConnection.HTTP_OK == result.code) {
Subscribers subscribers = JacksonUtils.toObj(result.content, Subscribers.class);
if (!result.ok()) {
Subscribers subscribers = JacksonUtils.toObj(result.getData(), Subscribers.class);
subscriberList.addAll(subscribers.getSubscribers());
}
}

View File

@ -16,6 +16,8 @@
package com.alibaba.nacos.naming.healthcheck;
import com.alibaba.nacos.common.http.Callback;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.naming.consistency.KeyBuilder;
@ -31,10 +33,7 @@ import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.push.PushService;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.Response;
import java.net.HttpURLConnection;
import java.util.List;
/**
@ -140,15 +139,26 @@ public class ClientBeatCheckTask implements Runnable {
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance?" + request.toUrl();
// delete instance asynchronously:
HttpClient.asyncHttpDelete(url, null, null, new AsyncCompletionHandler() {
HttpClient.asyncHttpDelete(url, null, null, new Callback<String>() {
@Override
public Object onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
public void onReceive(RestResult<String> result) {
if (!result.ok()) {
Loggers.SRV_LOG
.error("[IP-DEAD] failed to delete ip automatically, ip: {}, caused {}, resp code: {}",
instance.toJson(), response.getResponseBody(), response.getStatusCode());
instance.toJson(), result.getMessage(), result.getCode());
}
return null;
}
@Override
public void onError(Throwable throwable) {
Loggers.SRV_LOG
.error("[IP-DEAD] failed to delete ip automatically, ip: {}, error: {}", instance.toJson(),
throwable);
}
@Override
public void onCancel() {
}
});

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.naming.healthcheck;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
@ -34,7 +35,6 @@ import com.alibaba.nacos.naming.push.PushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.net.HttpURLConnection;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
@ -92,11 +92,11 @@ public class HealthCheckCommon {
JacksonUtils.toJson(list));
}
HttpClient.HttpResult httpResult = HttpClient.httpPost(
RestResult<String> httpResult = HttpClient.httpPost(
"http://" + server.getAddress() + ApplicationUtils.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + "/api/healthCheckResult", null, params);
if (httpResult.code != HttpURLConnection.HTTP_OK) {
if (!httpResult.ok()) {
Loggers.EVT_LOG.warn("[HEALTH-CHECK-SYNC] failed to send result to {}, result: {}", server,
JacksonUtils.toJson(list));
}

View File

@ -17,49 +17,24 @@
package com.alibaba.nacos.naming.misc;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.Callback;
import com.alibaba.nacos.common.http.HttpClientConfig;
import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.MediaType;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.HttpMethod;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.VersionUtils;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
/**
* Http Client.
@ -72,34 +47,11 @@ public class HttpClient {
private static final int CON_TIME_OUT_MILLIS = 5000;
private static AsyncHttpClient asyncHttpClient;
private static final NacosRestTemplate SYNC_NACOS_REST_TEMPLATE = HttpClientManager.getNacosRestTemplate();
private static CloseableHttpClient postClient;
private static final NacosRestTemplate APACHE_SYNC_NACOS_REST_TEMPLATE = HttpClientManager.getApacheRestTemplate();
static {
AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
builder.setMaximumConnectionsTotal(-1);
builder.setMaximumConnectionsPerHost(128);
builder.setAllowPoolingConnection(true);
builder.setFollowRedirects(false);
builder.setIdleConnectionTimeoutInMs(TIME_OUT_MILLIS);
builder.setConnectionTimeoutInMs(CON_TIME_OUT_MILLIS);
builder.setCompressionEnabled(true);
builder.setIOThreadMultiplier(1);
builder.setMaxRequestRetry(0);
builder.setUserAgent(UtilsAndCommons.SERVER_VERSION);
asyncHttpClient = new AsyncHttpClient(builder.build());
HttpClientBuilder builder2 = HttpClients.custom();
builder2.setUserAgent(UtilsAndCommons.SERVER_VERSION);
builder2.setConnectionTimeToLive(CON_TIME_OUT_MILLIS, TimeUnit.MILLISECONDS);
builder2.setMaxConnPerRoute(-1);
builder2.setMaxConnTotal(-1);
builder2.disableAutomaticRetries();
postClient = builder2.build();
}
private static final NacosAsyncRestTemplate ASYNC_REST_TEMPLATE = HttpClientManager.getAsyncRestTemplate();
/**
* Request http delete method.
@ -107,11 +59,11 @@ public class HttpClient {
* @param url url
* @param headers headers
* @param paramValues params
* @return {@link HttpResult} as response
* @return {@link RestResult} as response
*/
public static HttpResult httpDelete(String url, List<String> headers, Map<String, String> paramValues) {
public static RestResult<String> httpDelete(String url, List<String> headers, Map<String, String> paramValues) {
return request(url, headers, paramValues, StringUtils.EMPTY, CON_TIME_OUT_MILLIS, TIME_OUT_MILLIS, "UTF-8",
"DELETE");
HttpMethod.DELETE);
}
/**
@ -120,11 +72,11 @@ public class HttpClient {
* @param url url
* @param headers headers
* @param paramValues params
* @return {@link HttpResult} as response
* @return {@link RestResult} as response
*/
public static HttpResult httpGet(String url, List<String> headers, Map<String, String> paramValues) {
public static RestResult<String> httpGet(String url, List<String> headers, Map<String, String> paramValues) {
return request(url, headers, paramValues, StringUtils.EMPTY, CON_TIME_OUT_MILLIS, TIME_OUT_MILLIS, "UTF-8",
"GET");
HttpMethod.GET);
}
/**
@ -138,39 +90,31 @@ public class HttpClient {
* @param readTimeout timeout of request
* @param encoding charset of request
* @param method http method
* @return {@link HttpResult} as response
* @return {@link RestResult} as response
*/
public static HttpResult request(String url, List<String> headers, Map<String, String> paramValues, String body,
int connectTimeout, int readTimeout, String encoding, String method) {
HttpURLConnection conn = null;
public static RestResult<String> request(String url, List<String> headers, Map<String, String> paramValues,
String body, int connectTimeout, int readTimeout, String encoding, String method) {
Header header = Header.newInstance();
if (CollectionUtils.isNotEmpty(headers)) {
header.addAll(headers);
}
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
header.addParam(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version);
header.addParam(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION);
header.addParam(HttpHeaderConsts.REQUEST_SOURCE_HEADER, ApplicationUtils.getLocalAddress());
header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, encoding);
HttpClientConfig httpClientConfig = HttpClientConfig.builder().setConTimeOutMillis(connectTimeout)
.setReadTimeOutMillis(readTimeout).build();
Query query = Query.newInstance().initParams(paramValues);
query.addParam("encoding", "UTF-8");
query.addParam("nofix", "1");
try {
String encodedContent = encodingParams(paramValues, encoding);
url += StringUtils.isBlank(encodedContent) ? StringUtils.EMPTY : ("?" + encodedContent);
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
conn.setRequestMethod(method);
setHeaders(conn, headers, encoding);
if (StringUtils.isNotBlank(body)) {
conn.setDoOutput(true);
byte[] b = body.getBytes();
conn.setRequestProperty("Content-Length", String.valueOf(b.length));
conn.getOutputStream().write(b, 0, b.length);
conn.getOutputStream().flush();
conn.getOutputStream().close();
}
conn.connect();
return getResult(conn);
return SYNC_NACOS_REST_TEMPLATE
.exchange(url, httpClientConfig, header, query, body, method, String.class);
} catch (Exception e) {
Loggers.SRV_LOG.warn("Exception while request: {}, caused: {}", url, e);
return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap());
} finally {
IoUtils.closeQuietly(conn);
return RestResult.<String>builder().withCode(500).withMsg(e.toString()).build();
}
}
@ -180,11 +124,11 @@ public class HttpClient {
* @param url url
* @param headers headers
* @param paramValues params
* @param handler callback after request execute
* @param callback callback after request execute
*/
public static void asyncHttpGet(String url, List<String> headers, Map<String, String> paramValues,
AsyncCompletionHandler handler) throws Exception {
asyncHttpRequest(url, headers, paramValues, handler, HttpMethod.GET);
Callback<String> callback) throws Exception {
asyncHttpRequest(url, headers, paramValues, callback, HttpMethod.GET);
}
/**
@ -193,11 +137,11 @@ public class HttpClient {
* @param url url
* @param headers headers
* @param paramValues params
* @param handler callback after request execute
* @param callback callback after request execute
*/
public static void asyncHttpPost(String url, List<String> headers, Map<String, String> paramValues,
AsyncCompletionHandler handler) throws Exception {
asyncHttpRequest(url, headers, paramValues, handler, HttpMethod.POST);
Callback<String> callback) throws Exception {
asyncHttpRequest(url, headers, paramValues, callback, HttpMethod.POST);
}
/**
@ -206,11 +150,11 @@ public class HttpClient {
* @param url url
* @param headers headers
* @param paramValues params
* @param handler callback after request execute
* @param callback callback after request execute
*/
public static void asyncHttpDelete(String url, List<String> headers, Map<String, String> paramValues,
AsyncCompletionHandler handler) throws Exception {
asyncHttpRequest(url, headers, paramValues, handler, HttpMethod.DELETE);
Callback<String> callback) throws Exception {
asyncHttpRequest(url, headers, paramValues, callback, HttpMethod.DELETE);
}
/**
@ -223,124 +167,84 @@ public class HttpClient {
* @throws Exception exception when request
*/
public static void asyncHttpRequest(String url, List<String> headers, Map<String, String> paramValues,
AsyncCompletionHandler handler, String method) throws Exception {
if (!MapUtils.isEmpty(paramValues)) {
String encodedContent = encodingParams(paramValues, "UTF-8");
url += (null == encodedContent) ? "" : ("?" + encodedContent);
}
Callback<String> callback, String method) throws Exception {
AsyncHttpClient.BoundRequestBuilder builder;
Query query = Query.newInstance().initParams(paramValues);
query.addParam("encoding", "UTF-8");
query.addParam("nofix", "1");
Header header = Header.newInstance();
if (CollectionUtils.isNotEmpty(headers)) {
header.addAll(headers);
}
header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, "UTF-8");
switch (method) {
case HttpMethod.GET:
builder = asyncHttpClient.prepareGet(url);
ASYNC_REST_TEMPLATE.get(url, header, query, String.class, callback);
break;
case HttpMethod.POST:
builder = asyncHttpClient.preparePost(url);
ASYNC_REST_TEMPLATE.postForm(url, header, paramValues, String.class, callback);
break;
case HttpMethod.PUT:
builder = asyncHttpClient.preparePut(url);
ASYNC_REST_TEMPLATE.putForm(url, header, paramValues, String.class, callback);
break;
case HttpMethod.DELETE:
builder = asyncHttpClient.prepareDelete(url);
ASYNC_REST_TEMPLATE.delete(url, header, query, String.class, callback);
break;
default:
throw new RuntimeException("not supported method:" + method);
}
if (!CollectionUtils.isEmpty(headers)) {
for (String header : headers) {
builder.setHeader(header.split("=")[0], header.split("=")[1]);
}
}
builder.setHeader("Accept-Charset", "UTF-8");
if (handler != null) {
builder.execute(handler);
} else {
builder.execute();
}
}
/**
* Request http post method by async with large body.
*
* @param url url
* @param headers headers
* @param content full request content
* @param handler callback after request execute
* @param url url
* @param headers headers
* @param content full request content
* @param callback callback after request execute
*/
public static void asyncHttpPostLarge(String url, List<String> headers, String content,
AsyncCompletionHandler handler) throws Exception {
asyncHttpPostLarge(url, headers, content.getBytes(), handler);
public static void asyncHttpPostLarge(String url, List<String> headers, String content, Callback<String> callback)
throws Exception {
asyncHttpPostLarge(url, headers, content.getBytes(), callback);
}
/**
* Request http post method by async with large body.
*
* @param url url
* @param headers headers
* @param content full request content
* @param handler callback after request execute
* @param url url
* @param headers headers
* @param content full request content
* @param callback callback after request execute
*/
public static void asyncHttpPostLarge(String url, List<String> headers, byte[] content,
AsyncCompletionHandler handler) throws Exception {
AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.preparePost(url);
if (!CollectionUtils.isEmpty(headers)) {
for (String header : headers) {
builder.setHeader(header.split("=")[0], header.split("=")[1]);
}
}
builder.setBody(content);
builder.setHeader("Content-Type", "application/json; charset=UTF-8");
builder.setHeader("Accept-Charset", "UTF-8");
builder.setHeader("Accept-Encoding", "gzip");
builder.setHeader("Content-Encoding", "gzip");
if (handler != null) {
builder.execute(handler);
} else {
builder.execute();
public static void asyncHttpPostLarge(String url, List<String> headers, byte[] content, Callback<String> callback)
throws Exception {
Header header = Header.newInstance();
if (CollectionUtils.isNotEmpty(headers)) {
header.addAll(headers);
}
ASYNC_REST_TEMPLATE.post(url, header, Query.EMPTY, content, String.class, callback);
}
/**
* Request http delete method by async with large body.
*
* @param url url
* @param headers headers
* @param content full request content
* @param handler callback after request execute
* @param url url
* @param headers headers
* @param content full request content
* @param callback callback after request execute
*/
public static void asyncHttpDeleteLarge(String url, List<String> headers, String content,
AsyncCompletionHandler handler) throws Exception {
AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.prepareDelete(url);
if (!CollectionUtils.isEmpty(headers)) {
for (String header : headers) {
builder.setHeader(header.split("=")[0], header.split("=")[1]);
}
}
builder.setBody(content.getBytes());
builder.setHeader("Content-Type", "application/json; charset=UTF-8");
builder.setHeader("Accept-Charset", "UTF-8");
builder.setHeader("Accept-Encoding", "gzip");
builder.setHeader("Content-Encoding", "gzip");
if (handler != null) {
builder.execute(handler);
} else {
builder.execute();
public static void asyncHttpDeleteLarge(String url, List<String> headers, String content, Callback<String> callback)
throws Exception {
Header header = Header.newInstance();
if (CollectionUtils.isNotEmpty(headers)) {
header.addAll(headers);
}
ASYNC_REST_TEMPLATE.delete(url, header, content, String.class, callback);
}
public static HttpResult httpPost(String url, List<String> headers, Map<String, String> paramValues) {
public static RestResult<String> httpPost(String url, List<String> headers, Map<String, String> paramValues) {
return httpPost(url, headers, paramValues, "UTF-8");
}
@ -351,109 +255,40 @@ public class HttpClient {
* @param headers headers
* @param paramValues params
* @param encoding charset
* @return {@link HttpResult} as response
* @return {@link RestResult} as response
*/
public static HttpResult httpPost(String url, List<String> headers, Map<String, String> paramValues,
public static RestResult<String> httpPost(String url, List<String> headers, Map<String, String> paramValues,
String encoding) {
try {
HttpPost httpost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000)
.setConnectTimeout(5000).setSocketTimeout(5000).setRedirectsEnabled(true).setMaxRedirects(5)
.build();
httpost.setConfig(requestConfig);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : paramValues.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
Header header = Header.newInstance();
if (CollectionUtils.isNotEmpty(headers)) {
header.addAll(headers);
}
header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, encoding);
httpost.setEntity(new UrlEncodedFormEntity(nvps, encoding));
HttpResponse response = postClient.execute(httpost);
HttpEntity entity = response.getEntity();
String charset = encoding;
if (entity.getContentType() != null) {
HeaderElement[] headerElements = entity.getContentType().getElements();
if (headerElements != null && headerElements.length > 0 && headerElements[0] != null
&& headerElements[0].getParameterByName("charset") != null) {
charset = headerElements[0].getParameterByName("charset").getValue();
}
}
return new HttpResult(response.getStatusLine().getStatusCode(),
IoUtils.toString(entity.getContent(), charset), Collections.<String, String>emptyMap());
HttpClientConfig httpClientConfig = HttpClientConfig.builder().setConTimeOutMillis(5000).setReadTimeOutMillis(5000)
.setConnectionRequestTimeout(5000).setMaxRedirects(5).build();
return APACHE_SYNC_NACOS_REST_TEMPLATE.postForm(url, httpClientConfig, header, paramValues, String.class);
} catch (Throwable e) {
return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap());
return RestResult.<String>builder().withCode(500).withMsg(e.toString()).build();
}
}
/**
* Request http put method by async with large body.
*
* @param url url
* @param headers headers
* @param content full request content
* @param handler callback after request execute
* @param url url
* @param headers headers
* @param content full request content
* @param callback callback after request execute
*/
public static void asyncHttpPutLarge(String url, Map<String, String> headers, byte[] content,
AsyncCompletionHandler handler) throws Exception {
AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.preparePut(url);
if (!headers.isEmpty()) {
for (String headerKey : headers.keySet()) {
builder.setHeader(headerKey, headers.get(headerKey));
}
}
builder.setBody(content);
builder.setHeader("Content-Type", "application/json; charset=UTF-8");
builder.setHeader("Accept-Charset", "UTF-8");
builder.setHeader("Accept-Encoding", "gzip");
builder.setHeader("Content-Encoding", "gzip");
if (handler != null) {
builder.execute(handler);
} else {
builder.execute();
}
}
/**
* Request http get method by async with large body.
*
* @param url url
* @param headers headers
* @param content full request content
* @param handler callback after request execute
*/
public static void asyncHttpGetLarge(String url, Map<String, String> headers, byte[] content,
AsyncCompletionHandler handler) throws Exception {
AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.prepareGet(url);
if (!headers.isEmpty()) {
for (String headerKey : headers.keySet()) {
builder.setHeader(headerKey, headers.get(headerKey));
}
}
builder.setBody(content);
builder.setHeader("Content-Type", "application/json; charset=UTF-8");
builder.setHeader("Accept-Charset", "UTF-8");
builder.setHeader("Accept-Encoding", "gzip");
builder.setHeader("Content-Encoding", "gzip");
if (handler != null) {
builder.execute(handler);
} else {
builder.execute();
Callback<String> callback) throws Exception {
Header header = Header.newInstance();
if (MapUtils.isNotEmpty(headers)) {
header.addAll(headers);
}
ASYNC_REST_TEMPLATE.put(url, header, Query.EMPTY, content, String.class, callback);
}
/**
@ -462,29 +297,17 @@ public class HttpClient {
* @param url url
* @param headers headers
* @param content full request content
* @return {@link HttpResult} as response
* @return {@link RestResult} as response
*/
public static HttpResult httpPutLarge(String url, Map<String, String> headers, byte[] content) {
HttpClientBuilder builder = HttpClients.custom().setUserAgent(UtilsAndCommons.SERVER_VERSION)
.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS);
try (CloseableHttpClient httpClient = builder.build();) {
HttpPut httpPut = new HttpPut(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPut.setHeader(entry.getKey(), entry.getValue());
}
httpPut.setEntity(new ByteArrayEntity(content, ContentType.APPLICATION_JSON));
HttpResponse response = httpClient.execute(httpPut);
HttpEntity entity = response.getEntity();
HeaderElement[] headerElements = entity.getContentType().getElements();
String charset = headerElements[0].getParameterByName("charset").getValue();
return new HttpResult(response.getStatusLine().getStatusCode(),
IoUtils.toString(entity.getContent(), charset), Collections.<String, String>emptyMap());
public static RestResult<String> httpPutLarge(String url, Map<String, String> headers, byte[] content) {
Header header = Header.newInstance();
if (MapUtils.isNotEmpty(headers)) {
header.addAll(headers);
}
try {
return APACHE_SYNC_NACOS_REST_TEMPLATE.put(url, header, Query.EMPTY, content, String.class);
} catch (Exception e) {
return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap());
return RestResult.<String>builder().withCode(500).withMsg(e.toString()).build();
}
}
@ -494,33 +317,17 @@ public class HttpClient {
* @param url url
* @param headers headers
* @param content full request content
* @return {@link HttpResult} as response
* @return {@link RestResult} as response
*/
public static HttpResult httpGetLarge(String url, Map<String, String> headers, String content) {
HttpClientBuilder builder = HttpClients.custom();
builder.setUserAgent(UtilsAndCommons.SERVER_VERSION);
builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS);
try (CloseableHttpClient httpClient = builder.build();) {
HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity();
httpGetWithEntity.setURI(new URI(url));
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGetWithEntity.setHeader(entry.getKey(), entry.getValue());
}
httpGetWithEntity.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8")));
HttpResponse response = httpClient.execute(httpGetWithEntity);
HttpEntity entity = response.getEntity();
HeaderElement[] headerElements = entity.getContentType().getElements();
String charset = headerElements[0].getParameterByName("charset").getValue();
return new HttpResult(response.getStatusLine().getStatusCode(),
IoUtils.toString(entity.getContent(), charset), Collections.<String, String>emptyMap());
public static RestResult<String> httpGetLarge(String url, Map<String, String> headers, String content) {
Header header = Header.newInstance();
if (MapUtils.isNotEmpty(headers)) {
header.addAll(headers);
}
try {
return APACHE_SYNC_NACOS_REST_TEMPLATE.getLarge(url, header, Query.EMPTY, content, String.class);
} catch (Exception e) {
return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap());
return RestResult.<String>builder().withCode(500).withMsg(e.toString()).build();
}
}
@ -530,126 +337,20 @@ public class HttpClient {
* @param url url
* @param headers headers
* @param content full request content
* @return {@link HttpResult} as response
* @return {@link RestResult} as response
*/
public static HttpResult httpPostLarge(String url, Map<String, String> headers, String content) {
HttpClientBuilder builder = HttpClients.custom();
builder.setUserAgent(UtilsAndCommons.SERVER_VERSION);
builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS);
try (CloseableHttpClient httpClient = builder.build();) {
HttpPost httpost = new HttpPost(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpost.setHeader(entry.getKey(), entry.getValue());
}
httpost.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8")));
HttpResponse response = httpClient.execute(httpost);
HttpEntity entity = response.getEntity();
HeaderElement[] headerElements = entity.getContentType().getElements();
String charset = headerElements[0].getParameterByName("charset").getValue();
return new HttpResult(response.getStatusLine().getStatusCode(),
IoUtils.toString(entity.getContent(), charset), Collections.<String, String>emptyMap());
public static RestResult<String> httpPostLarge(String url, Map<String, String> headers, String content) {
Header header = Header.newInstance();
if (MapUtils.isNotEmpty(headers)) {
header.addAll(headers);
}
try {
return APACHE_SYNC_NACOS_REST_TEMPLATE.postJson(url, header, content, String.class);
} catch (Exception e) {
return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap());
return RestResult.<String>builder().withCode(500).withMsg(e.toString()).build();
}
}
private static HttpResult getResult(HttpURLConnection conn) throws IOException {
int respCode = conn.getResponseCode();
InputStream inputStream;
if (HttpURLConnection.HTTP_OK == respCode) {
inputStream = conn.getInputStream();
} else {
inputStream = conn.getErrorStream();
}
Map<String, String> respHeaders = new HashMap<String, String>(conn.getHeaderFields().size());
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
respHeaders.put(entry.getKey(), entry.getValue().get(0));
}
String gzipEncoding = "gzip";
if (gzipEncoding.equals(respHeaders.get(HttpHeaders.CONTENT_ENCODING))) {
inputStream = new GZIPInputStream(inputStream);
}
return new HttpResult(respCode, IoUtils.toString(inputStream, getCharset(conn)), respHeaders);
}
private static String getCharset(HttpURLConnection conn) {
String contentType = conn.getContentType();
if (StringUtils.isEmpty(contentType)) {
return "UTF-8";
}
String[] values = contentType.split(";");
if (values.length == 0) {
return "UTF-8";
}
String charset = "UTF-8";
for (String value : values) {
value = value.trim();
if (value.toLowerCase().startsWith("charset=")) {
charset = value.substring("charset=".length());
}
}
return charset;
}
private static void setHeaders(HttpURLConnection conn, List<String> headers, String encoding) {
if (null != headers) {
for (Iterator<String> iter = headers.iterator(); iter.hasNext(); ) {
conn.addRequestProperty(iter.next(), iter.next());
}
}
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding);
conn.addRequestProperty("Accept-Charset", encoding);
conn.addRequestProperty(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version);
conn.addRequestProperty(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION);
conn.addRequestProperty(HttpHeaderConsts.REQUEST_SOURCE_HEADER, ApplicationUtils.getLocalAddress());
}
/**
* Encoding parameters.
*
* @param params parameters
* @param encoding charset
* @return parameters string
* @throws UnsupportedEncodingException unsupported encodin exception
*/
public static String encodingParams(Map<String, String> params, String encoding)
throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
if (null == params || params.isEmpty()) {
return null;
}
params.put("encoding", encoding);
params.put("nofix", "1");
for (Map.Entry<String, String> entry : params.entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
continue;
}
sb.append(entry.getKey()).append("=");
sb.append(URLEncoder.encode(entry.getValue(), encoding));
sb.append("&");
}
return sb.toString();
}
/**
* Translate parameter map.
*
@ -664,33 +365,4 @@ public class HttpClient {
}
return map;
}
public static class HttpResult {
public final int code;
public final String content;
private final Map<String, String> respHeaders;
public HttpResult(int code, String content, Map<String, String> respHeaders) {
this.code = code;
this.content = content;
this.respHeaders = respHeaders;
}
public String getHeader(String name) {
return respHeaders.get(name);
}
}
public static class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "GET";
@Override
public String getMethod() {
return METHOD_NAME;
}
}
}

View File

@ -0,0 +1,149 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.naming.misc;
import com.alibaba.nacos.common.http.AbstractApacheHttpClientFactory;
import com.alibaba.nacos.common.http.AbstractHttpClientFactory;
import com.alibaba.nacos.common.http.HttpClientBeanHolder;
import com.alibaba.nacos.common.http.HttpClientConfig;
import com.alibaba.nacos.common.http.HttpClientFactory;
import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.alibaba.nacos.common.utils.ThreadUtils;
import org.slf4j.Logger;
import java.util.concurrent.TimeUnit;
import static com.alibaba.nacos.naming.misc.Loggers.SRV_LOG;
/**
* http Manager.
*
* @author mai.jh
*/
public class HttpClientManager {
private static final int TIME_OUT_MILLIS = 10000;
private static final int CON_TIME_OUT_MILLIS = 5000;
private static final HttpClientFactory SYNC_HTTP_CLIENT_FACTORY = new SyncHttpClientFactory();
private static final HttpClientFactory ASYNC_HTTP_CLIENT_FACTORY = new AsyncHttpClientFactory();
private static final HttpClientFactory APACHE_SYNC_HTTP_CLIENT_FACTORY = new ApacheSyncHttpClientFactory();
private static final NacosRestTemplate NACOS_REST_TEMPLATE;
private static final NacosRestTemplate APACHE_NACOS_REST_TEMPLATE;
private static final NacosAsyncRestTemplate NACOS_ASYNC_REST_TEMPLATE;
static {
// build nacos rest template
NACOS_REST_TEMPLATE = HttpClientBeanHolder.getNacosRestTemplate(SYNC_HTTP_CLIENT_FACTORY);
APACHE_NACOS_REST_TEMPLATE = HttpClientBeanHolder.getNacosRestTemplate(APACHE_SYNC_HTTP_CLIENT_FACTORY);
NACOS_ASYNC_REST_TEMPLATE = HttpClientBeanHolder.getNacosAsyncRestTemplate(ASYNC_HTTP_CLIENT_FACTORY);
ThreadUtils.addShutdownHook(new Runnable() {
@Override
public void run() {
shutdown();
}
});
}
public static NacosRestTemplate getNacosRestTemplate() {
return NACOS_REST_TEMPLATE;
}
/**
* Use apache http client to achieve.
* @return NacosRestTemplate
*/
public static NacosRestTemplate getApacheRestTemplate() {
return APACHE_NACOS_REST_TEMPLATE;
}
public static NacosAsyncRestTemplate getAsyncRestTemplate() {
return NACOS_ASYNC_REST_TEMPLATE;
}
private static void shutdown() {
SRV_LOG.warn("[NamingServerHttpClientManager] Start destroying HTTP-Client");
try {
HttpClientBeanHolder.shutdownNacostSyncRest(SYNC_HTTP_CLIENT_FACTORY.getClass().getName());
HttpClientBeanHolder.shutdownNacostSyncRest(APACHE_SYNC_HTTP_CLIENT_FACTORY.getClass().getName());
HttpClientBeanHolder.shutdownNacosAsyncRest(ASYNC_HTTP_CLIENT_FACTORY.getClass().getName());
} catch (Exception ex) {
SRV_LOG.error("[NamingServerHttpClientManager] An exception occurred when the HTTP client was closed : {}",
ExceptionUtil.getStackTrace(ex));
}
SRV_LOG.warn("[NamingServerHttpClientManager] Destruction of the end");
}
private static class AsyncHttpClientFactory extends AbstractHttpClientFactory {
@Override
protected HttpClientConfig buildHttpClientConfig() {
return HttpClientConfig.builder().setConTimeOutMillis(CON_TIME_OUT_MILLIS)
.setReadTimeOutMillis(TIME_OUT_MILLIS)
.setUserAgent(UtilsAndCommons.SERVER_VERSION)
.setMaxConnTotal(-1)
.setMaxConnPerRoute(128)
.setMaxRedirects(0).build();
}
@Override
protected Logger assignLogger() {
return SRV_LOG;
}
}
private static class SyncHttpClientFactory extends AbstractHttpClientFactory {
@Override
protected HttpClientConfig buildHttpClientConfig() {
return HttpClientConfig.builder().setConTimeOutMillis(CON_TIME_OUT_MILLIS)
.setReadTimeOutMillis(TIME_OUT_MILLIS)
.setMaxRedirects(0).build();
}
@Override
protected Logger assignLogger() {
return SRV_LOG;
}
}
private static class ApacheSyncHttpClientFactory extends AbstractApacheHttpClientFactory {
@Override
protected HttpClientConfig buildHttpClientConfig() {
return HttpClientConfig.builder()
.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS)
.setMaxConnTotal(Runtime.getRuntime().availableProcessors() * 2)
.setMaxConnPerRoute(Runtime.getRuntime().availableProcessors())
.setMaxRedirects(0).build();
}
@Override
protected Logger assignLogger() {
return SRV_LOG;
}
}
}

View File

@ -17,11 +17,11 @@
package com.alibaba.nacos.naming.misc;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.Callback;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.VersionUtils;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.Response;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
@ -69,28 +69,32 @@ public class NamingProxy {
headers.put(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version);
headers.put(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION);
headers.put("Connection", "Keep-Alive");
headers.put(HttpHeaderConsts.CONNECTION, "Keep-Alive");
HttpClient.asyncHttpPutLarge(
"http://" + server + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT
+ TIMESTAMP_SYNC_URL + "?source=" + NetUtils.localServer(), headers, checksums,
new AsyncCompletionHandler() {
new Callback<String>() {
@Override
public Object onCompleted(Response response) throws Exception {
if (HttpURLConnection.HTTP_OK != response.getStatusCode()) {
public void onReceive(RestResult<String> result) {
if (!result.ok()) {
Loggers.DISTRO.error("failed to req API: {}, code: {}, msg: {}",
"http://" + server + ApplicationUtils.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + TIMESTAMP_SYNC_URL,
response.getStatusCode(), response.getResponseBody());
result.getCode(), result.getMessage());
}
return null;
}
@Override
public void onThrowable(Throwable t) {
public void onError(Throwable throwable) {
Loggers.DISTRO
.error("failed to req API:" + "http://" + server + ApplicationUtils.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + TIMESTAMP_SYNC_URL, t);
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + TIMESTAMP_SYNC_URL, throwable);
}
@Override
public void onCancel() {
}
});
} catch (Exception e) {
@ -110,17 +114,17 @@ public class NamingProxy {
Map<String, String> params = new HashMap<>(8);
params.put("keys", StringUtils.join(keys, ","));
HttpClient.HttpResult result = HttpClient.httpGetLarge(
RestResult<String> result = HttpClient.httpGetLarge(
"http://" + server + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT
+ DATA_GET_URL, new HashMap<>(8), JacksonUtils.toJson(params));
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content.getBytes();
if (result.ok()) {
return result.getData().getBytes();
}
throw new IOException("failed to req API: " + "http://" + server + ApplicationUtils.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL + ". code: " + result.code + " msg: "
+ result.content);
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL + ". code: " + result.getCode() + " msg: "
+ result.getMessage());
}
/**
@ -133,17 +137,17 @@ public class NamingProxy {
public static byte[] getAllData(String server) throws Exception {
Map<String, String> params = new HashMap<>(8);
HttpClient.HttpResult result = HttpClient.httpGet(
RestResult<String> result = HttpClient.httpGet(
"http://" + server + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT
+ ALL_DATA_GET_URL, new ArrayList<>(), params);
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content.getBytes();
if (result.ok()) {
return result.getData().getBytes();
}
throw new IOException("failed to req API: " + "http://" + server + ApplicationUtils.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL + ". code: " + result.code + " msg: "
+ result.content);
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL + ". code: " + result.getCode() + " msg: "
+ result.getMessage());
}
/**
@ -158,23 +162,23 @@ public class NamingProxy {
headers.put(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version);
headers.put(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION);
headers.put("Accept-Encoding", "gzip,deflate,sdch");
headers.put("Connection", "Keep-Alive");
headers.put("Content-Encoding", "gzip");
headers.put(HttpHeaderConsts.ACCEPT_ENCODING, "gzip,deflate,sdch");
headers.put(HttpHeaderConsts.CONNECTION, "Keep-Alive");
headers.put(HttpHeaderConsts.CONTENT_ENCODING, "gzip");
try {
HttpClient.HttpResult result = HttpClient.httpPutLarge(
RestResult<String> result = HttpClient.httpPutLarge(
"http://" + curServer + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT
+ DATA_ON_SYNC_URL, headers, data);
if (HttpURLConnection.HTTP_OK == result.code) {
if (result.ok()) {
return true;
}
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) {
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.getCode()) {
return true;
}
throw new IOException("failed to req API:" + "http://" + curServer + ApplicationUtils.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL + ". code:" + result.code + " msg: "
+ result.content);
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL + ". code:" + result.getCode() + " msg: "
+ result.getData());
} catch (Exception e) {
Loggers.SRV_LOG.warn("NamingProxy", e);
}
@ -196,7 +200,7 @@ public class NamingProxy {
HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION, "Accept-Encoding",
"gzip,deflate,sdch", "Connection", "Keep-Alive", "Content-Encoding", "gzip");
HttpClient.HttpResult result;
RestResult<String> result;
if (!curServer.contains(UtilsAndCommons.IP_PORT_SPLITER)) {
curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort();
@ -204,17 +208,17 @@ public class NamingProxy {
result = HttpClient.httpGet("http://" + curServer + api, headers, params);
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content;
if (result.ok()) {
return result.getData();
}
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) {
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.getCode()) {
return StringUtils.EMPTY;
}
throw new IOException(
"failed to req API:" + "http://" + curServer + api + ". code:" + result.code + " msg: "
+ result.content);
"failed to req API:" + "http://" + curServer + api + ". code:" + result.getCode() + " msg: "
+ result.getMessage());
} catch (Exception e) {
Loggers.SRV_LOG.warn("NamingProxy", e);
}
@ -238,7 +242,7 @@ public class NamingProxy {
HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION, "Accept-Encoding",
"gzip,deflate,sdch", "Connection", "Keep-Alive", "Content-Encoding", "gzip");
HttpClient.HttpResult result;
RestResult<String> result;
if (!curServer.contains(UtilsAndCommons.IP_PORT_SPLITER)) {
curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort();
@ -254,17 +258,17 @@ public class NamingProxy {
+ "/api/" + api, headers, params);
}
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content;
if (result.ok()) {
return result.getData();
}
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) {
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.getCode()) {
return StringUtils.EMPTY;
}
throw new IOException("failed to req API:" + "http://" + curServer + ApplicationUtils.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + "/api/" + api + ". code:" + result.code + " msg: "
+ result.content);
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + "/api/" + api + ". code:" + result.getCode() + " msg: "
+ result.getMessage());
} catch (Exception e) {
Loggers.SRV_LOG.warn("NamingProxy", e);
}
@ -288,7 +292,7 @@ public class NamingProxy {
UtilsAndCommons.SERVER_VERSION, "Accept-Encoding", "gzip,deflate,sdch", "Connection", "Keep-Alive",
"Content-Encoding", "gzip");
HttpClient.HttpResult result;
RestResult<String> result;
if (!curServer.contains(UtilsAndCommons.IP_PORT_SPLITER)) {
curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort();
@ -304,17 +308,17 @@ public class NamingProxy {
+ path, headers, params);
}
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content;
if (result.ok()) {
return result.getData();
}
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) {
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.getCode()) {
return StringUtils.EMPTY;
}
throw new IOException("failed to req API:" + "http://" + curServer + ApplicationUtils.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + path + ". code:" + result.code + " msg: "
+ result.content);
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + path + ". code:" + result.getCode() + " msg: "
+ result.getMessage());
} catch (Exception e) {
Loggers.SRV_LOG.warn("NamingProxy", e);
}

View File

@ -16,12 +16,11 @@
package com.alibaba.nacos.naming.misc;
import com.alibaba.nacos.common.http.Callback;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.Response;
import org.springframework.util.StringUtils;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
@ -52,15 +51,23 @@ public class ServerStatusSynchronizer implements Synchronizer {
}
try {
HttpClient.asyncHttpGet(url, null, params, new AsyncCompletionHandler() {
HttpClient.asyncHttpGet(url, null, params, new Callback<String>() {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
public void onReceive(RestResult<String> result) {
if (!result.ok()) {
Loggers.SRV_LOG.warn("[STATUS-SYNCHRONIZE] failed to request serverStatus, remote server: {}",
serverIP);
return 1;
}
return 0;
}
@Override
public void onError(Throwable throwable) {
Loggers.SRV_LOG.warn("[STATUS-SYNCHRONIZE] failed to request serverStatus, remote server: {}", serverIP, throwable);
}
@Override
public void onCancel() {
}
});
} catch (Exception e) {

View File

@ -16,13 +16,12 @@
package com.alibaba.nacos.naming.misc;
import com.alibaba.nacos.common.http.Callback;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.Response;
import org.apache.commons.lang3.StringUtils;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
@ -53,16 +52,24 @@ public class ServiceStatusSynchronizer implements Synchronizer {
}
try {
HttpClient.asyncHttpPostLarge(url, null, JacksonUtils.toJson(params), new AsyncCompletionHandler() {
HttpClient.asyncHttpPostLarge(url, null, JacksonUtils.toJson(params), new Callback<String>() {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
public void onReceive(RestResult<String> result) {
if (!result.ok()) {
Loggers.SRV_LOG.warn("[STATUS-SYNCHRONIZE] failed to request serviceStatus, remote server: {}",
serverIP);
return 1;
}
return 0;
}
@Override
public void onError(Throwable throwable) {
Loggers.SRV_LOG.warn("[STATUS-SYNCHRONIZE] failed to request serviceStatus, remote server: " + serverIP, throwable);
}
@Override
public void onCancel() {
}
});
} catch (Exception e) {

View File

@ -19,6 +19,7 @@ package com.alibaba.nacos.naming.web;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.core.code.ControllerMethodsCache;
@ -139,11 +140,12 @@ public class DistroFilter implements Filter {
final String body = IoUtils.toString(req.getInputStream(), Charsets.UTF_8.name());
final Map<String, String> paramsValue = HttpClient.translateParameterMap(req.getParameterMap());
HttpClient.HttpResult result = HttpClient
RestResult<String> result = HttpClient
.request("http://" + targetServer + req.getRequestURI(), headerList, paramsValue, body,
PROXY_CONNECT_TIMEOUT, PROXY_READ_TIMEOUT, Charsets.UTF_8.name(), req.getMethod());
String data = result.ok() ? result.getData() : result.getMessage();
try {
WebUtils.response(resp, result.content, result.code);
WebUtils.response(resp, data, result.getCode());
} catch (Exception ignore) {
Loggers.SRV_LOG.warn("[DISTRO-FILTER] request failed: " + distroMapper.mapSrv(groupedServiceName)
+ urlString);