From 63a4e30ae637100902848999dc0c7219af1e8cd4 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Sat, 18 Jul 2020 14:42:02 +0800 Subject: [PATCH] [ISSUE #3224]nacos-client module http client replace (#3348) * nacos-client module http client replace * fix code style problem * add HashMap initialCapacity * fix code style problem * Modify the header object, keep the original response header to avoid modifying the original logic code * fix code style problem * naming http client request exception messages output change * Merge code --- .../client/config/NacosConfigService.java | 80 ++- .../nacos/client/config/http/HttpAgent.java | 23 +- .../client/config/http/MetricsHttpAgent.java | 28 +- .../client/config/http/ServerHttpAgent.java | 218 ++++---- .../client/config/impl/ClientWorker.java | 70 ++- .../config/impl/ConfigHttpClientManager.java | 171 +++++++ .../client/config/impl/HttpSimpleClient.java | 3 + .../client/config/impl/ServerListManager.java | 23 +- .../nacos/client/config/impl/SpasAdapter.java | 42 +- .../nacos/client/naming/net/NamingProxy.java | 4 +- .../nacos/client/security/SecurityProxy.java | 6 +- .../alibaba/nacos/client/utils/ParamUtil.java | 3 +- .../http/client/JdkHttpClientResponse.java | 7 +- .../nacos/common/http/param/Header.java | 29 ++ .../nacos/test/config/ConfigAPI_CITCase.java | 479 ++++++++++-------- .../ConfigExportAndImportAPI_CITCase.java | 55 +- 16 files changed, 730 insertions(+), 511 deletions(-) create mode 100644 client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigHttpClientManager.java diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index 80e3280d021..a0c614011f9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -28,21 +28,20 @@ import com.alibaba.nacos.client.config.http.MetricsHttpAgent; import com.alibaba.nacos.client.config.http.ServerHttpAgent; import com.alibaba.nacos.client.config.impl.ClientWorker; -import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor; import com.alibaba.nacos.client.config.utils.ContentUtils; import com.alibaba.nacos.client.config.utils.ParamUtils; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.ValidatorUtils; +import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.utils.StringUtils; import org.slf4j.Logger; -import java.io.IOException; import java.net.HttpURLConnection; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; /** @@ -179,37 +178,34 @@ private boolean removeConfigInner(String tenant, String dataId, String group, St group = null2defaultGroup(group); ParamUtils.checkKeyParam(dataId, group); String url = Constants.CONFIG_CONTROLLER_PATH; - List params = new ArrayList(); - params.add("dataId"); - params.add(dataId); - params.add("group"); - params.add(group); + Map params = new HashMap(4); + params.put("dataId", dataId); + params.put("group", group); + if (StringUtils.isNotEmpty(tenant)) { - params.add("tenant"); - params.add(tenant); + params.put("tenant", tenant); } if (StringUtils.isNotEmpty(tag)) { - params.add("tag"); - params.add(tag); + params.put("tag", tag); } - HttpResult result = null; + HttpRestResult result = null; try { result = agent.httpDelete(url, null, params, encode, POST_TIMEOUT); - } catch (IOException ioe) { - LOGGER.warn("[remove] error, " + dataId + ", " + group + ", " + tenant + ", msg: " + ioe.toString()); + } catch (Exception ex) { + LOGGER.warn("[remove] error, " + dataId + ", " + group + ", " + tenant + ", msg: " + ex.toString()); return false; } - if (HttpURLConnection.HTTP_OK == result.code) { + if (result.ok()) { LOGGER.info("[{}] [remove] ok, dataId={}, group={}, tenant={}", agent.getName(), dataId, group, tenant); return true; - } else if (HttpURLConnection.HTTP_FORBIDDEN == result.code) { + } else if (HttpURLConnection.HTTP_FORBIDDEN == result.getCode()) { LOGGER.warn("[{}] [remove] error, dataId={}, group={}, tenant={}, code={}, msg={}", agent.getName(), dataId, - group, tenant, result.code, result.content); - throw new NacosException(result.code, result.content); + group, tenant, result.getCode(), result.getMessage()); + throw new NacosException(result.getCode(), result.getMessage()); } else { LOGGER.warn("[{}] [remove] error, dataId={}, group={}, tenant={}, code={}, msg={}", agent.getName(), dataId, - group, tenant, result.code, result.content); + group, tenant, result.getCode(), result.getMessage()); return false; } } @@ -228,52 +224,44 @@ private boolean publishConfigInner(String tenant, String dataId, String group, S content = cr.getContent(); String url = Constants.CONFIG_CONTROLLER_PATH; - List params = new ArrayList(); - params.add("dataId"); - params.add(dataId); - params.add("group"); - params.add(group); - params.add("content"); - params.add(content); + Map params = new HashMap(6); + params.put("dataId", dataId); + params.put("group", group); + params.put("content", content); if (StringUtils.isNotEmpty(tenant)) { - params.add("tenant"); - params.add(tenant); + params.put("tenant", tenant); } if (StringUtils.isNotEmpty(appName)) { - params.add("appName"); - params.add(appName); + params.put("appName", appName); } if (StringUtils.isNotEmpty(tag)) { - params.add("tag"); - params.add(tag); + params.put("tag", tag); } - - List headers = new ArrayList(); + Map headers = new HashMap(1); if (StringUtils.isNotEmpty(betaIps)) { - headers.add("betaIps"); - headers.add(betaIps); + headers.put("betaIps", betaIps); } - HttpResult result = null; + HttpRestResult result = null; try { result = agent.httpPost(url, headers, params, encode, POST_TIMEOUT); - } catch (IOException ioe) { + } catch (Exception ex) { LOGGER.warn("[{}] [publish-single] exception, dataId={}, group={}, msg={}", agent.getName(), dataId, group, - ioe.toString()); + ex.toString()); return false; } - if (HttpURLConnection.HTTP_OK == result.code) { + if (result.ok()) { LOGGER.info("[{}] [publish-single] ok, dataId={}, group={}, tenant={}, config={}", agent.getName(), dataId, group, tenant, ContentUtils.truncateContent(content)); return true; - } else if (HttpURLConnection.HTTP_FORBIDDEN == result.code) { + } else if (HttpURLConnection.HTTP_FORBIDDEN == result.getCode()) { LOGGER.warn("[{}] [publish-single] error, dataId={}, group={}, tenant={}, code={}, msg={}", agent.getName(), - dataId, group, tenant, result.code, result.content); - throw new NacosException(result.code, result.content); + dataId, group, tenant, result.getCode(), result.getMessage()); + throw new NacosException(result.getCode(), result.getMessage()); } else { LOGGER.warn("[{}] [publish-single] error, dataId={}, group={}, tenant={}, code={}, msg={}", agent.getName(), - dataId, group, tenant, result.code, result.content); + dataId, group, tenant, result.getCode(), result.getMessage()); return false; } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java index da856cc9f87..451b10dc73b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java @@ -17,11 +17,10 @@ package com.alibaba.nacos.client.config.http; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; +import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.lifecycle.Closeable; -import java.io.IOException; -import java.util.List; +import java.util.Map; /** * HttpAgent. @@ -46,11 +45,11 @@ public interface HttpAgent extends Closeable { * @param encoding http encode * @param readTimeoutMs http timeout * @return HttpResult http response - * @throws IOException If an input or output exception occurred + * @throws Exception If an input or output exception occurred */ - HttpResult httpGet(String path, List headers, List paramValues, String encoding, long readTimeoutMs) - throws IOException; + HttpRestResult httpGet(String path, Map headers, Map paramValues, + String encoding, long readTimeoutMs) throws Exception; /** * invoke http post method. @@ -61,10 +60,10 @@ HttpResult httpGet(String path, List headers, List paramValues, * @param encoding http encode * @param readTimeoutMs http timeout * @return HttpResult http response - * @throws IOException If an input or output exception occurred + * @throws Exception If an input or output exception occurred */ - HttpResult httpPost(String path, List headers, List paramValues, String encoding, - long readTimeoutMs) throws IOException; + HttpRestResult httpPost(String path, Map headers, Map paramValues, + String encoding, long readTimeoutMs) throws Exception; /** * invoke http delete method. @@ -75,10 +74,10 @@ HttpResult httpPost(String path, List headers, List paramValues, * @param encoding http encode * @param readTimeoutMs http timeout * @return HttpResult http response - * @throws IOException If an input or output exception occurred + * @throws Exception If an input or output exception occurred */ - HttpResult httpDelete(String path, List headers, List paramValues, String encoding, - long readTimeoutMs) throws IOException; + HttpRestResult httpDelete(String path, Map headers, Map paramValues, + String encoding, long readTimeoutMs) throws Exception; /** * get name. diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java index ede4e830fd0..caac01e2b0a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java @@ -17,12 +17,12 @@ package com.alibaba.nacos.client.config.http; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.monitor.MetricsMonitor; +import com.alibaba.nacos.common.http.HttpRestResult; import io.prometheus.client.Histogram; import java.io.IOException; -import java.util.List; +import java.util.Map; /** * MetricsHttpAgent. @@ -43,12 +43,12 @@ public void start() throws NacosException { } @Override - public HttpResult httpGet(String path, List headers, List paramValues, String encoding, - long readTimeoutMs) throws IOException { + public HttpRestResult httpGet(String path, Map headers, Map paramValues, + String encode, long readTimeoutMs) throws Exception { Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("GET", path, "NA"); - HttpResult result; + HttpRestResult result; try { - result = httpAgent.httpGet(path, headers, paramValues, encoding, readTimeoutMs); + result = httpAgent.httpGet(path, headers, paramValues, encode, readTimeoutMs); } catch (IOException e) { throw e; } finally { @@ -60,12 +60,12 @@ public HttpResult httpGet(String path, List headers, List paramV } @Override - public HttpResult httpPost(String path, List headers, List paramValues, String encoding, - long readTimeoutMs) throws IOException { + public HttpRestResult httpPost(String path, Map headers, Map paramValues, + String encode, long readTimeoutMs) throws Exception { Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("POST", path, "NA"); - HttpResult result; + HttpRestResult result; try { - result = httpAgent.httpPost(path, headers, paramValues, encoding, readTimeoutMs); + result = httpAgent.httpPost(path, headers, paramValues, encode, readTimeoutMs); } catch (IOException e) { throw e; } finally { @@ -77,12 +77,12 @@ public HttpResult httpPost(String path, List headers, List param } @Override - public HttpResult httpDelete(String path, List headers, List paramValues, String encoding, - long readTimeoutMs) throws IOException { + public HttpRestResult httpDelete(String path, Map headers, Map paramValues, + String encode, long readTimeoutMs) throws Exception { Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("DELETE", path, "NA"); - HttpResult result; + HttpRestResult result; try { - result = httpAgent.httpDelete(path, headers, paramValues, encoding, readTimeoutMs); + result = httpAgent.httpDelete(path, headers, paramValues, encode, readTimeoutMs); } catch (IOException e) { throw e; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index 81c0f155ee5..6d83577ac58 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -19,8 +19,7 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.client.config.impl.HttpSimpleClient; -import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; +import com.alibaba.nacos.client.config.impl.ConfigHttpClientManager; import com.alibaba.nacos.client.config.impl.ServerListManager; import com.alibaba.nacos.client.config.impl.SpasAdapter; import com.alibaba.nacos.client.identify.StsConfig; @@ -28,29 +27,36 @@ import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.TemplateUtils; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.HttpClientConfig; +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.ConvertUtils; -import com.alibaba.nacos.common.utils.IoUtils; +import com.alibaba.nacos.common.utils.ExceptionUtil; import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.ThreadUtils; +import com.alibaba.nacos.common.utils.UuidUtils; +import com.alibaba.nacos.common.utils.VersionUtils; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.type.TypeReference; import org.slf4j.Logger; -import java.io.IOException; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.SocketTimeoutException; -import java.net.URL; -import java.util.ArrayList; import java.util.Date; -import java.util.List; +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. @@ -61,6 +67,9 @@ public class ServerHttpAgent implements HttpAgent { private static final Logger LOGGER = LogUtils.logger(ServerHttpAgent.class); + private static final NacosRestTemplate NACOS_RESTTEMPLATE = ConfigHttpClientManager.getInstance() + .getNacosRestTemplate(); + private SecurityProxy securityProxy; private String namespaceId; @@ -69,40 +78,28 @@ public class ServerHttpAgent implements HttpAgent { private ScheduledExecutorService executorService; - /** - * Invoke http get method. - * - * @param path 相对于web应用根,以/开头 - * @param headers headers - * @param paramValues parameters - * @param encoding encoding - * @param readTimeoutMs time out milliseconds - * @return http result - * @throws IOException io exception - */ @Override - public HttpResult httpGet(String path, List headers, List paramValues, String encoding, - long readTimeoutMs) throws IOException { + public HttpRestResult httpGet(String path, Map headers, Map paramValues, + String encode, long readTimeoutMs) throws Exception { final long endTime = System.currentTimeMillis() + readTimeoutMs; - final boolean isSsl = false; injectSecurityInfo(paramValues); String currentServerAddr = serverListMgr.getCurrentServerAddr(); int maxRetry = this.maxRetry; - + HttpClientConfig httpConfig = HttpClientConfig.builder() + .setReadTimeOutMillis(Long.valueOf(readTimeoutMs).intValue()) + .setConTimeOutMillis(ConfigHttpClientManager.getInstance().getConnectTimeoutOrDefault(100)).build(); do { try { - List newHeaders = getSpasHeaders(paramValues); + Header newHeaders = getSpasHeaders(paramValues, encode); if (headers != null) { newHeaders.addAll(headers); } - HttpResult result = HttpSimpleClient - .httpGet(getUrl(currentServerAddr, path), newHeaders, paramValues, encoding, readTimeoutMs, - isSsl); - if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR - || result.code == HttpURLConnection.HTTP_BAD_GATEWAY - || result.code == HttpURLConnection.HTTP_UNAVAILABLE) { + + HttpRestResult result = NACOS_RESTTEMPLATE + .get(getUrl(currentServerAddr, path), httpConfig, newHeaders, paramValues, String.class); + if (isFail(result)) { LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", - serverListMgr.getCurrentServerAddr(), result.code); + serverListMgr.getCurrentServerAddr(), result.getCode()); } else { // Update the currently available server addr serverListMgr.updateCurrentServerAddr(currentServerAddr); @@ -114,10 +111,10 @@ public HttpResult httpGet(String path, List headers, List paramV } catch (SocketTimeoutException socketTimeoutException) { LOGGER.error("[NACOS SocketTimeoutException httpGet] currentServerAddr:{}, err : {}", serverListMgr.getCurrentServerAddr(), socketTimeoutException.getMessage()); - } catch (IOException ioException) { - LOGGER.error("[NACOS IOException httpGet] currentServerAddr: " + serverListMgr.getCurrentServerAddr(), - ioException); - throw ioException; + } catch (Exception ex) { + LOGGER.error("[NACOS Exception httpGet] currentServerAddr: " + serverListMgr.getCurrentServerAddr(), + ex); + throw ex; } if (serverListMgr.getIterator().hasNext()) { @@ -138,30 +135,29 @@ public HttpResult httpGet(String path, List headers, List paramV } @Override - public HttpResult httpPost(String path, List headers, List paramValues, String encoding, - long readTimeoutMs) throws IOException { + public HttpRestResult httpPost(String path, Map headers, Map paramValues, + String encode, long readTimeoutMs) throws Exception { final long endTime = System.currentTimeMillis() + readTimeoutMs; - boolean isSsl = false; injectSecurityInfo(paramValues); String currentServerAddr = serverListMgr.getCurrentServerAddr(); int maxRetry = this.maxRetry; - + HttpClientConfig httpConfig = HttpClientConfig.builder() + .setReadTimeOutMillis(Long.valueOf(readTimeoutMs).intValue()) + .setConTimeOutMillis(ConfigHttpClientManager.getInstance().getConnectTimeoutOrDefault(3000)).build(); do { try { - List newHeaders = getSpasHeaders(paramValues); + Header newHeaders = getSpasHeaders(paramValues, encode); if (headers != null) { newHeaders.addAll(headers); } + HttpRestResult result = NACOS_RESTTEMPLATE + .postForm(getUrl(currentServerAddr, path), httpConfig, newHeaders, + new HashMap(0), paramValues, String.class); - HttpResult result = HttpSimpleClient - .httpPost(getUrl(currentServerAddr, path), newHeaders, paramValues, encoding, readTimeoutMs, - isSsl); - if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR - || result.code == HttpURLConnection.HTTP_BAD_GATEWAY - || result.code == HttpURLConnection.HTTP_UNAVAILABLE) { + if (isFail(result)) { LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", currentServerAddr, - result.code); + result.getCode()); } else { // Update the currently available server addr serverListMgr.updateCurrentServerAddr(currentServerAddr); @@ -173,9 +169,9 @@ public HttpResult httpPost(String path, List headers, List param } catch (SocketTimeoutException socketTimeoutException) { LOGGER.error("[NACOS SocketTimeoutException httpPost] currentServerAddr: {}, err : {}", currentServerAddr, socketTimeoutException.getMessage()); - } catch (IOException ioe) { - LOGGER.error("[NACOS IOException httpPost] currentServerAddr: " + currentServerAddr, ioe); - throw ioe; + } catch (Exception ex) { + LOGGER.error("[NACOS Exception httpPost] currentServerAddr: " + currentServerAddr, ex); + throw ex; } if (serverListMgr.getIterator().hasNext()) { @@ -196,46 +192,42 @@ public HttpResult httpPost(String path, List headers, List param } @Override - public HttpResult httpDelete(String path, List headers, List paramValues, String encoding, - long readTimeoutMs) throws IOException { + public HttpRestResult httpDelete(String path, Map headers, Map paramValues, + String encode, long readTimeoutMs) throws Exception { final long endTime = System.currentTimeMillis() + readTimeoutMs; - boolean isSsl = false; injectSecurityInfo(paramValues); String currentServerAddr = serverListMgr.getCurrentServerAddr(); int maxRetry = this.maxRetry; - + HttpClientConfig httpConfig = HttpClientConfig.builder() + .setReadTimeOutMillis(Long.valueOf(readTimeoutMs).intValue()) + .setConTimeOutMillis(ConfigHttpClientManager.getInstance().getConnectTimeoutOrDefault(100)).build(); do { try { - List newHeaders = getSpasHeaders(paramValues); + Header newHeaders = getSpasHeaders(paramValues, encode); if (headers != null) { newHeaders.addAll(headers); } - HttpResult result = HttpSimpleClient - .httpDelete(getUrl(currentServerAddr, path), newHeaders, paramValues, encoding, readTimeoutMs, - isSsl); - if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR - || result.code == HttpURLConnection.HTTP_BAD_GATEWAY - || result.code == HttpURLConnection.HTTP_UNAVAILABLE) { + HttpRestResult result = NACOS_RESTTEMPLATE + .delete(getUrl(currentServerAddr, path), httpConfig, newHeaders, paramValues, String.class); + if (isFail(result)) { LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", - serverListMgr.getCurrentServerAddr(), result.code); + serverListMgr.getCurrentServerAddr(), result.getCode()); } else { // Update the currently available server addr serverListMgr.updateCurrentServerAddr(currentServerAddr); return result; } } catch (ConnectException connectException) { - connectException.printStackTrace(); LOGGER.error("[NACOS ConnectException httpDelete] currentServerAddr:{}, err : {}", - serverListMgr.getCurrentServerAddr(), connectException.getMessage()); + serverListMgr.getCurrentServerAddr(), ExceptionUtil.getStackTrace(connectException)); } catch (SocketTimeoutException stoe) { - stoe.printStackTrace(); LOGGER.error("[NACOS SocketTimeoutException httpDelete] currentServerAddr:{}, err : {}", - serverListMgr.getCurrentServerAddr(), stoe.getMessage()); - } catch (IOException ioe) { + serverListMgr.getCurrentServerAddr(), ExceptionUtil.getStackTrace(stoe)); + } catch (Exception ex) { LOGGER.error( - "[NACOS IOException httpDelete] currentServerAddr: " + serverListMgr.getCurrentServerAddr(), - ioe); - throw ioe; + "[NACOS Exception httpDelete] currentServerAddr: " + serverListMgr.getCurrentServerAddr(), + ex); + throw ex; } if (serverListMgr.getIterator().hasNext()) { @@ -261,6 +253,12 @@ private String getUrl(String serverAddr, String relativePath) { return serverAddr + contextPath + relativePath; } + private boolean isFail(HttpRestResult result) { + return result.getCode() == HttpURLConnection.HTTP_INTERNAL_ERROR + || result.getCode() == HttpURLConnection.HTTP_BAD_GATEWAY + || result.getCode() == HttpURLConnection.HTTP_UNAVAILABLE; + } + public static String getAppname() { return ParamUtil.getAppName(); } @@ -276,7 +274,7 @@ public ServerHttpAgent(ServerListManager mgr, Properties properties) { public ServerHttpAgent(Properties properties) throws NacosException { this.serverListMgr = new ServerListManager(properties); - this.securityProxy = new SecurityProxy(properties); + this.securityProxy = new SecurityProxy(properties, NACOS_RESTTEMPLATE); this.namespaceId = properties.getProperty(PropertyKeyConst.NAMESPACE); init(properties); this.securityProxy.login(this.serverListMgr.getServerUrls()); @@ -301,14 +299,12 @@ public void run() { } - private void injectSecurityInfo(List params) { + private void injectSecurityInfo(Map params) { if (StringUtils.isNotBlank(securityProxy.getAccessToken())) { - params.add(Constants.ACCESS_TOKEN); - params.add(securityProxy.getAccessToken()); + params.put(Constants.ACCESS_TOKEN, securityProxy.getAccessToken()); } - if (StringUtils.isNotBlank(namespaceId) && !params.contains(SpasAdapter.TENANT_KEY)) { - params.add(SpasAdapter.TENANT_KEY); - params.add(namespaceId); + if (StringUtils.isNotBlank(namespaceId) && !params.containsKey(SpasAdapter.TENANT_KEY)) { + params.put(SpasAdapter.TENANT_KEY, namespaceId); } } @@ -358,29 +354,37 @@ public void start() throws NacosException { serverListMgr.start(); } - private List getSpasHeaders(List paramValues) throws IOException { - List newHeaders = new ArrayList(); + private Header getSpasHeaders(Map paramValues, String encode) throws Exception { + Header header = Header.newInstance(); // STS 临时凭证鉴权的优先级高于 AK/SK 鉴权 if (StsConfig.getInstance().isStsOn()) { StsCredential stsCredential = getStsCredential(); accessKey = stsCredential.accessKeyId; secretKey = stsCredential.accessKeySecret; - newHeaders.add("Spas-SecurityToken"); - newHeaders.add(stsCredential.securityToken); + header.addParam("Spas-SecurityToken", stsCredential.securityToken); } if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotEmpty(secretKey)) { - newHeaders.add("Spas-AccessKey"); - newHeaders.add(accessKey); - List signHeaders = SpasAdapter.getSignHeaders(paramValues, secretKey); + header.addParam("Spas-AccessKey", accessKey); + Map signHeaders = SpasAdapter.getSignHeaders(paramValues, secretKey); if (signHeaders != null) { - newHeaders.addAll(signHeaders); + header.addAll(signHeaders); } } - return newHeaders; + String ts = String.valueOf(System.currentTimeMillis()); + String token = MD5Utils.md5Hex(ts + ParamUtil.getAppKey(), Constants.ENCODE); + + header.addParam(Constants.CLIENT_APPNAME_HEADER, ParamUtil.getAppName()); + header.addParam(Constants.CLIENT_REQUEST_TS_HEADER, ts); + header.addParam(Constants.CLIENT_REQUEST_TOKEN_HEADER, token); + header.addParam(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version); + header.addParam("exConfigInfo", "true"); + header.addParam(HttpHeaderConsts.REQUEST_ID, UuidUtils.generateUuid()); + header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, encode); + return header; } - private StsCredential getStsCredential() throws IOException { + private StsCredential getStsCredential() throws Exception { boolean cacheSecurityCredentials = StsConfig.getInstance().isCacheSecurityCredentials(); if (cacheSecurityCredentials && stsCredential != null) { long currentTime = System.currentTimeMillis(); @@ -400,40 +404,29 @@ private StsCredential getStsCredential() throws IOException { return stsCredential; } - private static String getStsResponse() throws IOException { + private static String getStsResponse() throws Exception { String securityCredentials = StsConfig.getInstance().getSecurityCredentials(); if (securityCredentials != null) { return securityCredentials; } String securityCredentialsUrl = StsConfig.getInstance().getSecurityCredentialsUrl(); - HttpURLConnection conn = null; - int respCode; - String response; try { - conn = (HttpURLConnection) new URL(securityCredentialsUrl).openConnection(); - conn.setRequestMethod("GET"); - conn.setConnectTimeout(ParamUtil.getConnectTimeout() > 100 ? ParamUtil.getConnectTimeout() : 100); - conn.setReadTimeout(1000); - conn.connect(); - respCode = conn.getResponseCode(); - if (HttpURLConnection.HTTP_OK == respCode) { - response = IoUtils.toString(conn.getInputStream(), Constants.ENCODE); - } else { - response = IoUtils.toString(conn.getErrorStream(), Constants.ENCODE); + HttpRestResult result = NACOS_RESTTEMPLATE + .get(securityCredentialsUrl, Header.EMPTY, Query.EMPTY, String.class); + + if (!result.ok()) { + LOGGER.error( + "can not get security credentials, securityCredentialsUrl: {}, responseCode: {}, response: {}", + securityCredentialsUrl, result.getCode(), result.getMessage()); + throw new NacosException(NacosException.SERVER_ERROR, + "can not get security credentials, responseCode: " + result.getCode() + ", response: " + result + .getMessage()); } - } catch (IOException e) { + return result.getData(); + } catch (Exception e) { LOGGER.error("can not get security credentials", e); throw e; - } finally { - IoUtils.closeQuietly(conn); - } - if (HttpURLConnection.HTTP_OK == respCode) { - return response; } - LOGGER.error("can not get security credentials, securityCredentialsUrl: {}, responseCode: {}, response: {}", - securityCredentialsUrl, respCode, response); - throw new IOException( - "can not get security credentials, responseCode: " + respCode + ", response: " + response); } @Override @@ -461,6 +454,7 @@ public void shutdown() throws NacosException { String className = this.getClass().getName(); LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdownThreadPool(executorService, LOGGER); + ConfigHttpClientManager.getInstance().shutdown(); LOGGER.info("{} do shutdown stop", className); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index d92976b455a..93578dda94c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -24,13 +24,13 @@ import com.alibaba.nacos.client.config.common.GroupKey; import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; import com.alibaba.nacos.client.config.http.HttpAgent; -import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.utils.ContentUtils; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.TenantUtil; +import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.MD5Utils; @@ -43,7 +43,6 @@ import java.net.HttpURLConnection; import java.net.URLDecoder; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; @@ -285,29 +284,32 @@ public String[] getServerConfig(String dataId, String group, String tenant, long group = Constants.DEFAULT_GROUP; } - HttpResult result = null; + HttpRestResult result = null; try { - List params = null; + Map params = new HashMap(3); if (StringUtils.isBlank(tenant)) { - params = new ArrayList(Arrays.asList("dataId", dataId, "group", group)); + params.put("dataId", dataId); + params.put("group", group); } else { - params = new ArrayList(Arrays.asList("dataId", dataId, "group", group, "tenant", tenant)); + params.put("dataId", dataId); + params.put("group", group); + params.put("tenant", tenant); } result = agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), readTimeout); - } catch (IOException e) { + } catch (Exception ex) { String message = String .format("[%s] [sub-server] get server config exception, dataId=%s, group=%s, tenant=%s", agent.getName(), dataId, group, tenant); - LOGGER.error(message, e); - throw new NacosException(NacosException.SERVER_ERROR, e); + LOGGER.error(message, ex); + throw new NacosException(NacosException.SERVER_ERROR, ex); } - switch (result.code) { + switch (result.getCode()) { case HttpURLConnection.HTTP_OK: - LocalConfigInfoProcessor.saveSnapshot(agent.getName(), dataId, group, tenant, result.content); - ct[0] = result.content; - if (result.headers.containsKey(CONFIG_TYPE)) { - ct[1] = result.headers.get(CONFIG_TYPE).get(0); + LocalConfigInfoProcessor.saveSnapshot(agent.getName(), dataId, group, tenant, result.getData()); + ct[0] = result.getData(); + if (result.getHeader().getValue(CONFIG_TYPE) != null) { + ct[1] = result.getHeader().getValue(CONFIG_TYPE); } else { ct[1] = ConfigType.TEXT.getType(); } @@ -325,13 +327,13 @@ public String[] getServerConfig(String dataId, String group, String tenant, long case HttpURLConnection.HTTP_FORBIDDEN: { LOGGER.error("[{}] [sub-server-error] no right, dataId={}, group={}, tenant={}", agent.getName(), dataId, group, tenant); - throw new NacosException(result.code, result.content); + throw new NacosException(result.getCode(), result.getMessage()); } default: { LOGGER.error("[{}] [sub-server-error] dataId={}, group={}, tenant={}, code={}", agent.getName(), - dataId, group, tenant, result.code); - throw new NacosException(result.code, - "http error, code=" + result.code + ",dataId=" + dataId + ",group=" + group + ",tenant=" + dataId, group, tenant, result.getCode()); + throw new NacosException(result.getCode(), + "http error, code=" + result.getCode() + ",dataId=" + dataId + ",group=" + group + ",tenant=" + tenant); } } @@ -405,10 +407,9 @@ public void checkConfigInfo() { * @param cacheDatas CacheDatas for config infomations. * @param inInitializingCacheList initial cache lists. * @return String include dataId and group (ps: it maybe null). - * @throws IOException Exception. + * @throws Exception Exception. */ - List checkUpdateDataIds(List cacheDatas, List inInitializingCacheList) - throws IOException { + List checkUpdateDataIds(List cacheDatas, List inInitializingCacheList) throws Exception { StringBuilder sb = new StringBuilder(); for (CacheData cacheData : cacheDatas) { if (!cacheData.isUseLocalConfigInfo()) { @@ -439,20 +440,16 @@ List checkUpdateDataIds(List cacheDatas, List inIniti * @return The updated dataId list(ps: it maybe null). * @throws IOException Exception. */ - List checkUpdateConfigStr(String probeUpdateString, boolean isInitializingCacheList) throws IOException { - - List params = new ArrayList(2); - params.add(Constants.PROBE_MODIFY_REQUEST); - params.add(probeUpdateString); + List checkUpdateConfigStr(String probeUpdateString, boolean isInitializingCacheList) throws Exception { - List headers = new ArrayList(2); - headers.add("Long-Pulling-Timeout"); - headers.add("" + timeout); + Map params = new HashMap(2); + params.put(Constants.PROBE_MODIFY_REQUEST, probeUpdateString); + Map headers = new HashMap(2); + headers.put("Long-Pulling-Timeout", "" + timeout); // told server do not hang me up if new initializing cacheData added in if (isInitializingCacheList) { - headers.add("Long-Pulling-Timeout-No-Hangup"); - headers.add("true"); + headers.put("Long-Pulling-Timeout-No-Hangup", "true"); } if (StringUtils.isBlank(probeUpdateString)) { @@ -464,18 +461,19 @@ List checkUpdateConfigStr(String probeUpdateString, boolean isInitializi // increase the client's read timeout to avoid this problem. long readTimeoutMs = timeout + (long) Math.round(timeout >> 1); - HttpResult result = agent + HttpRestResult result = agent .httpPost(Constants.CONFIG_CONTROLLER_PATH + "/listener", headers, params, agent.getEncode(), readTimeoutMs); - if (HttpURLConnection.HTTP_OK == result.code) { + if (result.ok()) { setHealthServer(true); - return parseUpdateDataIdResponse(result.content); + return parseUpdateDataIdResponse(result.getData()); } else { setHealthServer(false); - LOGGER.error("[{}] [check-update] get changed dataId error, code: {}", agent.getName(), result.code); + LOGGER.error("[{}] [check-update] get changed dataId error, code: {}", agent.getName(), + result.getCode()); } - } catch (IOException e) { + } catch (Exception e) { setHealthServer(false); LOGGER.error("[" + agent.getName() + "] [check-update] get changed dataId exception", e); throw e; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigHttpClientManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigHttpClientManager.java new file mode 100644 index 00000000000..d28ef4b2bd4 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigHttpClientManager.java @@ -0,0 +1,171 @@ +/* + * 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.client.config.impl; + +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.client.utils.LogUtils; +import com.alibaba.nacos.client.utils.ParamUtil; +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.HttpClientRequestInterceptor; +import com.alibaba.nacos.common.http.client.HttpClientResponse; +import com.alibaba.nacos.common.http.client.NacosRestTemplate; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.lifecycle.Closeable; +import com.alibaba.nacos.common.model.RequestHttpEntity; +import com.alibaba.nacos.common.utils.ExceptionUtil; +import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.common.utils.MD5Utils; +import org.slf4j.Logger; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; + +import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; + +/** + * config http Manager. + * + * @author mai.jh + */ +public class ConfigHttpClientManager implements Closeable { + + private static final Logger LOGGER = LogUtils.logger(ConfigHttpClientManager.class); + + private static final HttpClientFactory HTTP_CLIENT_FACTORY = new ConfigHttpClientFactory(); + + private static final int CON_TIME_OUT_MILLIS = ParamUtil.getConnectTimeout(); + + private static final int READ_TIME_OUT_MILLIS = 3000; + + private static final NacosRestTemplate NACOS_REST_TEMPLATE; + + static { + NACOS_REST_TEMPLATE = HttpClientBeanHolder.getNacosRestTemplate(HTTP_CLIENT_FACTORY); + NACOS_REST_TEMPLATE.getInterceptors().add(new LimiterHttpClientRequestInterceptor()); + } + + private static class ConfigHttpClientManagerInstance { + + private static final ConfigHttpClientManager INSTANCE = new ConfigHttpClientManager(); + } + + public static ConfigHttpClientManager getInstance() { + return ConfigHttpClientManagerInstance.INSTANCE; + } + + @Override + public void shutdown() throws NacosException { + NAMING_LOGGER.warn("[ConfigHttpClientManager] Start destroying NacosRestTemplate"); + try { + HttpClientBeanHolder.shutdownNacostSyncRest(HTTP_CLIENT_FACTORY.getClass().getName()); + } catch (Exception ex) { + NAMING_LOGGER.error("[ConfigHttpClientManager] An exception occurred when the HTTP client was closed : {}", + ExceptionUtil.getStackTrace(ex)); + } + NAMING_LOGGER.warn("[ConfigHttpClientManager] Destruction of the end"); + } + + /** + * get connectTimeout. + * + * @param connectTimeout connectTimeout + * @return int return max timeout + */ + public int getConnectTimeoutOrDefault(int connectTimeout) { + return Math.max(CON_TIME_OUT_MILLIS, connectTimeout); + } + + /** + * get NacosRestTemplate Instance. + * + * @return NacosRestTemplate + */ + public NacosRestTemplate getNacosRestTemplate() { + return NACOS_REST_TEMPLATE; + } + + /** + * ConfigHttpClientFactory. + */ + private static class ConfigHttpClientFactory extends AbstractHttpClientFactory { + + @Override + protected HttpClientConfig buildHttpClientConfig() { + return HttpClientConfig.builder().setConTimeOutMillis(CON_TIME_OUT_MILLIS) + .setReadTimeOutMillis(READ_TIME_OUT_MILLIS).build(); + } + + @Override + protected Logger assignLogger() { + return LOGGER; + } + } + + /** + * config Limiter implement. + */ + private static class LimiterHttpClientRequestInterceptor implements HttpClientRequestInterceptor { + + @Override + public boolean isIntercept(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity) { + final String body = requestHttpEntity.getBody() == null ? "" : JacksonUtils.toJson(requestHttpEntity.getBody()); + return Limiter.isLimit(MD5Utils.md5Hex(uri + body, Constants.ENCODE)); + } + + @Override + public HttpClientResponse intercept() { + return new LimitResponse(); + } + } + + /** + * Limit Interrupt response. + */ + private static class LimitResponse implements HttpClientResponse { + + @Override + public Header getHeaders() { + return Header.EMPTY; + } + + @Override + public InputStream getBody() throws IOException { + return new ByteArrayInputStream("More than client-side current limit threshold".getBytes()); + } + + @Override + public int getStatusCode() { + return NacosException.CLIENT_OVER_THRESHOLD; + } + + @Override + public String getStatusText() { + return null; + } + + @Override + public void close() throws IOException { + + } + } +} diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java index f3861c14646..67187378c07 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java @@ -20,6 +20,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.client.NacosRestTemplate; import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.common.utils.UuidUtils; @@ -39,7 +40,9 @@ * Http tool. * * @author Nacos + * @deprecated Use NacosRestTemplate{@link NacosRestTemplate} unified http client */ +@Deprecated public class HttpSimpleClient { /** diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index 2c3b2119aa4..b666ad80d80 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -19,11 +19,14 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.SystemPropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.utils.EnvUtil; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.TemplateUtils; +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.lifecycle.Closeable; import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.utils.IoUtils; @@ -31,9 +34,7 @@ import com.alibaba.nacos.common.utils.ThreadUtils; import org.slf4j.Logger; -import java.io.IOException; import java.io.StringReader; -import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -59,6 +60,8 @@ public class ServerListManager implements Closeable { private static final String HTTP = "http://"; + private final NacosRestTemplate nacosRestTemplate = ConfigHttpClientManager.getInstance().getNacosRestTemplate(); + private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { @Override public Thread newThread(Runnable r) { @@ -342,13 +345,13 @@ private void updateIfChanged(List newList) { private List getApacheServerList(String url, String name) { try { - HttpResult httpResult = HttpSimpleClient.httpGet(url, null, null, null, 3000); - - if (HttpURLConnection.HTTP_OK == httpResult.code) { + HttpRestResult httpResult = nacosRestTemplate.get(url, Header.EMPTY, Query.EMPTY, String.class); + + if (httpResult.ok()) { if (DEFAULT_NAME.equals(name)) { - EnvUtil.setSelfEnv(httpResult.headers); + EnvUtil.setSelfEnv(httpResult.getHeader().getOriginalResponseHeader()); } - List lines = IoUtils.readLines(new StringReader(httpResult.content)); + List lines = IoUtils.readLines(new StringReader(httpResult.getData())); List result = new ArrayList(lines.size()); for (String serverAddr : lines) { if (StringUtils.isNotBlank(serverAddr)) { @@ -364,10 +367,10 @@ private List getApacheServerList(String url, String name) { return result; } else { LOGGER.error("[check-serverlist] error. addressServerUrl: {}, code: {}", addressServerUrl, - httpResult.code); + httpResult.getCode()); return null; } - } catch (IOException e) { + } catch (Exception e) { LOGGER.error("[check-serverlist] exception. url: " + url, e); return null; } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java index 210dbc6f7d7..418ef08668f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java @@ -24,11 +24,7 @@ import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; -import java.util.List; import java.util.Map; /** @@ -37,44 +33,34 @@ * @author Nacos */ public class SpasAdapter { - - public static List getSignHeaders(String resource, String secretKey) { - List header = new ArrayList(); + + public static Map getSignHeaders(String resource, String secretKey) { + Map header = new HashMap(2); String timeStamp = String.valueOf(System.currentTimeMillis()); - header.add("Timestamp"); - header.add(timeStamp); + header.put("Timestamp", timeStamp); if (secretKey != null) { - header.add("Spas-Signature"); String signature = ""; if (StringUtils.isBlank(resource)) { signature = signWithHmacSha1Encrypt(timeStamp, secretKey); } else { signature = signWithHmacSha1Encrypt(resource + "+" + timeStamp, secretKey); } - header.add(signature); + header.put("Spas-Signature", signature); } return header; } - - public static List getSignHeaders(List paramValues, String secretKey) { + + public static Map getSignHeaders(Map paramValues, String secretKey) { if (null == paramValues) { return null; } - Map signMap = new HashMap(5); - for (Iterator iter = paramValues.iterator(); iter.hasNext(); ) { - String key = iter.next(); - if (TENANT_KEY.equals(key) || GROUP_KEY.equals(key)) { - signMap.put(key, iter.next()); - } else { - iter.next(); - } - } + String resource = ""; - if (signMap.size() > 1) { - resource = signMap.get(TENANT_KEY) + "+" + signMap.get(GROUP_KEY); + if (paramValues.containsKey(TENANT_KEY) && paramValues.containsKey(GROUP_KEY)) { + resource = paramValues.get(TENANT_KEY) + "+" + paramValues.get(GROUP_KEY); } else { - if (!StringUtils.isBlank(signMap.get(GROUP_KEY))) { - resource = signMap.get(GROUP_KEY); + if (!StringUtils.isBlank(paramValues.get(GROUP_KEY))) { + resource = paramValues.get(GROUP_KEY); } } return getSignHeaders(resource, secretKey); @@ -97,14 +83,14 @@ public static String getAk() { */ public static String signWithHmacSha1Encrypt(String encryptText, String encryptKey) { try { - byte[] data = encryptKey.getBytes(StandardCharsets.UTF_8); + byte[] data = encryptKey.getBytes(Constants.ENCODE); // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称 SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1"); // 生成一个指定 Mac 算法 的 Mac 对象 Mac mac = Mac.getInstance("HmacSHA1"); // 用给定密钥初始化 Mac 对象 mac.init(secretKey); - byte[] text = encryptText.getBytes(StandardCharsets.UTF_8); + byte[] text = encryptText.getBytes(Constants.ENCODE); byte[] textFinal = mac.doFinal(text); // 完成 Mac 操作, base64编码,将byte数组转换为字符串 return new String(Base64.encodeBase64(textFinal), Constants.ENCODE); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index ba85bde72a2..77a811a5692 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -109,7 +109,7 @@ public class NamingProxy implements Closeable { public NamingProxy(String namespaceId, String endpoint, String serverList, Properties properties) { - this.securityProxy = new SecurityProxy(properties); + this.securityProxy = new SecurityProxy(properties, nacosRestTemplate); this.properties = properties; this.setServerPort(DEFAULT_SERVER_PORT); this.namespaceId = namespaceId; @@ -608,7 +608,7 @@ public String callServer(String api, Map params, Map> entry : conn.getHeaderFields().entrySet()) { - this.responseHeader.addParam(entry.getKey(), entry.getValue().get(0)); - } + this.responseHeader.setOriginalResponseHeader(conn.getHeaderFields()); return this.responseHeader; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java index 35dc8348bba..277a8bbce3f 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java @@ -37,8 +37,11 @@ public class Header { private final Map header; + private final Map> originalResponseHeader; + private Header() { header = new LinkedHashMap(); + originalResponseHeader = new LinkedHashMap>(); addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_JSON); addParam(HttpHeaderConsts.ACCEPT_CHARSET, "UTF-8"); addParam(HttpHeaderConsts.ACCEPT_ENCODING, "gzip"); @@ -120,6 +123,31 @@ public void addAll(Map params) { } } + /** + * set original format response header. + * + *

Currently only corresponds to the response header of JDK. + * + * @param headers original response header + */ + public void setOriginalResponseHeader(Map> headers) { + this.originalResponseHeader.putAll(headers); + for (Map.Entry> entry : this.originalResponseHeader.entrySet()) { + addParam(entry.getKey(), entry.getValue().get(0)); + } + } + + /** + * get original format response header. + * + *

Currently only corresponds to the response header of JDK. + * + * @return Map original response header + */ + public Map> getOriginalResponseHeader() { + return this.originalResponseHeader; + } + public String getCharset() { String acceptCharset = getValue(HttpHeaderConsts.ACCEPT_CHARSET); if (acceptCharset == null) { @@ -145,6 +173,7 @@ private String analysisCharset(String contentType) { public void clear() { header.clear(); + originalResponseHeader.clear(); } @Override diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java index b572e3d851f..219a9fd5107 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.test.config; import com.alibaba.nacos.Nacos; @@ -26,7 +27,7 @@ import com.alibaba.nacos.client.config.http.HttpAgent; import com.alibaba.nacos.client.config.http.MetricsHttpAgent; import com.alibaba.nacos.client.config.http.ServerHttpAgent; -import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; +import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.ThreadUtils; import org.junit.After; @@ -41,8 +42,8 @@ import org.springframework.test.context.junit4.SpringRunner; import java.net.HttpURLConnection; -import java.util.Arrays; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicInteger; @@ -52,41 +53,49 @@ * @author xiaochun.xxc */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos", "server.port=7001"}, - webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos", + "server.port=7001"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class ConfigAPI_CITCase { - + public static final long TIME_OUT = 5000; + static ConfigService iconfig = null; + static HttpAgent agent = null; - + static final String CONFIG_CONTROLLER_PATH = "/v1/cs/configs"; + String SPECIAL_CHARACTERS = "!@#$%^&*()_+-=_|/'?."; + String dataId = "yanlin"; + String group = "yanlin"; - + @LocalServerPort private int port; - + @Before public void setUp() throws Exception { Properties properties = new Properties(); - properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1"+":"+port); + properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1" + ":" + port); properties.put(PropertyKeyConst.CONTEXT_PATH, "/nacos"); iconfig = NacosFactory.createConfigService(properties); agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); agent.start(); } - + @After public void cleanup() throws Exception { - HttpResult result = null; + HttpRestResult result = null; try { - List params = Arrays.asList("dataId", dataId, "group", group, "beta", "true"); + Map params = new HashMap<>(); + params.put("dataId", dataId); + params.put("group", group); + params.put("beta", "true"); result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - Assert.assertTrue(JacksonUtils.toObj(result.content).get("data").booleanValue()); - Assert.assertTrue(JacksonUtils.toObj(result.content).get("data").booleanValue()); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + Assert.assertTrue(JacksonUtils.toObj(result.getData()).get("data").booleanValue()); + Assert.assertTrue(JacksonUtils.toObj(result.getData()).get("data").booleanValue()); } catch (Exception e) { e.printStackTrace(); Assert.fail(); @@ -98,13 +107,13 @@ public void cleanup() throws Exception { public static void cleanClientCache() throws Exception { ConfigCleanUtils.cleanClientCache(); } - + /** * @TCDescription : nacos_正常获取数据 * @TestStep : * @ExpectResult : */ - @Test(timeout = 3*TIME_OUT) + @Test(timeout = 3 * TIME_OUT) public void nacos_getconfig_1() throws Exception { final String content = "test"; boolean result = iconfig.publishConfig(dataId, group, content); @@ -119,22 +128,22 @@ public void nacos_getconfig_1() throws Exception { System.out.println(value); Assert.assertNull(value); } - + /** - * @TCDescription : nacos_服务端无配置时,获取配置 * @throws Exception + * @TCDescription : nacos_服务端无配置时,获取配置 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_getconfig_2() throws Exception { String content = iconfig.getConfig(dataId, "nacos", TIME_OUT); Assert.assertNull(content); } - + /** - * @TCDescription : nacos_获取配置时dataId为null * @throws Exception + * @TCDescription : nacos_获取配置时dataId为null */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_getconfig_3() throws Exception { try { String content = iconfig.getConfig(null, group, TIME_OUT); @@ -144,32 +153,32 @@ public void nacos_getconfig_3() throws Exception { } Assert.fail(); } - + /** - * @TCDescription : nacos_获取配置时group为null * @throws Exception + * @TCDescription : nacos_获取配置时group为null */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_getconfig_4() throws Exception { final String dataId = "nacos_getconfig_4"; final String content = "test"; boolean result = iconfig.publishConfig(dataId, null, content); Assert.assertTrue(result); Thread.sleep(TIME_OUT); - + String value = iconfig.getConfig(dataId, null, TIME_OUT); Assert.assertEquals(content, value); - + result = iconfig.removeConfig(dataId, null); Thread.sleep(TIME_OUT); Assert.assertTrue(result); } - + /** - * @TCDescription : nacos_服务端无该配置项时,正常创建配置 * @throws Exception + * @TCDescription : nacos_服务端无该配置项时,正常创建配置 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_publishConfig_1() throws Exception { final String content = "publishConfigTest"; boolean result = iconfig.publishConfig(dataId, group, content); @@ -178,45 +187,45 @@ public void nacos_publishConfig_1() throws Exception { result = iconfig.removeConfig(dataId, group); Assert.assertTrue(result); } - + /** - * @TCDescription : nacos_服务端有该配置项时,正常修改配置 * @throws Exception + * @TCDescription : nacos_服务端有该配置项时,正常修改配置 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_publishConfig_2() throws Exception { final String content = "publishConfigTest"; boolean result = iconfig.publishConfig(dataId, group, content); Thread.sleep(TIME_OUT); Assert.assertTrue(result); - + final String content1 = "test.abc"; result = iconfig.publishConfig(dataId, group, content1); Thread.sleep(TIME_OUT); String value = iconfig.getConfig(dataId, group, TIME_OUT); Assert.assertEquals(content1, value); } - + /** - * @TCDescription : nacos_发布配置时包含特殊字符 * @throws Exception + * @TCDescription : nacos_发布配置时包含特殊字符 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_publishConfig_3() throws Exception { String content = "test" + SPECIAL_CHARACTERS; boolean result = iconfig.publishConfig(dataId, group, content); Thread.sleep(TIME_OUT); Assert.assertTrue(result); - + String value = iconfig.getConfig(dataId, group, TIME_OUT); Assert.assertEquals(content, value); } - + /** - * @TCDescription : nacos_发布配置时dataId为null * @throws Exception + * @TCDescription : nacos_发布配置时dataId为null */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_publishConfig_4() throws Exception { try { String content = "test"; @@ -229,29 +238,29 @@ public void nacos_publishConfig_4() throws Exception { } Assert.fail(); } - + /** - * @TCDescription : nacos_发布配置时group为null * @throws Exception + * @TCDescription : nacos_发布配置时group为null */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_publishConfig_5() throws Exception { final String dataId = "nacos_publishConfig_5"; String content = "test"; boolean result = iconfig.publishConfig(dataId, null, content); Thread.sleep(TIME_OUT); Assert.assertTrue(result); - + String value = iconfig.getConfig(dataId, null, TIME_OUT); Assert.assertEquals(content, value); } - - + + /** - * @TCDescription : nacos_发布配置时配置内容为null * @throws Exception + * @TCDescription : nacos_发布配置时配置内容为null */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_publishConfig_6() throws Exception { String content = null; try { @@ -263,56 +272,56 @@ public void nacos_publishConfig_6() throws Exception { } Assert.fail(); } - + /** - * @TCDescription : nacos_发布配置时配置内容包含中文字符 * @throws Exception + * @TCDescription : nacos_发布配置时配置内容包含中文字符 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_publishConfig_7() throws Exception { String content = "阿里abc"; boolean result = iconfig.publishConfig(dataId, group, content); Thread.sleep(TIME_OUT); Assert.assertTrue(result); - String value = iconfig.getConfig(dataId, group, TIME_OUT); + String value = iconfig.getConfig(dataId, group, TIME_OUT); Assert.assertEquals(content, value); } - + /** - * @TCDescription : nacos_服务端有该配置项时,正常删除配置 * @throws Exception + * @TCDescription : nacos_服务端有该配置项时,正常删除配置 */ @Test public void nacos_removeConfig_1() throws Exception { String content = "test"; boolean result = iconfig.publishConfig(dataId, group, content); - + Assert.assertTrue(result); Thread.sleep(TIME_OUT); - + result = iconfig.removeConfig(dataId, group); Assert.assertTrue(result); Thread.sleep(TIME_OUT); String value = iconfig.getConfig(dataId, group, TIME_OUT); Assert.assertNull(value); } - + /** - * @TCDescription : nacos_服务端无该配置项时,配置删除失败 * @throws Exception + * @TCDescription : nacos_服务端无该配置项时,配置删除失败 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_removeConfig_2() throws Exception { group += "removeConfig2"; boolean result = iconfig.removeConfig(dataId, group); Assert.assertTrue(result); } - + /** - * @TCDescription : nacos_删除配置时dataId为null * @throws Exception + * @TCDescription : nacos_删除配置时dataId为null */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_removeConfig_3() throws Exception { try { boolean result = iconfig.removeConfig(null, group); @@ -323,28 +332,28 @@ public void nacos_removeConfig_3() throws Exception { } Assert.fail(); } - + /** - * @TCDescription : nacos_删除配置时group为null * @throws Exception + * @TCDescription : nacos_删除配置时group为null */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_removeConfig_4() throws Exception { boolean result = iconfig.removeConfig(dataId, null); Assert.assertTrue(result); } - + /** - * @TCDescription : nacos_添加对dataId的监听,在服务端修改配置后,获取监听后的修改的配置 * @throws Exception + * @TCDescription : nacos_添加对dataId的监听,在服务端修改配置后,获取监听后的修改的配置 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_addListener_1() throws Exception { final AtomicInteger count = new AtomicInteger(0); final String content = "test-abc"; boolean result = iconfig.publishConfig(dataId, group, content); Assert.assertTrue(result); - + Listener ml = new Listener() { @Override public void receiveConfigInfo(String configInfo) { @@ -352,7 +361,7 @@ public void receiveConfigInfo(String configInfo) { count.incrementAndGet(); Assert.assertEquals(content, configInfo); } - + @Override public Executor getExecutor() { return null; @@ -365,7 +374,7 @@ public Executor getExecutor() { Assert.assertTrue(count.get() >= 1); iconfig.removeListener(dataId, group, ml); } - + /** * @TCDescription : nacos_设置监听器为null,抛出异常信息 * @TestStep : @@ -377,8 +386,8 @@ public Executor getExecutor() { public void nacos_addListener_2() throws Exception { iconfig.addListener(dataId, group, null); } - - + + /** * @TCDescription : nacos_添加对dataId的监听,修改服务端配置,正常推送并只推送一次 * @TestStep : TODO Test steps @@ -398,7 +407,7 @@ public void nacos_addListener_3() throws InterruptedException, NacosException { // Maximum assurance level notification has been performed ThreadUtils.sleep(5000); - + Listener ml = new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { @@ -416,7 +425,7 @@ public void receiveConfigInfo(String configInfo) { Assert.assertEquals(1, count.get()); iconfig.removeListener(dataId, group, ml); } - + /** * @TCDescription : nacos_服务端无配置时,添加对dataId的监听 * @TestStep : TODO Test steps @@ -424,13 +433,13 @@ public void receiveConfigInfo(String configInfo) { * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_addListener_4() throws Exception { final AtomicInteger count = new AtomicInteger(0); - + iconfig.removeConfig(dataId, group); Thread.sleep(TIME_OUT); - + Listener ml = new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { @@ -442,14 +451,14 @@ public void receiveConfigInfo(String configInfo) { String content = "test-abc"; boolean result = iconfig.publishConfig(dataId, group, content); Assert.assertTrue(result); - + while (count.get() == 0) { Thread.sleep(3000); } Assert.assertEquals(1, count.get()); iconfig.removeListener(dataId, group, ml); } - + /** * @TCDescription : nacos_在主动拉取配置后并注册Listener,在更新配置后才触发Listener监听事件(使用特定接口) * @TestStep : TODO Test steps @@ -466,9 +475,9 @@ public void nacos_addListener_5() throws InterruptedException, NacosException { final String newContent = "new-test-def"; boolean result = iconfig.publishConfig(dataId, group, content); Assert.assertTrue(result); - + Thread.sleep(2000); - + Listener ml = new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { @@ -476,20 +485,20 @@ public void receiveConfigInfo(String configInfo) { Assert.assertEquals(newContent, configInfo); } }; - + String receiveContent = iconfig.getConfigAndSignListener(dataId, group, 1000, ml); System.out.println(receiveContent); - + result = iconfig.publishConfig(dataId, group, newContent); Assert.assertTrue(result); - + Assert.assertEquals(content, receiveContent); Thread.sleep(2000); - + Assert.assertEquals(1, count.get()); iconfig.removeListener(dataId, group, ml); } - + /** * @TCDescription : nacos_在主动拉取配置后并注册Listener,在更新配置后才触发Listener监听事件(进行配置参数设置) * @TestStep : TODO Test steps @@ -499,22 +508,22 @@ public void receiveConfigInfo(String configInfo) { */ @Test public void nacos_addListener_6() throws InterruptedException, NacosException { - + Properties properties = new Properties(); - properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1"+":"+port); + properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1" + ":" + port); properties.put(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG, "true"); ConfigService iconfig = NacosFactory.createConfigService(properties); - + final AtomicInteger count = new AtomicInteger(0); final String dataId = "nacos_addListener_6"; - final String group ="nacos_addListener_6"; + final String group = "nacos_addListener_6"; final String content = "test-abc"; final String newContent = "new-test-def"; boolean result = iconfig.publishConfig(dataId, group, content); Assert.assertTrue(result); - + Thread.sleep(2000); - + Listener ml = new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { @@ -523,26 +532,26 @@ public void receiveConfigInfo(String configInfo) { Assert.assertEquals(newContent, configInfo); } }; - + iconfig.addListener(dataId, group, ml); - + String receiveContent = iconfig.getConfig(dataId, group, 1000); - + System.out.println(receiveContent); - + result = iconfig.publishConfig(dataId, group, newContent); Assert.assertTrue(result); - + Thread.sleep(2000); - + receiveContent = iconfig.getConfig(dataId, group, 1000); - + Assert.assertEquals(newContent, receiveContent); - + Assert.assertEquals(1, count.get()); iconfig.removeListener(dataId, group, ml); } - + /** * @TCDescription : nacos_正常移除监听器 * @TestStep : TODO Test steps @@ -550,7 +559,7 @@ public void receiveConfigInfo(String configInfo) { * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_removeListener_1() throws Exception { iconfig.addListener(dataId, group, new AbstractListener() { @Override @@ -567,10 +576,10 @@ public void receiveConfigInfo(String configInfo) { } }); } catch (Exception e) { - + } } - + /** * @TCDescription : nacos_移除无该项dataId的监听器 * @TestStep : TODO Test steps @@ -585,14 +594,14 @@ public void nacos_removeListener_2() { iconfig.removeListener(dataId, group, new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { - + } }); } catch (Exception e) { Assert.fail(); } } - + /** * @TCDescription : nacos_存在多个监听器时,删除最后一个监听器 * @TestStep : TODO Test steps @@ -600,11 +609,11 @@ public void receiveConfigInfo(String configInfo) { * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_removeListener_3() throws Exception { final String contentRemove = "test-abc-two"; final AtomicInteger count = new AtomicInteger(0); - + Listener ml = new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { @@ -621,20 +630,20 @@ public void receiveConfigInfo(String configInfo) { }; iconfig.addListener(dataId, group, ml); iconfig.addListener(dataId, group, ml1); - + iconfig.removeListener(dataId, group, ml); Thread.sleep(TIME_OUT); - + boolean result = iconfig.publishConfig(dataId, group, contentRemove); Thread.sleep(TIME_OUT); Assert.assertTrue(result); - + while (count.get() == 0) { Thread.sleep(3000); } Assert.assertNotEquals(0, count.get()); } - + /** * @TCDescription : nacos_监听器为null时 * @TestStep : TODO Test steps @@ -646,7 +655,7 @@ public void receiveConfigInfo(String configInfo) { public void nacos_removeListener_4() { iconfig.removeListener(dataId, group, null); } - + /** * @TCDescription : nacos_openAPI_配置具体信息 * @TestStep : @@ -654,26 +663,28 @@ public void nacos_removeListener_4() { * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 3*TIME_OUT) + @Test(timeout = 3 * TIME_OUT) public void nacos_openAPI_detailConfig_1() { - HttpResult result = null; - + HttpRestResult result = null; + try { final String content = "test"; boolean ret = iconfig.publishConfig(dataId, group, content); Thread.sleep(TIME_OUT); Assert.assertTrue(ret); - - List params = Arrays.asList("dataId", dataId, "group", group, "show", "all"); + Map params = new HashMap<>(); + params.put("dataId", dataId); + params.put("group", group); + params.put("show", "all"); result = agent.httpGet(CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - - Assert.assertEquals(content, JacksonUtils.toObj(result.content).get("content").textValue()); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + + Assert.assertEquals(content, JacksonUtils.toObj(result.getData()).get("content").textValue()); } catch (Exception e) { Assert.fail(); } } - + /** * @TCDescription : nacos_openAPI_catalog信息 * @TestStep : @@ -681,28 +692,29 @@ public void nacos_openAPI_detailConfig_1() { * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 3*TIME_OUT) + @Test(timeout = 3 * TIME_OUT) public void nacos_openAPI_catalog() { - HttpResult result = null; - + HttpRestResult result = null; + try { final String content = "test"; boolean ret = iconfig.publishConfig(dataId, group, content); Thread.sleep(TIME_OUT); Assert.assertTrue(ret); - - List params = Arrays.asList("dataId", dataId, "group", group); - result = agent.httpGet(CONFIG_CONTROLLER_PATH+"/catalog", null, params, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - - System.out.println(result.content); - Assert.assertFalse(JacksonUtils.toObj(result.content).get("data").isNull()); - + Map params = new HashMap<>(); + params.put("dataId", dataId); + params.put("group", group); + result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/catalog", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + + System.out.println(result.getData()); + Assert.assertFalse(JacksonUtils.toObj(result.getData()).get("data").isNull()); + } catch (Exception e) { Assert.fail(); } } - + /** * @TCDescription : nacos_openAPI_queryBeta信息 * @TestStep : @@ -710,177 +722,206 @@ public void nacos_openAPI_catalog() { * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 3*TIME_OUT) + @Test(timeout = 3 * TIME_OUT) public void nacos_openAPI_queryBeta_1() { - HttpResult result = null; - + HttpRestResult result = null; + try { final String content = "test-beta"; - List headers = Arrays.asList("betaIps", "127.0.0.1"); - List params1 = Arrays.asList("dataId", dataId, "group", group, "content", content); - result = agent.httpPost(CONFIG_CONTROLLER_PATH + "/", headers, params1, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - Assert.assertEquals("true", result.content); - - List params = Arrays.asList("dataId", dataId, "group", group, "beta", "true"); + Map headers = new HashMap<>(); + headers.put("betaIps", "127.0.0.1"); + Map params = new HashMap<>(); + params.put("dataId", dataId); + params.put("group", group); + params.put("content", content); + result = agent.httpPost(CONFIG_CONTROLLER_PATH + "/", headers, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + Assert.assertEquals("true", result.getData()); + params.clear(); + params.put("dataId", dataId); + params.put("group", group); + params.put("beta", "true"); result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - Assert.assertEquals(content, JacksonUtils.toObj(result.content).get("data").get("content").textValue()); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + Assert.assertEquals(content, JacksonUtils.toObj(result.getData()).get("data").get("content").textValue()); // delete data result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); } catch (Exception e) { + Assert.fail(); } } - + /** * @TCDescription : nacos_openAPI_queryBeta删除信息 - * @TestStep : 1. 发布配置 - * 2. 删除Beta配置信息 + * @TestStep : 1. 发布配置 2. 删除Beta配置信息 * @ExpectResult : * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 3*TIME_OUT) + @Test(timeout = 3 * TIME_OUT) public void nacos_openAPI_queryBeta_delete() { - HttpResult result = null; - + HttpRestResult result = null; + try { final String content = "test-beta"; - List headers = Arrays.asList("betaIps", "127.0.0.1"); - List params1 = Arrays.asList("dataId", dataId, "group", group, "content", content); - result = agent.httpPost(CONFIG_CONTROLLER_PATH + "/", headers, params1, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - Assert.assertEquals("true", result.content); - - - List params = Arrays.asList("dataId", dataId, "group", group, "beta", "true"); + Map headers = new HashMap<>(); + headers.put("betaIps", "127.0.0.1"); + Map params = new HashMap<>(); + params.put("dataId", dataId); + params.put("group", group); + params.put("content", content); + result = agent.httpPost(CONFIG_CONTROLLER_PATH + "/", headers, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + Assert.assertEquals("true", result.getData()); + + params.clear(); + params.put("dataId", dataId); + params.put("group", group); + params.put("beta", "true"); result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); - - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - Assert.assertTrue(JacksonUtils.toObj(result.content).get("data").booleanValue()); + + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + Assert.assertTrue(JacksonUtils.toObj(result.getData()).get("data").booleanValue()); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } } - + /** * @TCDescription : nacos_openAPI_模糊查询配置信息 - * @TestStep : 1. 发布配置 - * 2. 模糊查询 + * @TestStep : 1. 发布配置 2. 模糊查询 * @ExpectResult : 获取查询到配置 * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_openAPI_fuzzySearchConfig() { - HttpResult result = null; - + HttpRestResult result = null; + try { final String content = "test123"; boolean ret = iconfig.publishConfig(dataId, group, content); Thread.sleep(TIME_OUT); Assert.assertTrue(ret); - - List params = Arrays.asList("dataId", dataId, "group", group, "pageNo","1", "pageSize","10", "search", "blur"); + Map params = new HashMap<>(); + params.put("dataId", dataId); + params.put("group", group); + params.put("pageNo", "1"); + params.put("pageSize", "10"); + params.put("search", "blur"); result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - - Assert.assertTrue(JacksonUtils.toObj(result.content).get("totalCount").intValue() >= 1); - Assert.assertTrue(JacksonUtils.toObj(result.content).get("pageItems").get(0).get("content").textValue().startsWith(content)); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + + Assert.assertTrue(JacksonUtils.toObj(result.getData()).get("totalCount").intValue() >= 1); + Assert.assertTrue(JacksonUtils.toObj(result.getData()).get("pageItems").get(0).get("content").textValue() + .startsWith(content)); } catch (Exception e) { Assert.fail(); } } - + /** * @TCDescription : nacos_openAPI_模糊查询配置信息 - * @TestStep : 1. 发布配置 - * 2. 查询配置信息 + * @TestStep : 1. 发布配置 2. 查询配置信息 * @ExpectResult : 获取查询到配置 * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_openAPI_fuzzySearchConfig_1() { - HttpResult result = null; - + HttpRestResult result = null; + try { final String content = "test123"; boolean ret = iconfig.publishConfig(dataId, group, content); Thread.sleep(TIME_OUT); Assert.assertTrue(ret); - - List params = Arrays.asList("dataId", dataId+"*", "group", group+"*", "pageNo","1", "pageSize","10", "search", "blur"); + Map params = new HashMap<>(); + params.put("dataId", dataId + "*"); + params.put("group", group + "*"); + params.put("pageNo", "1"); + params.put("pageSize", "10"); + params.put("search", "blur"); result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); - - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - Assert.assertTrue(JacksonUtils.toObj(result.content).get("totalCount").intValue() >= 1); - Assert.assertEquals(content, JacksonUtils.toObj(result.content).get("pageItems").get(0).get("content").textValue()); - + + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + Assert.assertTrue(JacksonUtils.toObj(result.getData()).get("totalCount").intValue() >= 1); + Assert.assertEquals(content, + JacksonUtils.toObj(result.getData()).get("pageItems").get(0).get("content").textValue()); + } catch (Exception e) { Assert.fail(); } } - + /** * @TCDescription : nacos_openAPI_查询配置信息 - * @TestStep : 1. 发布配置 - * 2. 查询配置信息 + * @TestStep : 1. 发布配置 2. 查询配置信息 * @ExpectResult : 获取查询到配置 * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_openAPI_searchConfig() { - HttpResult result = null; - + HttpRestResult result = null; + try { final String content = "test123"; boolean ret = iconfig.publishConfig(dataId, group, content); Assert.assertTrue(ret); Thread.sleep(TIME_OUT); - - List params = Arrays.asList("dataId", dataId, "group", group, "pageNo","1", "pageSize","10", "search", "accurate"); + Map params = new HashMap<>(); + params.put("dataId", dataId); + params.put("group", group); + params.put("pageNo", "1"); + params.put("pageSize", "10"); + params.put("search", "accurate"); result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); - - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - Assert.assertEquals(1, JacksonUtils.toObj(result.content).get("totalCount").intValue()); - Assert.assertEquals(content, JacksonUtils.toObj(result.content).get("pageItems").get(0).get("content").textValue()); - + + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + Assert.assertEquals(1, JacksonUtils.toObj(result.getData()).get("totalCount").intValue()); + Assert.assertEquals(content, + JacksonUtils.toObj(result.getData()).get("pageItems").get(0).get("content").textValue()); + } catch (Exception e) { Assert.fail(); } } - + /** * @TCDescription : nacos_openAPI_查询配置信息,包含中文,utf-8 - * @TestStep : 1. 发布配置 - * 2. 查询配置信息 + * @TestStep : 1. 发布配置 2. 查询配置信息 * @ExpectResult : 获取查询到配置 * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 5*TIME_OUT) + @Test(timeout = 5 * TIME_OUT) public void nacos_openAPI_searchConfig_2() { - HttpResult result = null; - + HttpRestResult result = null; + try { final String content = "test测试"; boolean ret = iconfig.publishConfig(dataId, group, content); Assert.assertTrue(ret); Thread.sleep(TIME_OUT); - - List params = Arrays.asList("dataId", dataId, "group", group, "pageNo","1", "pageSize","10", "search", "accurate"); - result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, "utf-8", TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - Assert.assertEquals(1, JacksonUtils.toObj(result.content).get("totalCount").intValue()); - Assert.assertEquals(content, JacksonUtils.toObj(result.content).get("pageItems").get(0).get("content").textValue()); + + Map params = new HashMap<>(); + params.put("dataId", dataId); + params.put("group", group); + params.put("pageNo", "1"); + params.put("pageSize", "10"); + params.put("search", "accurate"); + result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + Assert.assertEquals(1, JacksonUtils.toObj(result.getData()).get("totalCount").intValue()); + Assert.assertEquals(content, + JacksonUtils.toObj(result.getData()).get("pageItems").get(0).get("content").textValue()); } catch (Exception e) { Assert.fail(); } } - + } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_CITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_CITCase.java index cd55ccacaf4..10ee4ddd7c6 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_CITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_CITCase.java @@ -20,7 +20,7 @@ import com.alibaba.nacos.client.config.http.HttpAgent; import com.alibaba.nacos.client.config.http.MetricsHttpAgent; import com.alibaba.nacos.client.config.http.ServerHttpAgent; -import com.alibaba.nacos.client.config.impl.HttpSimpleClient; +import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.config.server.utils.ZipUtils; import com.fasterxml.jackson.databind.JsonNode; @@ -96,31 +96,44 @@ public void setUp() throws Exception { @After public void cleanup() throws Exception{ - HttpSimpleClient.HttpResult result; + HttpRestResult result; try { - List params2 = Arrays.asList("dataId", "testNoAppname1.yml", "group", "EXPORT_IMPORT_TEST_GROUP", "beta", "false"); - result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params2, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + Map params = new HashMap<>(); + params.put("dataId", "testNoAppname1.yml"); + params.put("group", "EXPORT_IMPORT_TEST_GROUP"); + params.put("beta", "false"); + result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); - List params3 = Arrays.asList("dataId", "testNoAppname2.txt", "group", "TEST1_GROUP", "beta", "false"); - result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params3, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); - - List params4 = Arrays.asList("dataId", "testHasAppname1.properties", "group", "EXPORT_IMPORT_TEST_GROUP", "beta", "false"); - result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params4, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + params.put("dataId", "testNoAppname2.txt"); + params.put("group", "TEST1_GROUP"); + params.put("beta", "false"); + result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); + + params.put("dataId", "testHasAppname1.properties"); + params.put("group", "EXPORT_IMPORT_TEST_GROUP"); + params.put("beta", "false"); + result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); - List params5 = Arrays.asList("dataId", "test1.yml", "group", "TEST_IMPORT", "beta", "false"); - result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params5, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + params.put("dataId", "test1.yml"); + params.put("group", "TEST_IMPORT"); + params.put("beta", "false"); + result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); - List params6 = Arrays.asList("dataId", "test2.txt", "group", "TEST_IMPORT", "beta", "false"); - result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params6, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + params.put("dataId", "test2.txt"); + params.put("group", "TEST_IMPORT"); + params.put("beta", "false"); + result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); - List params7 = Arrays.asList("dataId", "test3.properties", "group", "TEST_IMPORT", "beta", "false"); - result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params7, agent.getEncode(), TIME_OUT); - Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + params.put("dataId", "test3.properties"); + params.put("group", "TEST_IMPORT"); + params.put("beta", "false"); + result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.getCode()); } catch (Exception e) { Assert.fail(); }