diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java index 584ca7342..ded3beb1b 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java @@ -15,6 +15,8 @@ */ package com.alibaba.nacos.api.naming.pojo; +import com.alibaba.nacos.api.selector.AbstractSelector; + import java.util.HashMap; import java.util.Map; @@ -51,7 +53,7 @@ public class Service { /** * Selector name of this service */ - private String selectorName; + private AbstractSelector selector; private Map metadata = new HashMap(); @@ -111,11 +113,11 @@ public class Service { this.metadata.put(key, value); } - public String getSelectorName() { - return selectorName; + public AbstractSelector getSelector() { + return selector; } - public void setSelectorName(String selectorName) { - this.selectorName = selectorName; + public void setSelector(AbstractSelector selector) { + this.selector = selector; } } diff --git a/distribution/plugins/cmdb/nacos-cmdb-plugin-example.jar b/distribution/plugins/cmdb/nacos-cmdb-plugin-example.jar index a0cd10864..0a322370f 100644 Binary files a/distribution/plugins/cmdb/nacos-cmdb-plugin-example.jar and b/distribution/plugins/cmdb/nacos-cmdb-plugin-example.jar differ 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 4272d9ab3..f27cc5710 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 @@ -20,6 +20,7 @@ import com.alibaba.fastjson.JSONObject; import com.alibaba.nacos.api.naming.pojo.Cluster; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.Service; +import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.common.util.WebUtils; import com.alibaba.nacos.naming.core.Domain; import com.alibaba.nacos.naming.core.DomainsManager; @@ -31,6 +32,8 @@ import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.pojo.ClusterInfo; import com.alibaba.nacos.naming.pojo.IpAddressInfo; import com.alibaba.nacos.naming.pojo.ServiceDetailInfo; +import com.alibaba.nacos.naming.selector.LabelSelector; +import com.alibaba.nacos.naming.selector.NoneSelector; import com.alibaba.nacos.naming.view.ServiceDetailView; import com.alibaba.nacos.naming.view.ServiceView; import org.apache.commons.collections.CollectionUtils; @@ -125,6 +128,18 @@ public class CatalogController { service.setHealthCheckMode(HealthCheckMode.client.name()); } service.setMetadata(domain.getMetadata()); + + switch (SelectorType.valueOf(domain.getSelector().getType())) { + case label: + service.setSelector((LabelSelector) domain.getSelector()); + break; + case none: + case unknown: + default: + service.setSelector((NoneSelector) domain.getSelector()); + break; + } + detailView.setService(service); List clusters = new ArrayList<>(); 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 76ac1cdf0..2d9eec062 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 @@ -271,6 +271,10 @@ public class ServiceController { private Selector parseSelector(String selectorJsonString) throws NacosException { + if (StringUtils.isBlank(selectorJsonString)) { + return new NoneSelector(); + } + JSONObject selectorJson = JSON.parseObject(selectorJsonString); switch (SelectorType.valueOf(selectorJson.getString("type"))) { case none: diff --git a/naming/src/main/java/com/alibaba/nacos/naming/selector/LabelSelector.java b/naming/src/main/java/com/alibaba/nacos/naming/selector/LabelSelector.java index 5b1a37286..ae27eeaf6 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/selector/LabelSelector.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/selector/LabelSelector.java @@ -68,9 +68,9 @@ public class LabelSelector extends com.alibaba.nacos.api.selector.label.LabelSel private static final Set SUPPORTED_OUTER_CONNCETORS = new HashSet<>(); - private static final String CONSUMER_PREFIX = "CONSUMER\\.label\\."; + private static final String CONSUMER_PREFIX = "CONSUMER.label."; - private static final String PROVIDER_PREFIX = "PROVIDER\\.label\\."; + private static final String PROVIDER_PREFIX = "PROVIDER.label."; static { SUPPORTED_INNER_CONNCETORS.add("="); @@ -104,7 +104,11 @@ public class LabelSelector extends com.alibaba.nacos.api.selector.label.LabelSel boolean matched = true; for (String labelName : getLabels()) { - if (!StringUtils.equals(cmdbReader.queryLabel(consumer, PreservedEntityTypes.ip.name(), labelName), + + String consumerLabelValue = cmdbReader.queryLabel(consumer, PreservedEntityTypes.ip.name(), labelName); + + if (StringUtils.isNotBlank(consumerLabelValue) && + !StringUtils.equals(consumerLabelValue, cmdbReader.queryLabel(ipAddress.getIp(), PreservedEntityTypes.ip.name(), labelName))) { matched = false; break; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java b/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java index 9b5c3dd63..cecbfdd9e 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java @@ -1145,7 +1145,7 @@ public class ApiCommands { srvedIPs = domObj.srvIPs(clientIP, Arrays.asList(StringUtils.split(clusters, ","))); // filter ips using selector: - if (domObj.getSelector() != null) { + if (domObj.getSelector() != null && StringUtils.isNotBlank(clientIP)) { srvedIPs = domObj.getSelector().select(clientIP, srvedIPs); }