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 17bde92827d..13c1716a0d2 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 43d68c9a020..8a42df09192 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 8e359d9b7fb..46ad0c227ba 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 ServiceInfo(String key) { 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 void setName(String name) { 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 String getKeyEncoded() { @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 static ServiceInfo fromKey(String key) { 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 1212ca81920..97b7f1ab1fe 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 List getAllInstances(String serviceName, String groupName, List @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 List getAllInstances(String serviceName, List clusters, 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 List selectInstances(String serviceName, boolean healthy) throw @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 List selectInstances(String serviceName, boolean healthy, boole @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,26 +289,27 @@ public List selectInstances(String serviceName, List clusters, @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 { + 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 { + 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, ",")); } return selectInstances(serviceInfo, healthy); } - @Override - public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy, boolean subscribe) throws NacosException { - return null; - } - @Override public Instance selectOneHealthyInstance(String serviceName) throws NacosException { return selectOneHealthyInstance(serviceName, new ArrayList()); @@ -316,7 +317,7 @@ public Instance selectOneHealthyInstance(String serviceName) throws NacosExcepti @Override public Instance selectOneHealthyInstance(String serviceName, String groupName) throws NacosException { - return null; + return selectOneHealthyInstance(serviceName, groupName, true); } @Override @@ -326,7 +327,7 @@ public Instance selectOneHealthyInstance(String serviceName, boolean subscribe) @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 Instance selectOneHealthyInstance(String serviceName, List cluste @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 { + return selectOneHealthyInstance(serviceName, Constants.DEFAULT_GROUP, clusters, subscribe); + } + + @Override + public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { if (subscribe) { return Balancer.RandomByWeight.selectHost( - hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ","))); + hostReactor.getServiceInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","))); } else { return Balancer.RandomByWeight.selectHost( - hostReactor.getServiceInfoDirectlyFromServer(serviceName, StringUtils.join(clusters, ","))); + hostReactor.getServiceInfoDirectlyFromServer(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","))); } } @Override - public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { - return null; - } - - @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 217dde6410d..b183a790bbc 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 static void write(ServiceInfo dom, String dir) { 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 static Map read(String cacheDir) { 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 dacf29b941c..b4233727f76 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 boolean serverHealthy() { } } - 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 374ce2ed444..0f55ba330ef 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 e5ff798f743..be2545c658f 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 void doRaftAuth(HttpServletRequest req) throws Exception { 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 2a868ce418d..2cc4b5f2811 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 @@ 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 synchronized Datum readDatum(File file, String namespaceId) throws IOExce } 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 91c9f2ad910..6da4a2470e3 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 @@ 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 JSONObject serviceList(HttpServletRequest request) throws Exception { 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 ServiceDetailView serviceDetail(HttpServletRequest request) throws Except 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 JSONObject instanceList(HttpServletRequest request) throws Exception { 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 List listDetail(HttpServletRequest request) { 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 b8e229a2ffa..c051a8a773b 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 @@ 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 045298a052b..461ec1fcd2b 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 JSONObject server(HttpServletRequest request) { 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 91664f91284..45bbdf9d72a 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 @@ 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 String getData(PushService.PushClient client) throws Exception { 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 String register(HttpServletRequest request) throws Exception { 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 String deregister(HttpServletRequest request) throws Exception { 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 String update(HttpServletRequest request) throws Exception { 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 JSONObject list(HttpServletRequest request) throws Exception { 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 JSONObject beat(HttpServletRequest request) throws Exception { } 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 JSONObject listWithHealthStatus(HttpServletRequest request) throws NacosE 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 8bb15458908..b78d410af8a 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 @@ 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 JSONObject metrics(HttpServletRequest request) { @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 JSONObject getHealthyServerList(HttpServletRequest request) { @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 9b73ef31a85..668c8dd542c 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 @@ 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 String create(HttpServletRequest request) throws Exception { 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 String remove(HttpServletRequest request) throws Exception { 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 JSONObject list(HttpServletRequest request) throws Exception { 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 JSONObject list(HttpServletRequest request) throws Exception { 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 JSONObject list(HttpServletRequest request) throws Exception { 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 JSONObject list(HttpServletRequest request) throws Exception { 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 String serviceStatus(HttpServletRequest request) { 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 f2019d9608a..f9f3d4a8fe9 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 @@ 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 void addUpdatedService2Queue(String namespaceId, String serviceName, Stri @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 void onChange(String key, Service service) throws Exception { } 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 void createEmptyService(String namespaceId, String serviceName) throws Na 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 static class ServiceChecksum { 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 f9fff3a161c..8c12f566537 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.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 SwitchDomain getSwitchDomain() { @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 dc948bf5f61..050a9b9abc9 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.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 static String assembleFullServiceName(String namespaceId, String serviceN } 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 68e2d8c794f..7219a039924 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 void doFilter(ServletRequest servletRequest, ServletResponse servletRespo // 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 84feb7bcb8f..1875c810c79 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 void init() { } 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 59decd11c3d..aa9f5389156 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 void doFilter(ServletRequest request, ServletResponse response, FilterCha 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 56b1f3c5775..06e1eb7d0c7 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 d2c203bf9ae..0d84979a27d 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 @@ 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 void registerInstance() throws Exception { 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 void getInstances() throws Exception { 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 f3a560caef3..aadac114093 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 void before() { @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 void searchDom() throws Exception { 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 b38c59c6f54..8b1b0466979 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 void autoDregDomClustersTest() throws Exception { 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 315a9e41936..e3e72dc2731 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 @@ 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 void init() throws Exception { @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 void dregDomTest() throws Exception { 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 b795f1a133b..aeaaa45085a 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 d32b5262f40..18780b32ba1 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 void multipleTenant_putInstance() throws Exception { .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 void multipleTenant_putInstance() throws Exception { 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 5ee86658283..30a5afa5426 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 static boolean verifyInstanceList(List instanceList1, List headers = new ArrayList(); headers.add("User-Agent"); @@ -165,5 +165,15 @@ public static void setServerStatusUp(int localPort) { 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 a4ecb126d5e..4b59d1f3ccf 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 bb6176dc88d..3e9b1f1a723 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 void reCalculateCheckSum4Dom() throws Exception { 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 372d0faf9dd..3b4083ef459 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 02df3425583..4291aff02cc 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 5c82a589185..717e361a215 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 d749cd67df0..2ae850451c5 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 7ba3d7799c5..c85f01d8a93 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 87b5d813bce..f0d02058174 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();