This commit is contained in:
nkorange 2018-12-13 20:01:07 +08:00
parent 5f6bdf2d39
commit f96794e170
6 changed files with 34 additions and 9 deletions

View File

@ -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<String, String> metadata = new HashMap<String, String>();
@ -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;
}
}

View File

@ -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<Cluster> clusters = new ArrayList<>();

View File

@ -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:

View File

@ -68,9 +68,9 @@ public class LabelSelector extends com.alibaba.nacos.api.selector.label.LabelSel
private static final Set<String> 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;

View File

@ -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);
}