Skip to content

Commit

Permalink
#269 fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Mar 4, 2019
1 parent af26af7 commit 8c23f34
Show file tree
Hide file tree
Showing 37 changed files with 325 additions and 135 deletions.
2 changes: 2 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "@@";
}
37 changes: 30 additions & 7 deletions api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -428,7 +428,7 @@ public interface NamingService {
void subscribe(String serviceName, List<String> 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
Expand All @@ -439,7 +439,7 @@ public interface NamingService {
void subscribe(String serviceName, String groupName, List<String> clusters, EventListener listener) throws NacosException;

/**
* unsubscribe event listener of service
* Unsubscribe event listener of service
*
* @param serviceName name of service
* @param listener event listener
Expand All @@ -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
Expand All @@ -468,7 +468,7 @@ public interface NamingService {
void unsubscribe(String serviceName, List<String> 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
Expand All @@ -479,7 +479,7 @@ public interface NamingService {
void unsubscribe(String serviceName, String groupName, List<String> 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
Expand All @@ -489,7 +489,18 @@ public interface NamingService {
ListView<String> 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<String> 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
Expand All @@ -500,6 +511,18 @@ public interface NamingService {
*/
ListView<String> 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<String> getServicesOfServer(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException;

/**
* Get all subscribed services of current client
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +38,8 @@ public class ServiceInfo {
@JSONField(name = "dom")
private String name;

private String groupName;

private String clusters;

private long cacheMillis = 1000L;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -178,21 +189,24 @@ 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;
}

@JSONField(serialize = false)
public static String getKey(String name, String clusters) {

if (!isEmpty(clusters)) {
return name + SPLITER + clusters;
return name + Constants.SERVICE_INFO_SPLITER + clusters;
}

return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,17 @@ public List<Instance> getAllInstances(String serviceName, String groupName, List
@Override
public List<Instance> getAllInstances(String serviceName, List<String> clusters, boolean subscribe)
throws NacosException {
return getAllInstances(serviceName, Constants.DEFAULT_GROUP, clusters, subscribe);
}

@Override
public List<Instance> getAllInstances(String serviceName, String groupName, List<String> 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<Instance> list;
if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
Expand All @@ -255,19 +260,14 @@ public List<Instance> getAllInstances(String serviceName, List<String> clusters,
return list;
}

@Override
public List<Instance> getAllInstances(String serviceName, String groupName, List<String> clusters, boolean subscribe) throws NacosException {
return null;
}

@Override
public List<Instance> selectInstances(String serviceName, boolean healthy) throws NacosException {
return selectInstances(serviceName, new ArrayList<String>(), healthy);
}

@Override
public List<Instance> selectInstances(String serviceName, String groupName, boolean healthy) throws NacosException {
return null;
return selectInstances(serviceName, groupName, healthy, true);
}

@Override
Expand All @@ -278,7 +278,7 @@ public List<Instance> selectInstances(String serviceName, boolean healthy, boole

@Override
public List<Instance> selectInstances(String serviceName, String groupName, boolean healthy, boolean subscribe) throws NacosException {
return null;
return selectInstances(serviceName, groupName, new ArrayList<String>(), healthy, subscribe);
}

@Override
Expand All @@ -289,34 +289,35 @@ public List<Instance> selectInstances(String serviceName, List<String> clusters,

@Override
public List<Instance> selectInstances(String serviceName, String groupName, List<String> clusters, boolean healthy) throws NacosException {
return null;
return selectInstances(serviceName, groupName, clusters, healthy, true);
}

@Override
public List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy,
boolean subscribe) throws NacosException {
return selectInstances(serviceName, Constants.DEFAULT_GROUP, clusters, healthy, subscribe);
}

@Override
public List<Instance> selectInstances(String serviceName, String groupName, List<String> 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<Instance> selectInstances(String serviceName, String groupName, List<String> clusters, boolean healthy, boolean subscribe) throws NacosException {
return null;
}

@Override
public Instance selectOneHealthyInstance(String serviceName) throws NacosException {
return selectOneHealthyInstance(serviceName, new ArrayList<String>());
}

@Override
public Instance selectOneHealthyInstance(String serviceName, String groupName) throws NacosException {
return null;
return selectOneHealthyInstance(serviceName, groupName, true);
}

@Override
Expand All @@ -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<String>(), subscribe);
}

@Override
Expand All @@ -336,78 +337,87 @@ public Instance selectOneHealthyInstance(String serviceName, List<String> cluste

@Override
public Instance selectOneHealthyInstance(String serviceName, String groupName, List<String> clusters) throws NacosException {
return null;
return selectOneHealthyInstance(serviceName, groupName, clusters, true);
}

@Override
public Instance selectOneHealthyInstance(String serviceName, List<String> clusters, boolean subscribe)
throws NacosException {
return selectOneHealthyInstance(serviceName, Constants.DEFAULT_GROUP, clusters, subscribe);
}

@Override
public Instance selectOneHealthyInstance(String serviceName, String groupName, List<String> 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<String> 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<String>(), listener);
}

@Override
public void subscribe(String serviceName, String groupName, EventListener listener) throws NacosException {

subscribe(serviceName, groupName, new ArrayList<String>(), listener);
}

@Override
public void subscribe(String service, List<String> clusters, EventListener listener) {
eventDispatcher.addListener(hostReactor.getServiceInfo(service, StringUtils.join(clusters, ",")),
StringUtils.join(clusters, ","), listener);
public void subscribe(String serviceName, List<String> clusters, EventListener listener) throws NacosException {
subscribe(serviceName, Constants.DEFAULT_GROUP, clusters, listener);
}

@Override
public void subscribe(String serviceName, String groupName, List<String> 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<String>(), listener);
}

@Override
public void unsubscribe(String serviceName, String groupName, EventListener listener) throws NacosException {

unsubscribe(serviceName, groupName, new ArrayList<String>(), listener);
}

@Override
public void unsubscribe(String service, List<String> clusters, EventListener listener) {
eventDispatcher.removeListener(service, StringUtils.join(clusters, ","), listener);
public void unsubscribe(String serviceName, List<String> clusters, EventListener listener) throws NacosException {
unsubscribe(serviceName, Constants.DEFAULT_GROUP, clusters, listener);
}

@Override
public void unsubscribe(String serviceName, String groupName, List<String> clusters, EventListener listener) throws NacosException {

eventDispatcher.removeListener(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","), listener);
}

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

@Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize, String groupName) throws NacosException {
return getServicesOfServer(pageNo, pageSize, groupName, null);
}

@Override
public ListView<String> 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<String> getServicesOfServer(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException {
return serverProxy.getServiceList(pageNo, pageSize, groupName, selector);
}

@Override
Expand Down
Loading

0 comments on commit 8c23f34

Please sign in to comment.