Skip to content

Commit

Permalink
alibaba#187 Refactor some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Dec 10, 2018
1 parent 0409aae commit 3198ec6
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 112 deletions.
12 changes: 12 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.selector.AbstractSelector;

import java.util.List;

Expand Down Expand Up @@ -186,6 +187,17 @@ public interface NamingService {
*/
ListView<String> getServicesOfServer(int pageNo, int pageSize) throws NacosException;

/**
* Get all service names from server
*
* @param pageNo page index
* @param pageSize page size
* @param selector selector to filter the resource
* @return list of service names
* @throws NacosException
*/
ListView<String> getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException;

/**
* Get all subscribed services of current client
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alibaba.nacos.api.selector;

/**
* @author <a href="mailto:[email protected]">nkorange</a>
*/
public abstract class AbstractSelector {

private String type;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}
19 changes: 19 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/selector/SelectorType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.alibaba.nacos.api.selector;

/**
* @author <a href="mailto:[email protected]">nkorange</a>
*/
public enum SelectorType {
/**
* not match any type
*/
unknown,
/**
* not filter out
*/
none,
/**
* select by label
*/
label
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.alibaba.nacos.api.selector.label;

/**
* @author <a href="mailto:[email protected]">nkorange</a>
*/
public class ExpressionBuilder {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alibaba.nacos.api.selector.label;

import com.alibaba.nacos.api.selector.AbstractSelector;
import com.alibaba.nacos.api.selector.SelectorType;

/**
* @author <a href="mailto:[email protected]">nkorange</a>
*/
public class LabelSelector extends AbstractSelector {

/**
* Label expression of this selector.
*/
private String expression;

public LabelSelector() {
this.setType(SelectorType.label.name());
}

public String getExpression() {
return expression;
}

public void setExpression(String expression) {
this.expression = expression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.selector.AbstractSelector;
import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.beat.BeatReactor;
import com.alibaba.nacos.client.naming.core.Balancer;
Expand Down Expand Up @@ -256,6 +257,11 @@ public ListView<String> getServicesOfServer(int pageNo, int pageSize) throws Nac
return serverProxy.getServiceList(pageNo, pageSize);
}

@Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException {
return serverProxy.getServiceList(pageNo, pageSize);
}

@Override
public List<ServiceInfo> getSubscribeServices() {
return new ArrayList<ServiceInfo>(hostReactor.getServiceInfoMap().values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.api.selector.AbstractSelector;
import com.alibaba.nacos.client.naming.utils.*;
import com.alibaba.nacos.common.util.UuidUtils;

Expand Down Expand Up @@ -205,10 +206,15 @@ public boolean serverHealthy() {
}

public ListView<String> getServiceList(int pageNo, int pageSize) throws NacosException {
return getServiceList(pageNo, pageSize, null);
}

public ListView<String> getServiceList(int pageNo, int pageSize, AbstractSelector selector) throws NacosException {

Map<String, String> params = new HashMap<String, String>(4);
params.put("pageNo", String.valueOf(pageNo));
params.put("pageSize", String.valueOf(pageSize));


String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/service/list", params);

Expand Down
Binary file modified distribution/plugins/cmdb/nacos-cmdb-plugin-example.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public String deregister(HttpServletRequest request) throws Exception {
return deRegService(request);
}

@RequestMapping(value = "/instance/update", method = RequestMethod.POST)
@RequestMapping(value = {"/instance/update", "instance"}, method = RequestMethod.POST)
public String update(HttpServletRequest request) throws Exception {
String serviceName = WebUtils.required(request, "serviceName");
Map<String, String[]> params = new HashMap<>(request.getParameterMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.common.util.WebUtils;
import com.alibaba.nacos.naming.core.DomainsManager;
import com.alibaba.nacos.naming.core.VirtualClusterDomain;
Expand All @@ -25,8 +26,7 @@
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.selector.LabelSelector;
import com.alibaba.nacos.naming.selector.NoneSelector;
import com.alibaba.nacos.naming.selector.AbstractSelector;
import com.alibaba.nacos.naming.selector.SelectorType;
import com.alibaba.nacos.naming.selector.Selector;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -208,7 +208,7 @@ public String update(HttpServletRequest request) throws Exception {
return "ok";
}

private AbstractSelector parseSelector(String selectorJsonString) throws NacosException {
private Selector parseSelector(String selectorJsonString) throws NacosException {

JSONObject selectorJson = JSON.parseObject(selectorJsonString);
switch (SelectorType.valueOf(selectorJson.getString("type"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import com.alibaba.nacos.naming.push.PushService;
import com.alibaba.nacos.naming.raft.RaftCore;
import com.alibaba.nacos.naming.raft.RaftListener;
import com.alibaba.nacos.naming.selector.Selector;
import com.alibaba.nacos.naming.selector.NoneSelector;
import com.alibaba.nacos.naming.selector.AbstractSelector;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.RandomStringUtils;
Expand Down Expand Up @@ -63,7 +63,7 @@ public class VirtualClusterDomain implements Domain, RaftListener {
private Boolean enableHealthCheck = true;
private Boolean enabled = true;
private Boolean enableClientBeat = false;
private AbstractSelector selector = new NoneSelector();
private Selector selector = new NoneSelector();

/**
* IP will be deleted if it has not send beat for some time, default timeout is half an hour .
Expand Down Expand Up @@ -144,11 +144,11 @@ public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

public AbstractSelector getSelector() {
public Selector getSelector() {
return selector;
}

public void setSelector(AbstractSelector selector) {
public void setSelector(Selector selector) {
this.selector = selector;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.alibaba.nacos.naming.core.Domain;
import com.alibaba.nacos.naming.exception.NacosException;
import com.alibaba.nacos.naming.healthcheck.JsonAdapter;
import com.alibaba.nacos.naming.selector.AbstractSelector;
import com.alibaba.nacos.naming.selector.Selector;
import com.alibaba.nacos.naming.selector.SelectorJsonAdapter;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -126,9 +126,9 @@ public class UtilsAndCommons {
.putDeserializer(AbstractHealthChecker.class, JsonAdapter.getInstance());

SerializeConfig.getGlobalInstance()
.put(AbstractSelector.class, SelectorJsonAdapter.getInstance());
.put(Selector.class, SelectorJsonAdapter.getInstance());
ParserConfig.getGlobalInstance()
.putDeserializer(AbstractSelector.class, SelectorJsonAdapter.getInstance());
.putDeserializer(Selector.class, SelectorJsonAdapter.getInstance());

// write null values, otherwise will cause compatibility issues
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.WriteNullStringAsEmpty.getMask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import com.alibaba.nacos.api.cmdb.pojo.PreservedEntityTypes;
import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.cmdb.service.CmdbReader;
import com.alibaba.nacos.naming.boot.SpringContext;
import com.alibaba.nacos.naming.core.IpAddress;
Expand Down Expand Up @@ -52,7 +53,7 @@
* @author <a href="mailto:[email protected]">nkorange</a>
* @see CmdbReader
*/
public class LabelSelector extends AbstractSelector {
public class LabelSelector extends com.alibaba.nacos.api.selector.label.LabelSelector implements Selector {

private CmdbReader cmdbReader;

Expand All @@ -63,17 +64,6 @@ public class LabelSelector extends AbstractSelector {
*/
private Set<String> labels;

/**
* Label expression of this selector.
* <p>
* Currently we only support this very single type of expression:
* <pre>
* consumer.labelA = provider.labelA & consumer.labelB = provider.labelB
* </pre>
* TODO what this expression means?
*/
private String expression;

private static final Set<String> SUPPORTED_INNER_CONNCETORS = new HashSet<>();

private static final Set<String> SUPPORTED_OUTER_CONNCETORS = new HashSet<>();
Expand All @@ -95,15 +85,8 @@ public void setLabels(Set<String> labels) {
this.labels = labels;
}

public String getExpression() {
return expression;
}

public void setExpression(String expression) {
this.expression = expression;
}

public LabelSelector() {
setType(SelectorType.label.name());
ApplicationContext context = SpringContext.getAppContext();
cmdbReader = context.getBean(CmdbReader.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package com.alibaba.nacos.naming.selector;

import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.naming.core.IpAddress;

import java.util.List;

/**
* @author <a href="mailto:[email protected]">nkorange</a>
*/
public class NoneSelector extends AbstractSelector {
public class NoneSelector extends com.alibaba.nacos.api.selector.AbstractSelector implements Selector {

public NoneSelector() {
this.setType(SelectorType.none.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,17 @@
* recognizes it and can correctly create this type of selector.
*
* @author <a href="mailto:[email protected]">nkorange</a>
* @see SelectorType
* @see com.alibaba.nacos.api.selector.SelectorType
* @see SelectorJsonAdapter
*/
public abstract class AbstractSelector {
public interface Selector {

/**
* @see SelectorType
* Get the type of this selector
*
* @return type of selector
*/
protected String type = "unknown";

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
String getType();

/**
* Select qualified instances from providers
Expand All @@ -57,5 +51,5 @@ public void setType(String type) {
* @param providers candidate provider addresses
* @return selected provider addresses
*/
public abstract List<IpAddress> select(String consumer, List<IpAddress> providers);
List<IpAddress> select(String consumer, List<IpAddress> providers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.nacos.api.selector.AbstractSelector;
import com.alibaba.nacos.api.selector.SelectorType;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
Expand Down Expand Up @@ -82,7 +84,7 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
return;
}

AbstractSelector selector = (AbstractSelector) object;
Selector selector = (Selector) object;

writer.writeFieldValue(',', "type", selector.getType());

Expand Down

This file was deleted.

Loading

0 comments on commit 3198ec6

Please sign in to comment.