From 8c23f34806211442e804710db9b062ada3bc0ed3 Mon Sep 17 00:00:00 2001 From: nkorange Date: Mon, 4 Mar 2019 17:12:29 +0800 Subject: [PATCH] #269 fix bug --- .../alibaba/nacos/api/common/Constants.java | 2 + .../nacos/api/naming/NamingService.java | 37 +++++-- .../nacos/api/naming/pojo/ServiceInfo.java | 30 ++++-- .../client/naming/NacosNamingService.java | 102 ++++++++++-------- .../nacos/client/naming/cache/DiskCache.java | 6 +- .../nacos/client/naming/net/NamingProxy.java | 7 +- naming/pom.xml | 6 +- .../alibaba/nacos/naming/acl/AuthChecker.java | 3 +- .../persistent/raft/RaftStore.java | 9 +- .../naming/controllers/CatalogController.java | 11 +- .../naming/controllers/ClusterController.java | 3 +- .../naming/controllers/HealthController.java | 3 +- .../controllers/InstanceController.java | 17 +-- .../controllers/OperatorController.java | 5 +- .../naming/controllers/ServiceController.java | 27 +++-- .../nacos/naming/core/ServiceManager.java | 11 +- .../nacos/naming/misc/SwitchManager.java | 5 +- .../nacos/naming/misc/UtilsAndCommons.java | 17 ++- .../nacos/naming/web/DistroFilter.java | 7 +- .../alibaba/nacos/naming/web/FilterBase.java | 3 +- .../nacos/naming/web/TrafficReviseFilter.java | 2 +- .../src/main/resources/application.properties | 6 ++ .../controllers/InstanceControllerTest.java | 5 +- .../nacos/naming/core/DomainsManagerTest.java | 7 +- .../naming/AutoDeregisterInstance_ITCase.java | 18 +++- .../naming/DeregisterInstance_ITCase.java | 15 ++- .../nacos/test/naming/MultiTenant_ITCase.java | 12 +++ .../MultiTenant_InstanceAPI_ITCase.java | 15 ++- .../alibaba/nacos/test/naming/NamingBase.java | 12 ++- .../test/naming/RegisterInstance_ITCase.java | 11 ++ .../nacos/test/naming/RestAPI_ITCase.java | 2 +- .../test/naming/SelectInstances_ITCase.java | 8 ++ .../SelectOneHealthyInstance_ITCase.java | 8 ++ .../nacos/test/naming/ServiceListTest.java | 8 ++ .../test/naming/SubscribeCluster_ITCase.java | 8 ++ .../nacos/test/naming/Subscribe_ITCase.java | 8 ++ .../nacos/test/naming/Unsubscribe_ITCase.java | 10 +- 37 files changed, 328 insertions(+), 138 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java index 17bde9282..13c1716a0 100644 --- a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java +++ b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java @@ -124,4 +124,6 @@ public class Constants { public static final String DEFAULT_NAMESPACE_ID = "public"; public static final int WRITE_REDIRECT_CODE = 307; + + public static final String SERVICE_INFO_SPLITER = "@@"; } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java index 43d68c9a0..8a42df091 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java @@ -418,7 +418,7 @@ public interface NamingService { void subscribe(String serviceName, String groupName, EventListener listener) throws NacosException; /** - * subscribe service to receive events of instances alteration + * Subscribe service to receive events of instances alteration * * @param serviceName name of service * @param clusters list of cluster @@ -428,7 +428,7 @@ public interface NamingService { void subscribe(String serviceName, List clusters, EventListener listener) throws NacosException; /** - * subscribe service to receive events of instances alteration + * Subscribe service to receive events of instances alteration * * @param serviceName name of service * @param groupName group of service @@ -439,7 +439,7 @@ public interface NamingService { void subscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException; /** - * unsubscribe event listener of service + * Unsubscribe event listener of service * * @param serviceName name of service * @param listener event listener @@ -458,7 +458,7 @@ public interface NamingService { void unsubscribe(String serviceName, String groupName, EventListener listener) throws NacosException; /** - * unsubscribe event listener of service + * Unsubscribe event listener of service * * @param serviceName name of service * @param clusters list of cluster @@ -468,7 +468,7 @@ public interface NamingService { void unsubscribe(String serviceName, List clusters, EventListener listener) throws NacosException; /** - * unsubscribe event listener of service + * Unsubscribe event listener of service * * @param serviceName name of service * @param groupName group of service @@ -479,7 +479,7 @@ public interface NamingService { void unsubscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException; /** - * get all service names from server + * Get all service names from server * * @param pageNo page index * @param pageSize page size @@ -489,7 +489,18 @@ public interface NamingService { ListView getServicesOfServer(int pageNo, int pageSize) throws NacosException; /** - * Get all subscribed services of current client + * Get all service names from server + * + * @param pageNo page index + * @param pageSize page size + * @param groupName group name + * @return list of service names + * @throws NacosException + */ + ListView getServicesOfServer(int pageNo, int pageSize, String groupName) throws NacosException; + + /** + * Get all service names from server with selector * * @param pageNo page index * @param pageSize page size @@ -500,6 +511,18 @@ public interface NamingService { */ ListView getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException; + /** + * Get all service names from server with selector + * + * @param pageNo page index + * @param pageSize page size + * @param groupName group name + * @param selector selector to filter the resource + * @return list of service names + * @throws NacosException + */ + ListView getServicesOfServer(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException; + /** * Get all subscribed services of current client * diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java index 8e359d9b7..46ad0c227 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.api.naming.pojo; import com.alibaba.fastjson.annotation.JSONField; +import com.alibaba.nacos.api.common.Constants; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -37,6 +38,8 @@ public class ServiceInfo { @JSONField(name = "dom") private String name; + private String groupName; + private String clusters; private long cacheMillis = 1000L; @@ -67,7 +70,7 @@ public class ServiceInfo { int clusterIndex = 1; int serviceNameIndex = 0; - String[] keys = key.split(SPLITER); + String[] keys = key.split(Constants.SERVICE_INFO_SPLITER); if (keys.length >= maxIndex) { this.name = keys[serviceNameIndex]; this.clusters = keys[clusterIndex]; @@ -105,6 +108,14 @@ public class ServiceInfo { this.name = name; } + public String getGroupName() { + return groupName; + } + + public void setGroupName(String groupName) { + this.groupName = groupName; + } + public void setLastRefTime(long lastRefTime) { this.lastRefTime = lastRefTime; } @@ -178,13 +189,16 @@ public class ServiceInfo { @JSONField(serialize = false) public static ServiceInfo fromKey(String key) { ServiceInfo serviceInfo = new ServiceInfo(); - - if (key.contains(SPLITER)) { - serviceInfo.setName(key.split(SPLITER)[0]); - serviceInfo.setClusters(key.split(SPLITER)[1]); - return serviceInfo; + int maxSegCount = 3; + String[] segs = key.split(Constants.SERVICE_INFO_SPLITER); + if (segs.length == maxSegCount -1) { + serviceInfo.setGroupName(segs[0]); + serviceInfo.setName(segs[1]); + } else if (segs.length == maxSegCount) { + serviceInfo.setGroupName(segs[0]); + serviceInfo.setName(segs[1]); + serviceInfo.setClusters(segs[2]); } - serviceInfo.setName(key); return serviceInfo; } @@ -192,7 +206,7 @@ public class ServiceInfo { public static String getKey(String name, String clusters) { if (!isEmpty(clusters)) { - return name + SPLITER + clusters; + return name + Constants.SERVICE_INFO_SPLITER + clusters; } return name; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 1212ca819..97b7f1ab1 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -241,12 +241,17 @@ public class NacosNamingService implements NamingService { @Override public List getAllInstances(String serviceName, List clusters, boolean subscribe) throws NacosException { + return getAllInstances(serviceName, Constants.DEFAULT_GROUP, clusters, subscribe); + } + + @Override + public List getAllInstances(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { ServiceInfo serviceInfo; if (subscribe) { - serviceInfo = hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ",")); + serviceInfo = hostReactor.getServiceInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ",")); } else { - serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(serviceName, StringUtils.join(clusters, ",")); + serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ",")); } List list; if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { @@ -255,11 +260,6 @@ public class NacosNamingService implements NamingService { return list; } - @Override - public List getAllInstances(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { - return null; - } - @Override public List selectInstances(String serviceName, boolean healthy) throws NacosException { return selectInstances(serviceName, new ArrayList(), healthy); @@ -267,7 +267,7 @@ public class NacosNamingService implements NamingService { @Override public List selectInstances(String serviceName, String groupName, boolean healthy) throws NacosException { - return null; + return selectInstances(serviceName, groupName, healthy, true); } @Override @@ -278,7 +278,7 @@ public class NacosNamingService implements NamingService { @Override public List selectInstances(String serviceName, String groupName, boolean healthy, boolean subscribe) throws NacosException { - return null; + return selectInstances(serviceName, groupName, new ArrayList(), healthy, subscribe); } @Override @@ -289,24 +289,25 @@ public class NacosNamingService implements NamingService { @Override public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy) throws NacosException { - return null; + return selectInstances(serviceName, groupName, clusters, healthy, true); } @Override public List selectInstances(String serviceName, List clusters, boolean healthy, boolean subscribe) throws NacosException { - ServiceInfo serviceInfo; - if (subscribe) { - serviceInfo = hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ",")); - } else { - serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(serviceName, StringUtils.join(clusters, ",")); - } - return selectInstances(serviceInfo, healthy); + return selectInstances(serviceName, Constants.DEFAULT_GROUP, clusters, healthy, subscribe); } @Override public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy, boolean subscribe) throws NacosException { - return null; + + ServiceInfo serviceInfo; + if (subscribe) { + serviceInfo = hostReactor.getServiceInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ",")); + } else { + serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ",")); + } + return selectInstances(serviceInfo, healthy); } @Override @@ -316,7 +317,7 @@ public class NacosNamingService implements NamingService { @Override public Instance selectOneHealthyInstance(String serviceName, String groupName) throws NacosException { - return null; + return selectOneHealthyInstance(serviceName, groupName, true); } @Override @@ -326,7 +327,7 @@ public class NacosNamingService implements NamingService { @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, boolean subscribe) throws NacosException { - return null; + return selectOneHealthyInstance(serviceName, groupName, new ArrayList(), subscribe); } @Override @@ -336,78 +337,87 @@ public class NacosNamingService implements NamingService { @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters) throws NacosException { - return null; + return selectOneHealthyInstance(serviceName, groupName, clusters, true); } @Override public Instance selectOneHealthyInstance(String serviceName, List clusters, boolean subscribe) throws NacosException { - - if (subscribe) { - return Balancer.RandomByWeight.selectHost( - hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ","))); - } else { - return Balancer.RandomByWeight.selectHost( - hostReactor.getServiceInfoDirectlyFromServer(serviceName, StringUtils.join(clusters, ","))); - } + return selectOneHealthyInstance(serviceName, Constants.DEFAULT_GROUP, clusters, subscribe); } @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { - return null; + + if (subscribe) { + return Balancer.RandomByWeight.selectHost( + hostReactor.getServiceInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","))); + } else { + return Balancer.RandomByWeight.selectHost( + hostReactor.getServiceInfoDirectlyFromServer(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","))); + } } @Override - public void subscribe(String service, EventListener listener) { - eventDispatcher.addListener(hostReactor.getServiceInfo(service, StringUtils.EMPTY), StringUtils.EMPTY, - listener); + public void subscribe(String serviceName, EventListener listener) throws NacosException { + subscribe(serviceName, new ArrayList(), listener); } @Override public void subscribe(String serviceName, String groupName, EventListener listener) throws NacosException { - + subscribe(serviceName, groupName, new ArrayList(), listener); } @Override - public void subscribe(String service, List clusters, EventListener listener) { - eventDispatcher.addListener(hostReactor.getServiceInfo(service, StringUtils.join(clusters, ",")), - StringUtils.join(clusters, ","), listener); + public void subscribe(String serviceName, List clusters, EventListener listener) throws NacosException { + subscribe(serviceName, Constants.DEFAULT_GROUP, clusters, listener); } @Override public void subscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException { - + eventDispatcher.addListener(hostReactor.getServiceInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, + StringUtils.join(clusters, ",")), StringUtils.join(clusters, ","), listener); } @Override - public void unsubscribe(String service, EventListener listener) { - eventDispatcher.removeListener(service, StringUtils.EMPTY, listener); + public void unsubscribe(String serviceName, EventListener listener) throws NacosException { + unsubscribe(serviceName, new ArrayList(), listener); } @Override public void unsubscribe(String serviceName, String groupName, EventListener listener) throws NacosException { - + unsubscribe(serviceName, groupName, new ArrayList(), listener); } @Override - public void unsubscribe(String service, List clusters, EventListener listener) { - eventDispatcher.removeListener(service, StringUtils.join(clusters, ","), listener); + public void unsubscribe(String serviceName, List clusters, EventListener listener) throws NacosException { + unsubscribe(serviceName, Constants.DEFAULT_GROUP, clusters, listener); } @Override public void unsubscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException { - + eventDispatcher.removeListener(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","), listener); } @Override public ListView getServicesOfServer(int pageNo, int pageSize) throws NacosException { - return serverProxy.getServiceList(pageNo, pageSize); + return serverProxy.getServiceList(pageNo, pageSize, Constants.DEFAULT_GROUP); + } + + @Override + public ListView getServicesOfServer(int pageNo, int pageSize, String groupName) throws NacosException { + return getServicesOfServer(pageNo, pageSize, groupName, null); } @Override public ListView getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException { - return serverProxy.getServiceList(pageNo, pageSize, selector); + return getServicesOfServer(pageNo, pageSize, Constants.DEFAULT_GROUP, selector); + } + + @Override + public ListView getServicesOfServer(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException { + return serverProxy.getServiceList(pageNo, pageSize, groupName, selector); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/DiskCache.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/DiskCache.java index 217dde641..b183a790b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/DiskCache.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/DiskCache.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.client.naming.cache; import com.alibaba.fastjson.JSON; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.client.naming.utils.CollectionUtils; @@ -44,7 +45,6 @@ public class DiskCache { makeSureCacheDirExists(dir); - File file = new File(dir, dom.getKeyEncoded()); if (!file.exists()) { // add another !file.exists() to avoid conflicted creating-new-file from multi-instances @@ -93,8 +93,8 @@ public class DiskCache { String fileName = URLDecoder.decode(file.getName(), "UTF-8"); - if (!(fileName.endsWith(ServiceInfo.SPLITER + "meta") || fileName.endsWith( - ServiceInfo.SPLITER + "special-url"))) { + if (!(fileName.endsWith(Constants.SERVICE_INFO_SPLITER + "meta") || fileName.endsWith( + Constants.SERVICE_INFO_SPLITER + "special-url"))) { ServiceInfo dom = new ServiceInfo(fileName); List ips = new ArrayList(); dom.setHosts(ips); 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 dacf29b94..b4233727f 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 @@ -251,16 +251,17 @@ public class NamingProxy { } } - public ListView getServiceList(int pageNo, int pageSize) throws NacosException { - return getServiceList(pageNo, pageSize, null); + public ListView getServiceList(int pageNo, int pageSize, String groupName) throws NacosException { + return getServiceList(pageNo, pageSize, groupName, null); } - public ListView getServiceList(int pageNo, int pageSize, AbstractSelector selector) throws NacosException { + public ListView getServiceList(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException { Map params = new HashMap(4); params.put("pageNo", String.valueOf(pageNo)); params.put("pageSize", String.valueOf(pageSize)); params.put(CommonParams.NAMESPACE_ID, namespaceId); + params.put(CommonParams.GROUP_NAME, groupName); if (selector != null) { switch (SelectorType.valueOf(selector.getType())) { diff --git a/naming/pom.xml b/naming/pom.xml index 374ce2ed4..0f55ba330 100644 --- a/naming/pom.xml +++ b/naming/pom.xml @@ -204,9 +204,9 @@ src/main/resources - - application.properties - + + + diff --git a/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java b/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java index e5ff798f7..be2545c65 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java @@ -15,6 +15,7 @@ */ package com.alibaba.nacos.naming.acl; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.core.Service; @@ -67,7 +68,7 @@ public class AuthChecker { public void doAuth(Map params, HttpServletRequest req) throws Exception { String namespaceId = WebUtils.optional(req, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.optional(req, "name", ""); if (StringUtils.isEmpty(serviceName)) { serviceName = WebUtils.optional(req, "serviceName", ""); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java index 2a868ce41..2cc4b5f28 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.naming.consistency.persistent.raft; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.naming.consistency.ApplyAction; import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.KeyBuilder; @@ -145,11 +146,11 @@ public class RaftStore { } if (StringUtils.isBlank(serviceDatum.value.getGroupName())) { - serviceDatum.value.setGroupName(UtilsAndCommons.DEFAULT_GROUP_NAME); + serviceDatum.value.setGroupName(Constants.DEFAULT_GROUP); } - if (!serviceDatum.value.getName().contains(UtilsAndCommons.GROUP_SERVICE_CONNECTOR)) { - serviceDatum.value.setName(UtilsAndCommons.DEFAULT_GROUP_NAME - + UtilsAndCommons.GROUP_SERVICE_CONNECTOR + serviceDatum.value.getName()); + if (!serviceDatum.value.getName().contains(Constants.SERVICE_INFO_SPLITER)) { + serviceDatum.value.setName(Constants.DEFAULT_GROUP + + Constants.SERVICE_INFO_SPLITER + serviceDatum.value.getName()); } return serviceDatum; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java index 91c9f2ad9..6da4a2470 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.naming.controllers; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.pojo.Cluster; import com.alibaba.nacos.core.utils.WebUtils; @@ -52,7 +53,7 @@ public class CatalogController { public JSONObject serviceList(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); JSONObject result = new JSONObject(); int page = Integer.parseInt(WebUtils.required(request, "startPg")); @@ -99,7 +100,7 @@ public class CatalogController { public ServiceDetailView serviceDetail(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); Service service = serviceManager.getService(namespaceId, serviceName); if (service == null) { @@ -133,7 +134,7 @@ public class CatalogController { public JSONObject instanceList(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String clusterName = WebUtils.required(request, CommonParams.CLUSTER_NAME); int page = Integer.parseInt(WebUtils.required(request, "startPg")); @@ -176,7 +177,7 @@ public class CatalogController { public List listDetail(HttpServletRequest request) { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); List serviceDetailInfoList = new ArrayList<>(); serviceManager @@ -203,7 +204,7 @@ public class CatalogController { public JSONObject rt4Service(HttpServletRequest request) { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); Service service = serviceManager.getService(namespaceId, serviceName); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java index b8e229a2f..c051a8a77 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.naming.controllers; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker; import com.alibaba.nacos.core.utils.WebUtils; @@ -50,7 +51,7 @@ public class ClusterController { public String update(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String clusterName = WebUtils.required(request, CommonParams.CLUSTER_NAME); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String healthChecker = WebUtils.required(request, "healthChecker"); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java index 045298a05..461ec1fcd 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.naming.controllers; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.boot.RunningConfig; @@ -71,7 +72,7 @@ public class HealthController { public String update(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String ip = WebUtils.required(request, "ip"); int port = Integer.parseInt(WebUtils.required(request, "port")); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index 91664f912..45bbdf9d7 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.naming.controllers; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.boot.RunningConfig; @@ -95,7 +96,7 @@ public class InstanceController { public String register(HttpServletRequest request) throws Exception { String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); - String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, UtilsAndCommons.DEFAULT_NAMESPACE_ID); + String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); serviceManager.registerInstance(namespaceId, serviceName, parseInstance(request)); return "ok"; @@ -106,7 +107,7 @@ public class InstanceController { public String deregister(HttpServletRequest request) throws Exception { Instance instance = getIPAddress(request); String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); Service service = serviceManager.getService(namespaceId, serviceName); @@ -119,10 +120,10 @@ public class InstanceController { return "ok"; } - @RequestMapping(value = {"/instance/update", "instance"}, method = RequestMethod.PUT) + @RequestMapping(value = "/instance", method = RequestMethod.PUT) public String update(HttpServletRequest request) throws Exception { String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); - String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, UtilsAndCommons.DEFAULT_NAMESPACE_ID); + String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); serviceManager.updateInstance(namespaceId, serviceName, parseInstance(request)); return "ok"; @@ -132,7 +133,7 @@ public class InstanceController { public JSONObject list(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String agent = request.getHeader("Client-Version"); @@ -158,7 +159,7 @@ public class InstanceController { public JSONObject detail(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String cluster = WebUtils.optional(request, CommonParams.CLUSTER_NAME, UtilsAndCommons.DEFAULT_CLUSTER_NAME); String ip = WebUtils.required(request, "ip"); @@ -209,7 +210,7 @@ public class InstanceController { } String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String beat = WebUtils.required(request, "beat"); RsInfo clientBeat = JSON.parseObject(beat, RsInfo.class); if (StringUtils.isBlank(clientBeat.getCluster())) { @@ -291,7 +292,7 @@ public class InstanceController { namespaceId = key.split(UtilsAndCommons.NAMESPACE_SERVICE_CONNECTOR)[0]; serviceName = key.split(UtilsAndCommons.NAMESPACE_SERVICE_CONNECTOR)[1]; } else { - namespaceId = UtilsAndCommons.DEFAULT_NAMESPACE_ID; + namespaceId = Constants.DEFAULT_NAMESPACE_ID; serviceName = key; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java index 8bb154589..b78d410af 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.naming.controllers; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.core.utils.SystemUtils; import com.alibaba.nacos.core.utils.WebUtils; @@ -154,7 +155,7 @@ public class OperatorController { @RequestMapping("/getResponsibleServer4Dom") public JSONObject getResponsibleServer4Dom(HttpServletRequest request) { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String dom = WebUtils.required(request, "dom"); Service service = serviceManager.getService(namespaceId, dom); @@ -181,7 +182,7 @@ public class OperatorController { @RequestMapping("/responsible") public JSONObject responsible(HttpServletRequest request) { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String dom = WebUtils.required(request, "dom"); Service service = serviceManager.getService(namespaceId, dom); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java index 9b73ef31a..668c8dd54 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ServiceController.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.naming.controllers; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.core.utils.WebUtils; @@ -61,7 +62,7 @@ public class ServiceController { public String create(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); @@ -99,7 +100,7 @@ public class ServiceController { public String remove(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); Service service = serviceManager.getService(namespaceId, serviceName); @@ -120,7 +121,7 @@ public class ServiceController { public JSONObject detail(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); Service service = serviceManager.getService(namespaceId, serviceName); @@ -156,7 +157,8 @@ public class ServiceController { int pageNo = NumberUtils.toInt(WebUtils.required(request, "pageNo")); int pageSize = NumberUtils.toInt(WebUtils.required(request, "pageSize")); String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); + String groupName = WebUtils.optional(request, CommonParams.GROUP_NAME, Constants.DEFAULT_GROUP); String selectorString = WebUtils.optional(request, "selector", StringUtils.EMPTY); List serviceNameList = serviceManager.getAllServiceNameList(namespaceId); @@ -169,6 +171,15 @@ public class ServiceController { return result; } + Iterator iterator = serviceNameList.iterator(); + + while (iterator.hasNext()) { + String serviceName = iterator.next(); + if (!serviceName.startsWith(groupName + Constants.SERVICE_INFO_SPLITER)) { + iterator.remove(); + } + } + if (StringUtils.isNotBlank(selectorString)) { JSONObject selectorJson = JSON.parseObject(selectorString); @@ -211,6 +222,10 @@ public class ServiceController { end = serviceNameList.size(); } + for (int i = start; i < end; i++) { + serviceNameList.add(i, serviceNameList.get(i).replace(groupName + Constants.SERVICE_INFO_SPLITER, "")); + } + result.put("doms", serviceNameList.subList(start, end)); result.put("count", serviceNameList.size()); @@ -222,7 +237,7 @@ public class ServiceController { public String update(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); float protectThreshold = NumberUtils.toFloat(WebUtils.required(request, "protectThreshold")); String metadata = WebUtils.optional(request, "metadata", StringUtils.EMPTY); @@ -331,7 +346,7 @@ public class ServiceController { public JSONObject checksum(HttpServletRequest request) { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - UtilsAndCommons.DEFAULT_NAMESPACE_ID); + Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); Service service = serviceManager.getService(namespaceId, serviceName); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index f2019d960..f9f3d4a8f 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.naming.core; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.naming.cluster.ServerListManager; import com.alibaba.nacos.naming.cluster.ServerMode; @@ -112,12 +113,12 @@ public class ServiceManager implements RecordListener { @Override public boolean interests(String key) { - return KeyBuilder.matchServiceMetaKey(key); + return KeyBuilder.matchServiceMetaKey(key) && !KeyBuilder.matchSwitchKey(key); } @Override public boolean matchUnlistenKey(String key) { - return KeyBuilder.matchServiceMetaKey(key); + return KeyBuilder.matchServiceMetaKey(key) && !KeyBuilder.matchSwitchKey(key); } @Override @@ -129,7 +130,7 @@ public class ServiceManager implements RecordListener { } if (StringUtils.isBlank(service.getNamespaceId())) { - service.setNamespaceId(UtilsAndCommons.DEFAULT_NAMESPACE_ID); + service.setNamespaceId(Constants.DEFAULT_NAMESPACE_ID); } Loggers.RAFT.info("[RAFT-NOTIFIER] datum is changed, key: {}, value: {}", key, service); @@ -332,7 +333,7 @@ public class ServiceManager implements RecordListener { service = new Service(); service.setName(serviceName); service.setNamespaceId(namespaceId); - service.setGroupName(UtilsAndCommons.DEFAULT_GROUP_NAME); + service.setGroupName(Constants.DEFAULT_GROUP); // now validate the service. if failed, exception will be thrown service.setLastModifiedMillis(System.currentTimeMillis()); service.recalculateChecksum(); @@ -600,7 +601,7 @@ public class ServiceManager implements RecordListener { public Map serviceName2Checksum = new HashMap(); public ServiceChecksum() { - this.namespaceId = UtilsAndCommons.DEFAULT_NAMESPACE_ID; + this.namespaceId = Constants.DEFAULT_NAMESPACE_ID; } public ServiceChecksum(String namespaceId) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchManager.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchManager.java index f9fff3a16..8c12f5665 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchManager.java @@ -19,6 +19,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.naming.cluster.ServerMode; import com.alibaba.nacos.naming.consistency.ConsistencyService; +import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.RecordListener; import com.alibaba.nacos.naming.consistency.Datum; import org.apache.commons.lang3.StringUtils; @@ -400,12 +401,12 @@ public class SwitchManager implements RecordListener { @Override public boolean interests(String key) { - return key.contains(UtilsAndCommons.SWITCH_DOMAIN_NAME); + return KeyBuilder.matchSwitchKey(key); } @Override public boolean matchUnlistenKey(String key) { - return key.contains(UtilsAndCommons.SWITCH_DOMAIN_NAME); + return KeyBuilder.matchSwitchKey(key); } @Override diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java index dc948bf5f..050a9b9ab 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java @@ -20,6 +20,7 @@ import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.parser.ParserConfig; import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SerializerFeature; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker; import com.alibaba.nacos.naming.exception.NacosException; import com.alibaba.nacos.naming.healthcheck.JsonAdapter; @@ -111,16 +112,10 @@ public class UtilsAndCommons { public static final String NAMESPACE_SERVICE_CONNECTOR = "##"; - public static final String GROUP_SERVICE_CONNECTOR = "@@"; - public static final String UPDATE_INSTANCE_ACTION_ADD = "add"; public static final String UPDATE_INSTANCE_ACTION_REMOVE = "remove"; - public static final String DEFAULT_NAMESPACE_ID = "public"; - - public static final String DEFAULT_GROUP_NAME = "DEFAULT_GROUP"; - public static final String DATA_BASE_DIR = NACOS_HOME + File.separator + "data" + File.separator + "naming"; public static final ScheduledExecutorService DOMAIN_SYNCHRONIZATION_EXECUTOR; @@ -245,16 +240,16 @@ public class UtilsAndCommons { } public static String getServiceName(String serviceNameWithGroup) { - if (!serviceNameWithGroup.contains(GROUP_SERVICE_CONNECTOR)) { + if (!serviceNameWithGroup.contains(Constants.SERVICE_INFO_SPLITER)) { return serviceNameWithGroup; } - return serviceNameWithGroup.split(GROUP_SERVICE_CONNECTOR)[1]; + return serviceNameWithGroup.split(Constants.SERVICE_INFO_SPLITER)[1]; } public static String getGroupName(String serviceNameWithGroup) { - if (!serviceNameWithGroup.contains(GROUP_SERVICE_CONNECTOR)) { - return DEFAULT_GROUP_NAME; + if (!serviceNameWithGroup.contains(Constants.SERVICE_INFO_SPLITER)) { + return Constants.DEFAULT_GROUP; } - return serviceNameWithGroup.split(GROUP_SERVICE_CONNECTOR)[0]; + return serviceNameWithGroup.split(Constants.SERVICE_INFO_SPLITER)[0]; } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java index 68e2d8c79..7219a0399 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java @@ -15,6 +15,7 @@ */ package com.alibaba.nacos.naming.web; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.misc.HttpClient; @@ -112,11 +113,13 @@ public class DistroFilter implements Filter { // user groupName@@serviceName as new service name: String groupName = req.getParameter(CommonParams.GROUP_NAME); if (StringUtils.isBlank(groupName)) { - groupName = UtilsAndCommons.DEFAULT_GROUP_NAME; + groupName = Constants.DEFAULT_GROUP; } OverrideParameterRequestWrapper requestWrapper = OverrideParameterRequestWrapper.buildRequest(req); - requestWrapper.addParameter(CommonParams.SERVICE_NAME, groupName + UtilsAndCommons.GROUP_SERVICE_CONNECTOR + serviceName); + if (StringUtils.isNotBlank(serviceName) && !serviceName.contains(Constants.SERVICE_INFO_SPLITER)) { + requestWrapper.addParameter(CommonParams.SERVICE_NAME, groupName + Constants.SERVICE_INFO_SPLITER + serviceName); + } filterChain.doFilter(requestWrapper, resp); } catch (AccessControlException e) { resp.sendError(HttpServletResponse.SC_FORBIDDEN, "access denied: " + UtilsAndCommons.getAllExceptionMsg(e)); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java b/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java index 84feb7bcb..1875c810c 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java @@ -15,6 +15,7 @@ */ package com.alibaba.nacos.naming.web; +import com.alibaba.nacos.naming.boot.RunningConfig; import com.alibaba.nacos.naming.controllers.*; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; @@ -50,7 +51,7 @@ public class FilterBase { } public Method getMethod(String httpMethod, String path) { - String key = httpMethod + "-->" + path; + String key = httpMethod + "-->" + path.replace("/nacos", ""); return methodCache.get(key); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java index 59decd11c..aa9f53891 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java @@ -98,7 +98,7 @@ public class TrafficReviseFilter implements Filter { return; } - resp.getWriter().write("service is " + serverStatusManager.getServerStatus().name() + " now, please try again later!"); + resp.getWriter().write("server is " + serverStatusManager.getServerStatus().name() + " now, please try again later!"); resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); } } diff --git a/naming/src/main/resources/application.properties b/naming/src/main/resources/application.properties index 56b1f3c57..06e1eb7d0 100644 --- a/naming/src/main/resources/application.properties +++ b/naming/src/main/resources/application.properties @@ -25,3 +25,9 @@ server.tomcat.accesslog.enabled=true server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D # default current work dir server.tomcat.basedir= + +nacos.naming.partition.taskDispatchThreadCount=10 +nacos.naming.partition.taskDispatchPeriod=200 +nacos.naming.partition.batchSyncKeyCount=1000 +nacos.naming.partition.initDataRatio=0.9 +nacos.naming.partition.syncRetryDelay=5000 diff --git a/naming/src/test/java/com/alibaba/nacos/naming/controllers/InstanceControllerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/controllers/InstanceControllerTest.java index d2c203bf9..0d84979a2 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/controllers/InstanceControllerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/controllers/InstanceControllerTest.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.naming.controllers; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.naming.BaseTest; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet; import com.alibaba.nacos.naming.core.Cluster; @@ -84,7 +85,7 @@ public class InstanceControllerTest extends BaseTest { ipList.add(instance); service.updateIPs(ipList, false); - Mockito.when(serviceManager.getService(UtilsAndCommons.DEFAULT_NAMESPACE_ID, "nacos.test.1")).thenReturn(service); + Mockito.when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, "nacos.test.1")).thenReturn(service); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put("/naming/instance") @@ -129,7 +130,7 @@ public class InstanceControllerTest extends BaseTest { ipList.add(instance); service.updateIPs(ipList, false); - Mockito.when(serviceManager.getService(UtilsAndCommons.DEFAULT_NAMESPACE_ID, "nacos.test.1")).thenReturn(service); + Mockito.when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, "nacos.test.1")).thenReturn(service); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/v1/ns/instances") diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/DomainsManagerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/DomainsManagerTest.java index f3a560cae..aadac1140 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/DomainsManagerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/DomainsManagerTest.java @@ -15,6 +15,7 @@ */ package com.alibaba.nacos.naming.core; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.naming.BaseTest; import com.alibaba.nacos.naming.misc.UtilsAndCommons; import org.junit.Assert; @@ -44,7 +45,7 @@ public class DomainsManagerTest extends BaseTest { @Test public void easyRemoveDom() throws Exception { - serviceManager.easyRemoveService(UtilsAndCommons.DEFAULT_NAMESPACE_ID, "nacos.test.1"); + serviceManager.easyRemoveService(Constants.DEFAULT_NAMESPACE_ID, "nacos.test.1"); } @Test @@ -52,9 +53,9 @@ public class DomainsManagerTest extends BaseTest { Service service = new Service(); service.setName("nacos.test.1"); - serviceManager.chooseServiceMap(UtilsAndCommons.DEFAULT_NAMESPACE_ID).put("nacos.test.1", service); + serviceManager.chooseServiceMap(Constants.DEFAULT_NAMESPACE_ID).put("nacos.test.1", service); - List list = serviceManager.searchServices(UtilsAndCommons.DEFAULT_NAMESPACE_ID, "nacos.test.*"); + List list = serviceManager.searchServices(Constants.DEFAULT_NAMESPACE_ID, "nacos.test.*"); Assert.assertNotNull(list); Assert.assertEquals(1, list.size()); Assert.assertEquals("nacos.test.1", list.get(0).getName()); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java index b38c59c6f..8b1b04669 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java @@ -15,6 +15,7 @@ */ package com.alibaba.nacos.test.naming; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; @@ -55,10 +56,19 @@ public class AutoDeregisterInstance_ITCase { @Before public void init() throws Exception { + + NamingBase.prepareServer(port); + if (naming == null) { - //TimeUnit.SECONDS.sleep(10); -// naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); - naming = NamingFactory.createNamingService("11.239.112.161:8848,11.239.113.204:8848,11.239.114.187:8848"); + naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); + } + + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; } } @@ -84,7 +94,7 @@ public class AutoDeregisterInstance_ITCase { NacosNamingService namingServiceImpl = (NacosNamingService) naming; - namingServiceImpl.getBeatReactor().removeBeatInfo(serviceName, "DEFAULT_GROUP", "127.0.0.1", TEST_PORT); + namingServiceImpl.getBeatReactor().removeBeatInfo(serviceName, Constants.DEFAULT_GROUP, "127.0.0.1", TEST_PORT); verifyInstanceList(instances, 1, serviceName); instances = naming.getAllInstances(serviceName); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/DeregisterInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/DeregisterInstance_ITCase.java index 315a9e419..e3e72dc27 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/DeregisterInstance_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/DeregisterInstance_ITCase.java @@ -18,7 +18,6 @@ package com.alibaba.nacos.test.naming; import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; -import com.alibaba.nacos.client.naming.NacosNamingService; import com.alibaba.nacos.naming.NamingApp; import org.junit.Assert; import org.junit.Before; @@ -53,12 +52,20 @@ public class DeregisterInstance_ITCase { @Before public void init() throws Exception { - NamingBase.setServerStatusUp(port); + NamingBase.prepareServer(port); if (naming == null) { //TimeUnit.SECONDS.sleep(10); naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); } + + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; + } } /** @@ -69,7 +76,7 @@ public class DeregisterInstance_ITCase { @Test public void dregDomTest() throws Exception { String serviceName = randomDomainName(); - + System.out.println(serviceName); naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT); naming.registerInstance(serviceName, "127.0.0.2", TEST_PORT); @@ -77,7 +84,7 @@ public class DeregisterInstance_ITCase { verifyInstanceList(instances, 2, serviceName); instances = naming.getAllInstances(serviceName); - Assert.assertEquals(instances.size(), 2); + Assert.assertEquals(2, instances.size()); naming.deregisterInstance(serviceName, "127.0.0.1", TEST_PORT); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_ITCase.java index b795f1a13..aeaaa4508 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_ITCase.java @@ -44,7 +44,19 @@ public class MultiTenant_ITCase { @Before public void init() throws Exception { + + NamingBase.prepareServer(port); + naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); + + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; + } + Properties properties = new Properties(); properties.put(PropertyKeyConst.NAMESPACE, "namespace-1"); properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1" + ":" + port); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_InstanceAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_InstanceAPI_ITCase.java index d32b5262f..18780b32b 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_InstanceAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/MultiTenant_InstanceAPI_ITCase.java @@ -61,11 +61,22 @@ public class MultiTenant_InstanceAPI_ITCase { @Before public void init() throws Exception { + + NamingBase.prepareServer(port); + String url = String.format("http://localhost:%d/", port); this.base = new URL(url); naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; + } + Properties properties = new Properties(); properties.put(PropertyKeyConst.NAMESPACE, "namespace-1"); properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1" + ":" + port); @@ -221,10 +232,10 @@ public class MultiTenant_InstanceAPI_ITCase { .appendParam("serviceName", serviceName) .appendParam("ip", "33.33.33.33") .appendParam("port", "8888") - .appendParam("namespaceId", "namespace-1") //新增 .done(), String.class, HttpMethod.PUT); + System.out.println(response.getBody()); Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); response = request("/nacos/v1/ns/instance/list", @@ -235,7 +246,7 @@ public class MultiTenant_InstanceAPI_ITCase { String.class); Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); JSONObject json = JSON.parseObject(response.getBody()); - Assert.assertEquals(2, json.getJSONArray("hosts").size()); + Assert.assertEquals(1, json.getJSONArray("hosts").size()); //namespace-2个数 response = request("/nacos/v1/ns/instance/list", diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java index 5ee866582..30a5afa54 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java @@ -156,7 +156,7 @@ public class NamingBase { return true; } - public static void setServerStatusUp(int localPort) { + public static void prepareServer(int localPort) { String url = "http://127.0.0.1:" + localPort + "/nacos/v1/ns/operator/switches?entry=overriddenServerStatus&value=UP"; List headers = new ArrayList(); headers.add("User-Agent"); @@ -165,5 +165,15 @@ public class NamingBase { HttpClient.request(url, headers, new HashMap(), "UTF-8", "PUT"); Assert.assertEquals(HttpStatus.SC_OK, result.code); + + + url = "http://127.0.0.1:" + localPort + "/nacos/v1/ns/operator/switches?entry=serverMode&value=AP"; + headers = new ArrayList(); + headers.add("User-Agent"); + headers.add("Nacos-Server"); + result = + HttpClient.request(url, headers, new HashMap(), "UTF-8", "PUT"); + + Assert.assertEquals(HttpStatus.SC_OK, result.code); } } diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java index a4ecb126d..4b59d1f3c 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java @@ -53,10 +53,21 @@ public class RegisterInstance_ITCase { @Before public void init() throws Exception { + + NamingBase.prepareServer(port); + if (naming == null) { TimeUnit.SECONDS.sleep(10); naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); } + + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; + } } @Test diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/RestAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/RestAPI_ITCase.java index bb6176dc8..3e9b1f1a7 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/RestAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/RestAPI_ITCase.java @@ -508,7 +508,7 @@ public class RestAPI_ITCase { ResponseEntity response = request("/nacos/v1/ns/api/reCalculateCheckSum4Dom", Params.newParams() - .appendParam(CommonParams.NAMESPACE_ID, UtilsAndCommons.DEFAULT_NAMESPACE_ID) + .appendParam(CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID) .appendParam("dom", NamingBase.TEST_DOM_1) .done(), String.class); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/SelectInstances_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/SelectInstances_ITCase.java index 372d0faf9..3b4083ef4 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/SelectInstances_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/SelectInstances_ITCase.java @@ -56,10 +56,18 @@ public class SelectInstances_ITCase { @Before public void init() throws Exception { + NamingBase.prepareServer(port); if (naming == null) { //TimeUnit.SECONDS.sleep(10); naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); } + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; + } } /** diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/SelectOneHealthyInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/SelectOneHealthyInstance_ITCase.java index 02df34255..4291aff02 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/SelectOneHealthyInstance_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/SelectOneHealthyInstance_ITCase.java @@ -50,10 +50,18 @@ public class SelectOneHealthyInstance_ITCase { private int port; @Before public void init() throws Exception{ + NamingBase.prepareServer(port); if (naming == null) { //TimeUnit.SECONDS.sleep(10); naming = NamingFactory.createNamingService("127.0.0.1"+":"+port); } + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; + } } diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/ServiceListTest.java b/test/src/test/java/com/alibaba/nacos/test/naming/ServiceListTest.java index 5c82a5891..717e361a2 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/ServiceListTest.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/ServiceListTest.java @@ -57,9 +57,17 @@ public class ServiceListTest { @Before public void init() throws Exception { + NamingBase.prepareServer(port); if (naming == null) { naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); } + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; + } } @Test diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/SubscribeCluster_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/SubscribeCluster_ITCase.java index d749cd67d..2ae850451 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/SubscribeCluster_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/SubscribeCluster_ITCase.java @@ -54,11 +54,19 @@ public class SubscribeCluster_ITCase { @Before public void init() throws Exception { + NamingBase.prepareServer(port); instances.clear(); if (naming == null) { //TimeUnit.SECONDS.sleep(10); naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); } + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; + } } private volatile List instances = Collections.emptyList(); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/Subscribe_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/Subscribe_ITCase.java index 7ba3d7799..c85f01d8a 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/Subscribe_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/Subscribe_ITCase.java @@ -53,11 +53,19 @@ public class Subscribe_ITCase { @Before public void init() throws Exception { + NamingBase.prepareServer(port); instances.clear(); if (naming == null) { //TimeUnit.SECONDS.sleep(10); naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); } + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; + } } private volatile List instances = Collections.emptyList(); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/Unsubscribe_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/Unsubscribe_ITCase.java index 87b5d813b..f0d020581 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/Unsubscribe_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/Unsubscribe_ITCase.java @@ -51,14 +51,22 @@ public class Unsubscribe_ITCase { private NamingService naming; @LocalServerPort private int port; - + @Before public void init() throws Exception{ + NamingBase.prepareServer(port); instances = Collections.emptyList(); if (naming == null) { //TimeUnit.SECONDS.sleep(10); naming = NamingFactory.createNamingService("127.0.0.1"+":"+port); } + while (true) { + if (!"UP".equals(naming.getServerStatus())) { + Thread.sleep(1000L); + continue; + } + break; + } } private volatile List instances = Collections.emptyList();