Merge branch 'develop' of https://github.com/alibaba/nacos into jraft_naming

This commit is contained in:
chuntaojun 2020-08-22 00:00:44 +08:00
commit 6ddab7523e
14 changed files with 222 additions and 168 deletions

View File

@ -49,14 +49,13 @@ import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.Callable;
/**
* Server Agent.
@ -94,9 +93,9 @@ public class ServerHttpAgent implements HttpAgent {
if (headers != null) {
newHeaders.addAll(headers);
}
Query query = Query.newInstance().initParams(paramValues);
HttpRestResult<String> result = NACOS_RESTTEMPLATE
.get(getUrl(currentServerAddr, path), httpConfig, newHeaders, paramValues, String.class);
.get(getUrl(currentServerAddr, path), httpConfig, newHeaders, query, String.class);
if (isFail(result)) {
LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}",
serverListMgr.getCurrentServerAddr(), result.getCode());
@ -152,8 +151,7 @@ public class ServerHttpAgent implements HttpAgent {
newHeaders.addAll(headers);
}
HttpRestResult<String> result = NACOS_RESTTEMPLATE
.postForm(getUrl(currentServerAddr, path), httpConfig, newHeaders,
new HashMap<String, String>(0), paramValues, String.class);
.postForm(getUrl(currentServerAddr, path), httpConfig, newHeaders, paramValues, String.class);
if (isFail(result)) {
LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", currentServerAddr,
@ -207,8 +205,9 @@ public class ServerHttpAgent implements HttpAgent {
if (headers != null) {
newHeaders.addAll(headers);
}
Query query = Query.newInstance().initParams(paramValues);
HttpRestResult<String> result = NACOS_RESTTEMPLATE
.delete(getUrl(currentServerAddr, path), httpConfig, newHeaders, paramValues, String.class);
.delete(getUrl(currentServerAddr, path), httpConfig, newHeaders, query, String.class);
if (isFail(result)) {
LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}",
serverListMgr.getCurrentServerAddr(), result.getCode());

View File

@ -25,6 +25,7 @@ import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.beat.BeatReactor;
import com.alibaba.nacos.client.naming.cache.DiskCache;
import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.utils.JacksonUtils;
@ -296,6 +297,14 @@ public class HostReactor implements Closeable {
return serviceInfoMap.get(serviceObj.getKey());
}
private void updateServiceNow(String serviceName, String clusters) {
try {
updateService(serviceName, clusters);
} catch (NacosException e) {
NAMING_LOGGER.error("[NA] failed to update serviceName: " + serviceName, e);
}
}
/**
* Schedule update if absent.
*
@ -323,7 +332,7 @@ public class HostReactor implements Closeable {
* @param serviceName service name
* @param clusters clusters
*/
public void updateServiceNow(String serviceName, String clusters) {
public void updateService(String serviceName, String clusters) throws NacosException {
ServiceInfo oldService = getServiceInfo0(serviceName, clusters);
try {
@ -332,8 +341,6 @@ public class HostReactor implements Closeable {
if (StringUtils.isNotEmpty(result)) {
processServiceJson(result);
}
} catch (Exception e) {
NAMING_LOGGER.error("[NA] failed to update serviceName: " + serviceName, e);
} finally {
if (oldService != null) {
synchronized (oldService) {
@ -375,26 +382,42 @@ public class HostReactor implements Closeable {
private final String serviceName;
/**
* the fail situation. 1:can't connect to server 2:serviceInfo's hosts is empty
*/
private int failCount = 0;
public UpdateTask(String serviceName, String clusters) {
this.serviceName = serviceName;
this.clusters = clusters;
}
private void incFailCount() {
int limit = 6;
if (failCount == limit) {
return;
}
failCount++;
}
private void resetFailCount() {
failCount = 0;
}
@Override
public void run() {
long delayTime = -1;
long delayTime = DEFAULT_DELAY;
try {
ServiceInfo serviceObj = serviceInfoMap.get(ServiceInfo.getKey(serviceName, clusters));
if (serviceObj == null) {
updateServiceNow(serviceName, clusters);
delayTime = DEFAULT_DELAY;
updateService(serviceName, clusters);
return;
}
if (serviceObj.getLastRefTime() <= lastRefTime) {
updateServiceNow(serviceName, clusters);
updateService(serviceName, clusters);
serviceObj = serviceInfoMap.get(ServiceInfo.getKey(serviceName, clusters));
} else {
// if serviceName already updated by push, we should not override it
@ -410,17 +433,18 @@ public class HostReactor implements Closeable {
NAMING_LOGGER.info("update task is stopped, service:" + serviceName + ", clusters:" + clusters);
return;
}
if (CollectionUtils.isEmpty(serviceObj.getHosts())) {
incFailCount();
return;
}
delayTime = serviceObj.getCacheMillis();
resetFailCount();
} catch (Throwable e) {
incFailCount();
NAMING_LOGGER.warn("[NA] failed to update serviceName: " + serviceName, e);
} finally {
if (delayTime > 0) {
executor.schedule(this, delayTime, TimeUnit.MILLISECONDS);
}
executor.schedule(this, Math.min(delayTime << failCount, DEFAULT_DELAY * 60), TimeUnit.MILLISECONDS);
}
}
}
}

View File

@ -596,7 +596,7 @@ public class NamingProxy implements Closeable {
try {
HttpRestResult<String> restResult = nacosRestTemplate
.exchangeForm(url, header, params, body, method, String.class);
.exchangeForm(url, header, Query.newInstance().initParams(params), body, method, String.class);
end = System.currentTimeMillis();
MetricsMonitor.getNamingRequestMonitor(method, url, String.valueOf(restResult.getCode()))

View File

@ -21,6 +21,7 @@ import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
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 com.fasterxml.jackson.databind.JsonNode;
@ -138,7 +139,7 @@ public class SecurityProxy {
}
try {
HttpRestResult<String> restResult = nacosRestTemplate
.postForm(url, Header.EMPTY, params, bodyMap, String.class);
.postForm(url, Header.EMPTY, Query.newInstance().initParams(params), bodyMap, String.class);
if (!restResult.ok()) {
SECURITY_LOGGER.error("login failed: {}", JacksonUtils.toJson(restResult));
return false;

View File

@ -229,7 +229,7 @@ public final class HttpUtils {
* @return {@link URI}
*/
public static URI buildUri(String url, Query query) throws URISyntaxException {
if (!query.isEmpty()) {
if (query != null && !query.isEmpty()) {
url = url + "?" + query.toQueryUrl();
}
return new URI(url);

View File

@ -66,26 +66,6 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {
execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseType, callback);
}
/**
* async http get URL request params are expanded using the given map {@code paramValues}.
*
* <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 headers
* @param paramValues paramValues
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void get(String url, Header header, Map<String, String> paramValues, Type responseType,
Callback<T> callback) {
execute(url, HttpMethod.GET, new RequestHttpEntity(header, Query.newInstance().initParams(paramValues)),
responseType, callback);
}
/**
* async get request, may be pulling a lot of data URL request params are expanded using the given query {@link
* Query}, More request parameters can be set via body.
@ -150,7 +130,7 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {
* async http put Json Create a new resource by PUTting the given body to http request, http header contentType
* default 'application/json;charset=UTF-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
* <p>URL request params are expanded using the given query {@link Query}.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type
*
@ -159,15 +139,36 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param query http query param
* @param body http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void putJson(String url, Header header, Map<String, String> paramValues, String body, Type responseType,
public <T> void putJson(String url, Header header, Query query, String body, Type responseType,
Callback<T> callback) {
execute(url, HttpMethod.PUT, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON),
Query.newInstance().initParams(paramValues), body), responseType, callback);
execute(url, HttpMethod.PUT,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON), query, body), responseType,
callback);
}
/**
* async http put Json Create a new resource by PUTting the given body to http request, http header contentType
* default 'application/json;charset=UTF-8'.
*
* <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 http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void putJson(String url, Header header, String body, Type responseType, Callback<T> callback) {
execute(url, HttpMethod.PUT, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON), body),
responseType, callback);
}
/**
@ -199,8 +200,6 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {
* async http put from Create a new resource by PUTting the given map {@code bodyValues} to http request, http
* header contentType default 'application/x-www-form-urlencoded;charset=utf-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type.
*
* <p>{@code callback} Result callback execution,
@ -208,15 +207,15 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param bodyValues http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void putForm(String url, Header header, Map<String, String> paramValues, Map<String, String> bodyValues,
Type responseType, Callback<T> callback) {
execute(url, HttpMethod.PUT, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues), responseType, callback);
public <T> void putForm(String url, Header header, Map<String, String> bodyValues, Type responseType,
Callback<T> callback) {
execute(url, HttpMethod.PUT,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), bodyValues),
responseType, callback);
}
/**
@ -244,7 +243,7 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {
* async http post Json Create a new resource by POSTing the given object to the http request, http header
* contentType default 'application/json;charset=UTF-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
* <p>URL request params are expanded using the given query {@link Query}.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type.
*
@ -253,15 +252,36 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param query http query param
* @param body http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void postJson(String url, Header header, Map<String, String> paramValues, String body, Type responseType,
public <T> void postJson(String url, Header header, Query query, String body, Type responseType,
Callback<T> callback) {
execute(url, HttpMethod.POST, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON),
Query.newInstance().initParams(paramValues), body), responseType, callback);
execute(url, HttpMethod.POST,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON), query, body), responseType,
callback);
}
/**
* async http post Json Create a new resource by POSTing the given object to the http request, http header
* contentType default 'application/json;charset=UTF-8'.
*
* <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 http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void postJson(String url, Header header, String body, Type responseType, Callback<T> callback) {
execute(url, HttpMethod.POST, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON), body),
responseType, callback);
}
/**
@ -293,8 +313,6 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {
* async http post from Create a new resource by PUTting the given map {@code bodyValues} to http request, http
* header contentType default 'application/x-www-form-urlencoded;charset=utf-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type.
*
* <p>{@code callback} Result callback execution,
@ -302,17 +320,15 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param bodyValues http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void postForm(String url, Header header, Map<String, String> paramValues, Map<String, String> bodyValues,
Type responseType, Callback<T> callback) {
public <T> void postForm(String url, Header header, Map<String, String> bodyValues, Type responseType,
Callback<T> callback) {
execute(url, HttpMethod.POST,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues), responseType, callback);
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), bodyValues),
responseType, callback);
}
@SuppressWarnings("unchecked")

View File

@ -70,25 +70,6 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
return execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseType);
}
/**
* http get URL request params are expanded using the given query {@link Query}.
*
* <p>{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type.
*
* @param url url
* @param header headers
* @param paramValues paramValues
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> get(String url, Header header, Map<String, String> paramValues, Type responseType)
throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(header,
Query.newInstance().initParams(paramValues));
return execute(url, HttpMethod.GET, requestHttpEntity, responseType);
}
/**
* http get URL request params are expanded using the given query {@link Query}.
*
@ -99,15 +80,14 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
* @param url url
* @param config http config
* @param header headers
* @param paramValues paramValues
* @param query http query param
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> get(String url, HttpClientConfig config, Header header,
Map<String, String> paramValues, Type responseType) throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(config, header,
Query.newInstance().initParams(paramValues));
public <T> HttpRestResult<T> get(String url, HttpClientConfig config, Header header, Query query, Type responseType)
throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(config, header, query);
return execute(url, HttpMethod.GET, requestHttpEntity, responseType);
}
@ -156,15 +136,14 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
* @param url url
* @param config http config
* @param header http header param
* @param paramValues http query param
* @param query http query param
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> delete(String url, HttpClientConfig config, Header header,
Map<String, String> paramValues, Type responseType) throws Exception {
return execute(url, HttpMethod.DELETE,
new RequestHttpEntity(config, header, Query.newInstance().initParams(paramValues)), responseType);
public <T> HttpRestResult<T> delete(String url, HttpClientConfig config, Header header, Query query,
Type responseType) throws Exception {
return execute(url, HttpMethod.DELETE, new RequestHttpEntity(config, header, query), responseType);
}
/**
@ -191,22 +170,41 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
* http put json Create a new resource by PUTting the given body to http request, http header contentType default
* 'application/json;charset=UTF-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
* <p>URL request params are expanded using the given query {@link Query}.
*
* <p>{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type.
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param query http query param
* @param body http body param
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> putJson(String url, Header header, Map<String, String> paramValues, String body,
Type responseType) throws Exception {
public <T> HttpRestResult<T> putJson(String url, Header header, Query query, String body, Type responseType)
throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON),
Query.newInstance().initParams(paramValues), body);
query, body);
return execute(url, HttpMethod.PUT, requestHttpEntity, responseType);
}
/**
* http put json Create a new resource by PUTting the given body to http request, http header contentType default
* 'application/json;charset=UTF-8'.
*
* <p>{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type.
*
* @param url url
* @param header http header param
* @param body http body param
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> putJson(String url, Header header, String body, Type responseType) throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON),
body);
return execute(url, HttpMethod.PUT, requestHttpEntity, responseType);
}
@ -237,23 +235,19 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
* http put from Create a new resource by PUTting the given map {@code bodyValues} to http request, http header
* contentType default 'application/x-www-form-urlencoded;charset=utf-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
*
* <p>{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type.
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param bodyValues http body param
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> putForm(String url, Header header, Map<String, String> paramValues,
Map<String, String> bodyValues, Type responseType) throws Exception {
public <T> HttpRestResult<T> putForm(String url, Header header, Map<String, String> bodyValues, Type responseType)
throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues);
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), bodyValues);
return execute(url, HttpMethod.PUT, requestHttpEntity, responseType);
}
@ -261,8 +255,6 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
* http put from Create a new resource by PUTting the given map {@code bodyValues} to http request, http header
* contentType default 'application/x-www-form-urlencoded;charset=utf-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
*
* <p>{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type.
*
* <p>{@code config} Specify the request config via {@link HttpClientConfig}
@ -270,17 +262,15 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
* @param url url
* @param config http config
* @param header http header param
* @param paramValues http query param
* @param bodyValues http body param
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> putForm(String url, HttpClientConfig config, Header header,
Map<String, String> paramValues, Map<String, String> bodyValues, Type responseType) throws Exception {
Map<String, String> bodyValues, Type responseType) throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(config,
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues);
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), bodyValues);
return execute(url, HttpMethod.PUT, requestHttpEntity, responseType);
}
@ -308,22 +298,41 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
* http post json Create a new resource by POSTing the given object to the http request, http header contentType
* default 'application/json;charset=UTF-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
* <p>URL request params are expanded using the given query {@link Query}.
*
* <p>{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type.
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param query http query param
* @param body http body param
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> postJson(String url, Header header, Map<String, String> paramValues, String body,
Type responseType) throws Exception {
public <T> HttpRestResult<T> postJson(String url, Header header, Query query, String body, Type responseType)
throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON),
Query.newInstance().initParams(paramValues), body);
query, body);
return execute(url, HttpMethod.POST, requestHttpEntity, responseType);
}
/**
* http post json Create a new resource by POSTing the given object to the http request, http header contentType
* default 'application/json;charset=UTF-8'.
*
* <p>{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type.
*
* @param url url
* @param header http header param
* @param body http body param
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> postJson(String url, Header header, String body, Type responseType) throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON),
body);
return execute(url, HttpMethod.POST, requestHttpEntity, responseType);
}
@ -354,23 +363,19 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
* http post from Create a new resource by PUTting the given map {@code bodyValues} to http request, http header
* contentType default 'application/x-www-form-urlencoded;charset=utf-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
*
* <p>{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type.
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param bodyValues http body param
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> postForm(String url, Header header, Map<String, String> paramValues,
Map<String, String> bodyValues, Type responseType) throws Exception {
public <T> HttpRestResult<T> postForm(String url, Header header, Map<String, String> bodyValues, Type responseType)
throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues);
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), bodyValues);
return execute(url, HttpMethod.POST, requestHttpEntity, responseType);
}
@ -378,8 +383,6 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
* http post from Create a new resource by PUTting the given map {@code bodyValues} to http request, http header
* contentType default 'application/x-www-form-urlencoded;charset=utf-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
*
* <p>{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type.
*
* <p>{@code config} Specify the request config via {@link HttpClientConfig}
@ -387,17 +390,15 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
* @param url url
* @param config http config
* @param header http header param
* @param paramValues http query param
* @param bodyValues http body param
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> postForm(String url, HttpClientConfig config, Header header,
Map<String, String> paramValues, Map<String, String> bodyValues, Type responseType) throws Exception {
Map<String, String> bodyValues, Type responseType) throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(config,
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues);
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), bodyValues);
return execute(url, HttpMethod.POST, requestHttpEntity, responseType);
}
@ -407,18 +408,17 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param query http query param
* @param bodyValues http body param
* @param httpMethod http method
* @param responseType return type
* @return {@link HttpRestResult}
* @throws Exception ex
*/
public <T> HttpRestResult<T> exchangeForm(String url, Header header, Map<String, String> paramValues,
Map<String, String> bodyValues, String httpMethod, Type responseType) throws Exception {
public <T> HttpRestResult<T> exchangeForm(String url, Header header, Query query, Map<String, String> bodyValues,
String httpMethod, Type responseType) throws Exception {
RequestHttpEntity requestHttpEntity = new RequestHttpEntity(
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues);
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues);
return execute(url, httpMethod, requestHttpEntity, responseType);
}
@ -450,7 +450,7 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate {
if (logger.isDebugEnabled()) {
logger.debug("HTTP method: {}, url: {}, body: {}", httpMethod, uri, requestEntity.getBody());
}
ResponseHandler<T> responseHandler = super.selectResponseHandler(responseType);
HttpClientResponse response = null;
try {

View File

@ -42,14 +42,22 @@ public class RequestHttpEntity {
this(null, header, query);
}
public RequestHttpEntity(HttpClientConfig httpClientConfig, Header header, Query query) {
this(httpClientConfig, header, query, null);
public RequestHttpEntity(Header header, Object body) {
this(null, header, null, body);
}
public RequestHttpEntity(Header header, Query query, Object body) {
this(null, header, query, body);
}
public RequestHttpEntity(HttpClientConfig httpClientConfig, Header header, Query query) {
this(httpClientConfig, header, query, null);
}
public RequestHttpEntity(HttpClientConfig httpClientConfig, Header header, Object body) {
this(httpClientConfig, header, null, body);
}
public RequestHttpEntity(HttpClientConfig httpClientConfig, Header header, Query query, Object body) {
handleHeader(header);
this.httpClientConfig = httpClientConfig;

View File

@ -502,8 +502,14 @@ public class JRaftServer {
Loggers.RAFT
.error("Fail to refresh route configuration for group : {}, status is : {}", groupName, status);
}
// fix issue #3661 https://github.com/alibaba/nacos/issues/3661
status = instance.refreshLeader(this.cliClientService, groupName, rpcRequestTimeoutMs);
if (!status.isOk()) {
Loggers.RAFT
.error("Fail to refresh leader for group : {}, status is : {}", groupName, status);
}
} catch (Exception e) {
Loggers.RAFT.error("Fail to refresh route configuration for group : {}, error is : {}", groupName, e);
Loggers.RAFT.error("Fail to refresh raft metadata info for group : {}, error is : {}", groupName, e);
}
}

View File

@ -516,19 +516,6 @@ public class InstanceController {
ClientInfo clientInfo = new ClientInfo(agent);
ObjectNode result = JacksonUtils.createEmptyJsonNode();
Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) {
if (Loggers.SRV_LOG.isDebugEnabled()) {
Loggers.SRV_LOG.debug("no instance to serve for service: {}", serviceName);
}
result.put("name", serviceName);
result.put("clusters", clusters);
result.replace("hosts", JacksonUtils.createEmptyArrayNode());
return result;
}
checkIfDisabled(service);
long cacheMillis = switchDomain.getDefaultCacheMillis();
// now try to enable the push
@ -546,6 +533,19 @@ public class InstanceController {
cacheMillis = switchDomain.getDefaultCacheMillis();
}
if (service == null) {
if (Loggers.SRV_LOG.isDebugEnabled()) {
Loggers.SRV_LOG.debug("no instance to serve for service: {}", serviceName);
}
result.put("name", serviceName);
result.put("clusters", clusters);
result.put("cacheMillis", cacheMillis);
result.replace("hosts", JacksonUtils.createEmptyArrayNode());
return result;
}
checkIfDisabled(service);
List<Instance> srvedIPs;
srvedIPs = service.srvIPs(Arrays.asList(StringUtils.split(clusters, ",")));

View File

@ -149,7 +149,7 @@ public class NacosAsyncRestTemplate_ITCase {
Map<String, String> param = new HashMap<>();
param.put("serviceName", "app-test");
CallbackMap<Map> callbackMap = new CallbackMap<>();
nacosRestTemplate.get(url, Header.newInstance(), param, Map.class, callbackMap);
nacosRestTemplate.get(url, Header.newInstance(), Query.newInstance().initParams(param), Map.class, callbackMap);
Thread.sleep(2000);
HttpRestResult<Map> restResult = callbackMap.getRestResult();
System.out.println(restResult.getData());

View File

@ -76,7 +76,7 @@ public class NacosRestTemplate_ITCase {
param.put("group", "DEFAULT_GROUP");
param.put("content", "aaa=b");
HttpRestResult<String> restResult = nacosRestTemplate
.postForm(url, Header.newInstance(), Query.EMPTY, param, String.class);
.postForm(url, Header.newInstance(), param, String.class);
Assert.assertTrue(restResult.ok());
System.out.println(restResult.getData());
System.out.println(restResult.getHeader());
@ -104,7 +104,7 @@ public class NacosRestTemplate_ITCase {
param.put("port", "8080");
param.put("ip", "11.11.11.11");
HttpRestResult<String> restResult = nacosRestTemplate
.postForm(url, Header.newInstance(), Query.newInstance(), param, String.class);
.postForm(url, Header.newInstance(), param, String.class);
Assert.assertTrue(restResult.ok());
System.out.println(restResult.getData());
}
@ -117,7 +117,7 @@ public class NacosRestTemplate_ITCase {
param.put("port", "8080");
param.put("ip", "11.11.11.11");
HttpRestResult<String> restResult = nacosRestTemplate
.putForm(url, Header.newInstance(), Query.newInstance(), param, String.class);
.putForm(url, Header.newInstance(), param, String.class);
Assert.assertTrue(restResult.ok());
System.out.println(restResult.getData());
}
@ -137,7 +137,7 @@ public class NacosRestTemplate_ITCase {
String url = IP + INSTANCE_PATH + "/instance/list";
Map<String, String> param = new HashMap<>();
param.put("serviceName", "app-test");
HttpRestResult<Map> restResult = nacosRestTemplate.get(url, Header.newInstance(), param, Map.class);
HttpRestResult<Map> restResult = nacosRestTemplate.get(url, Header.newInstance(), Query.newInstance().initParams(param), Map.class);
Assert.assertTrue(restResult.ok());
Assert.assertEquals(restResult.getData().get("dom"), "app-test");
System.out.println(restResult.getData());

View File

@ -24,7 +24,6 @@ import com.alibaba.nacos.common.http.client.HttpClientRequestInterceptor;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.client.response.HttpClientResponse;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.model.RequestHttpEntity;
import org.junit.Assert;
import org.junit.Before;
@ -119,7 +118,7 @@ public class NacosRestTemplate_Interceptors_ITCase {
param.put("group", "DEFAULT_GROUP");
param.put("content", "aaa=b");
HttpRestResult<String> restResult = nacosRestTemplate
.postForm(url, Header.newInstance(), Query.EMPTY, param, String.class);
.postForm(url, Header.newInstance(), param, String.class);
Assert.assertEquals(500, restResult.getCode());
Assert.assertEquals("Stop request", restResult.getData());
System.out.println(restResult.getData());

View File

@ -21,12 +21,13 @@ import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.test.base.HttpClient4Test;
import org.apache.http.HttpStatus;
import org.junit.Assert;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author nkorange
@ -178,13 +179,13 @@ public class NamingBase extends HttpClient4Test {
String url = "http://127.0.0.1:" + localPort + "/nacos/v1/ns/operator/switches?entry=overriddenServerStatus&value=" + status;
Header header = Header.newInstance();
header.addParam(HttpHeaderConsts.USER_AGENT_HEADER, "Nacos-Server");
HttpRestResult<String> result = nacosRestTemplate.putForm(url, header, Query.EMPTY, new HashMap<>(), String.class);
HttpRestResult<String> result = nacosRestTemplate.putForm(url, header, new HashMap<>(), String.class);
System.out.println(result);
Assert.assertEquals(HttpStatus.SC_OK, result.getCode());
url = "http://127.0.0.1:" + localPort + "/nacos/v1/ns/operator/switches?entry=autoChangeHealthCheckEnabled&value=" + false;
result = nacosRestTemplate.putForm(url, header, Query.EMPTY, new HashMap<>(), String.class);
result = nacosRestTemplate.putForm(url, header, new HashMap<>(), String.class);
System.out.println(result);
Assert.assertEquals(HttpStatus.SC_OK, result.getCode());
}
@ -194,7 +195,7 @@ public class NamingBase extends HttpClient4Test {
Header header = Header.newInstance();
header.addParam(HttpHeaderConsts.USER_AGENT_HEADER, "Nacos-Server");
HttpRestResult<String> result = nacosRestTemplate.putForm(url, header, Query.EMPTY, new HashMap<>(), String.class);
HttpRestResult<String> result = nacosRestTemplate.putForm(url, header, new HashMap<>(), String.class);
System.out.println(result);
Assert.assertEquals(HttpStatus.SC_OK, result.getCode());
}