Skip to content

Commit

Permalink
1. Support get all services from client; 2. Refactor dom class
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Sep 11, 2018
1 parent d17e9f3 commit 899bee7
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package com.alibaba.nacos.api.naming;

import java.util.List;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.listener.EventListener;
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 java.util.List;

/**
* @author dungu.zpf
Expand Down Expand Up @@ -72,7 +74,7 @@ public interface NamingService {
* @param serviceName name of service
* @param ip instance ip
* @param port instance port
* @param clusterName instance cluster name
* @param clusterName instance cluster name
* @throws NacosException
*/
void deregisterInstance(String serviceName, String ip, int port, String clusterName) throws NacosException;
Expand Down Expand Up @@ -173,4 +175,22 @@ public interface NamingService {
* @throws NacosException
*/
void unsubscribe(String serviceName, List<String> clusters, EventListener listener) throws NacosException;

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

/**
* Get all subscribed services of current client
*
* @return subscribed services
* @throws NacosException
*/
List<ServiceInfo> getSubscribeServices() throws NacosException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.alibaba.nacos.api.naming.pojo;

import com.alibaba.fastjson.JSON;

import java.util.List;

/**
* @author dungu.zpf
*/
public class ListView<T> {

private List<T> data;
private int count;

public List<T> getData() {
return data;
}

public void setData(List<T> data) {
this.data = data;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

@Override
public String toString() {
return JSON.toJSONString(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.client.naming.core;
package com.alibaba.nacos.api.naming.pojo;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.api.naming.pojo.Instance;
Expand All @@ -27,7 +27,7 @@
/**
* @author dungu.zpf
*/
public class Domain {
public class ServiceInfo {
@JSONField(serialize = false)
private String jsonFromServer = StringUtils.EMPTY;
public static final String SPLITER = "@@";
Expand All @@ -50,7 +50,7 @@ public class Domain {

private volatile boolean allIPs = false;

public Domain() {
public ServiceInfo() {
}

public boolean isAllIPs() {
Expand All @@ -61,32 +61,32 @@ public void setAllIPs(boolean allIPs) {
this.allIPs = allIPs;
}

public Domain(String key) {
public ServiceInfo(String key) {

int maxKeySectionCount = 4;
int allIpFlagIndex = 3;
int envIndex = 2;
int clusterIndex = 1;
int domNameIndex = 0;
int serviceNameIndex = 0;

String[] keys = key.split(SPLITER);
if (keys.length >= maxKeySectionCount) {
this.name = keys[domNameIndex];
this.name = keys[serviceNameIndex];
this.clusters = keys[clusterIndex];
this.env = keys[envIndex];
if (StringUtils.equals(keys[allIpFlagIndex], UtilAndComs.ALL_IPS)) {
this.setAllIPs(true);
}
} else if (keys.length >= allIpFlagIndex) {
this.name = keys[domNameIndex];
this.name = keys[serviceNameIndex];
this.clusters = keys[clusterIndex];
if (StringUtils.equals(keys[envIndex], UtilAndComs.ALL_IPS)) {
this.setAllIPs(true);
} else {
this.env = keys[envIndex];
}
} else if (keys.length >= envIndex) {
this.name = keys[domNameIndex];
this.name = keys[serviceNameIndex];
if (StringUtils.equals(keys[clusterIndex], UtilAndComs.ALL_IPS)) {
this.setAllIPs(true);
} else {
Expand All @@ -97,11 +97,11 @@ public Domain(String key) {
this.name = keys[0];
}

public Domain(String name, String clusters) {
public ServiceInfo(String name, String clusters) {
this(name, clusters, StringUtils.EMPTY);
}

public Domain(String name, String clusters, String env) {
public ServiceInfo(String name, String clusters, String env) {
this.name = name;
this.clusters = clusters;
this.env = env;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.pojo.Cluster;
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.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.beat.BeatReactor;
import com.alibaba.nacos.client.naming.core.*;
import com.alibaba.nacos.client.naming.core.Balancer;
import com.alibaba.nacos.client.naming.core.EventDispatcher;
import com.alibaba.nacos.client.naming.core.HostReactor;
import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.StringUtils;
Expand Down Expand Up @@ -167,9 +171,9 @@ public List<Instance> getAllInstances(String serviceName) throws NacosException
@Override
public List<Instance> getAllInstances(String serviceName, List<String> clusters) throws NacosException {

Domain domain = hostReactor.getDom(serviceName, StringUtils.join(clusters, ","), StringUtils.EMPTY, false);
ServiceInfo serviceInfo = hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ","), StringUtils.EMPTY, false);
List<Instance> list;
if (domain == null || CollectionUtils.isEmpty(list = domain.getHosts())) {
if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
throw new IllegalStateException("no host to srv for dom: " + serviceName);
}
return list;
Expand All @@ -183,9 +187,9 @@ public List<Instance> selectInstances(String serviceName, boolean healthyOnly) t
@Override
public List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy) throws NacosException {

Domain domain = hostReactor.getDom(serviceName, StringUtils.join(clusters, ","), StringUtils.EMPTY, false);
ServiceInfo serviceInfo = hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ","), StringUtils.EMPTY, false);
List<Instance> list;
if (domain == null || CollectionUtils.isEmpty(list = domain.getHosts())) {
if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
throw new IllegalStateException("no host to srv for dom: " + serviceName);
}

Expand Down Expand Up @@ -217,17 +221,17 @@ public Instance selectOneHealthyInstance(String serviceName) {

@Override
public Instance selectOneHealthyInstance(String serviceName, List<String> clusters) {
return Balancer.RandomByWeight.selectHost(hostReactor.getDom(serviceName, StringUtils.join(clusters, ",")));
return Balancer.RandomByWeight.selectHost(hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ",")));
}

@Override
public void subscribe(String service, EventListener listener) {
eventDispatcher.addListener(hostReactor.getDom(service, StringUtils.EMPTY), StringUtils.EMPTY, listener);
eventDispatcher.addListener(hostReactor.getServiceInfo(service, StringUtils.EMPTY), StringUtils.EMPTY, listener);
}

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

@Override
Expand All @@ -239,4 +243,14 @@ public void unsubscribe(String service, EventListener listener) {
public void unsubscribe(String service, List<String> clusters, EventListener listener) {
eventDispatcher.removeListener(service, StringUtils.join(clusters, ","), listener);
}

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

@Override
public List<ServiceInfo> getSubscribeServices() {
return new ArrayList<>(hostReactor.getServiceInfoMap().values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package com.alibaba.nacos.client.naming.backups;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.client.naming.cache.ConcurrentDiskUtil;
import com.alibaba.nacos.client.naming.cache.DiskCache;
import com.alibaba.nacos.client.naming.core.Domain;
import com.alibaba.nacos.client.naming.core.HostReactor;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.LogUtils;
Expand Down Expand Up @@ -47,7 +47,7 @@ public FailoverReactor(HostReactor hostReactor, String cacheDir) {
this.init();
}

private Map<String, Domain> domainMap = new ConcurrentHashMap<String, Domain>();
private Map<String, ServiceInfo> serviceMap = new ConcurrentHashMap<String, ServiceInfo>();
private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Expand Down Expand Up @@ -144,7 +144,7 @@ class FailoverFileReader implements Runnable {

@Override
public void run() {
Map<String, Domain> domMap = new HashMap<String, Domain>(16);
Map<String, ServiceInfo> domMap = new HashMap<String, ServiceInfo>(16);

BufferedReader reader = null;
try {
Expand All @@ -168,7 +168,7 @@ public void run() {
continue;
}

Domain dom = new Domain(file.getName());
ServiceInfo dom = new ServiceInfo(file.getName());

try {
String dataString = ConcurrentDiskUtil.getFileContent(file, Charset.defaultCharset().toString());
Expand All @@ -177,7 +177,7 @@ public void run() {
String json;
if ((json = reader.readLine()) != null) {
try {
dom = JSON.parseObject(json, Domain.class);
dom = JSON.parseObject(json, ServiceInfo.class);
} catch (Exception e) {
LogUtils.LOG.error("NA", "error while parsing cached dom : " + json, e);
}
Expand All @@ -203,24 +203,24 @@ public void run() {
}

if (domMap.size() > 0) {
domainMap = domMap;
serviceMap = domMap;
}
}
}

class DiskFileWriter extends TimerTask {
public void run() {
Map<String, Domain> map = hostReactor.getDomMap();
for (Map.Entry<String, Domain> entry : map.entrySet()) {
Domain domain = entry.getValue();
if (StringUtils.equals(domain.getKey(), UtilAndComs.ALL_IPS) || StringUtils.equals(domain.getName(), UtilAndComs.ENV_LIST_KEY)
|| StringUtils.equals(domain.getName(), "00-00---000-ENV_CONFIGS-000---00-00")
|| StringUtils.equals(domain.getName(), "vipclient.properties")
|| StringUtils.equals(domain.getName(), "00-00---000-ALL_HOSTS-000---00-00")) {
Map<String, ServiceInfo> map = hostReactor.getServiceInfoMap();
for (Map.Entry<String, ServiceInfo> entry : map.entrySet()) {
ServiceInfo serviceInfo = entry.getValue();
if (StringUtils.equals(serviceInfo.getKey(), UtilAndComs.ALL_IPS) || StringUtils.equals(serviceInfo.getName(), UtilAndComs.ENV_LIST_KEY)
|| StringUtils.equals(serviceInfo.getName(), "00-00---000-ENV_CONFIGS-000---00-00")
|| StringUtils.equals(serviceInfo.getName(), "vipclient.properties")
|| StringUtils.equals(serviceInfo.getName(), "00-00---000-ALL_HOSTS-000---00-00")) {
continue;
}

DiskCache.write(domain, failoverDir);
DiskCache.write(serviceInfo, failoverDir);
}
}
}
Expand All @@ -229,14 +229,14 @@ public boolean isFailoverSwitch() {
return Boolean.parseBoolean(switchParams.get("failover-mode"));
}

public Domain getDom(String key) {
Domain domain = domainMap.get(key);
public ServiceInfo getService(String key) {
ServiceInfo serviceInfo = serviceMap.get(key);

if (domain == null) {
domain = new Domain();
domain.setName(key);
if (serviceInfo == null) {
serviceInfo = new ServiceInfo();
serviceInfo.setName(key);
}

return domain;
return serviceInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.core.Domain;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.LogUtils;
import com.alibaba.nacos.client.naming.utils.StringUtils;
Expand All @@ -36,7 +36,7 @@
*/
public class DiskCache {

public static void write(Domain dom, String dir) {
public static void write(ServiceInfo dom, String dir) {

try {
makeSureCacheDirExists(dir);
Expand Down Expand Up @@ -72,8 +72,8 @@ public static String getLineSeperator() {
return lineSeparator;
}

public static Map<String, Domain> read(String cacheDir) {
Map<String, Domain> domMap = new HashMap<String, Domain>(16);
public static Map<String, ServiceInfo> read(String cacheDir) {
Map<String, ServiceInfo> domMap = new HashMap<String, ServiceInfo>(16);

BufferedReader reader = null;
try {
Expand All @@ -87,12 +87,12 @@ public static Map<String, Domain> read(String cacheDir) {
continue;
}

if (!(file.getName().endsWith(Domain.SPLITER + "meta") || file.getName().endsWith(Domain.SPLITER + "special-url"))) {
Domain dom = new Domain(file.getName());
if (!(file.getName().endsWith(ServiceInfo.SPLITER + "meta") || file.getName().endsWith(ServiceInfo.SPLITER + "special-url"))) {
ServiceInfo dom = new ServiceInfo(file.getName());
List<Instance> ips = new ArrayList<Instance>();
dom.setHosts(ips);

Domain newFormat = null;
ServiceInfo newFormat = null;

try {
String dataString = ConcurrentDiskUtil.getFileContent(file, Charset.defaultCharset().toString());
Expand All @@ -105,7 +105,7 @@ public static Map<String, Domain> read(String cacheDir) {
continue;
}

newFormat = JSON.parseObject(json, Domain.class);
newFormat = JSON.parseObject(json, ServiceInfo.class);

if (StringUtils.isEmpty(newFormat.getName())) {
ips.add(JSON.parseObject(json, Instance.class));
Expand Down
Loading

0 comments on commit 899bee7

Please sign in to comment.