Skip to content

Commit

Permalink
zk registry compatible empty extinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
rayzhang0603 committed Oct 30, 2017
1 parent e8b304f commit 141492a
Showing 1 changed file with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,8 @@

package com.weibo.api.motan.registry.zookeeper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

import com.weibo.api.motan.closable.Closable;
import com.weibo.api.motan.closable.ShutDownHook;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.Watcher;

import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.exception.MotanFrameworkException;
import com.weibo.api.motan.registry.support.command.CommandFailbackRegistry;
Expand All @@ -40,6 +26,16 @@
import com.weibo.api.motan.rpc.URL;
import com.weibo.api.motan.util.ConcurrentHashSet;
import com.weibo.api.motan.util.LoggerUtil;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.commons.lang3.StringUtils;
import org.apache.zookeeper.Watcher;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

public class ZookeeperRegistry extends CommandFailbackRegistry implements Closable {
private ZkClient zkClient;
Expand Down Expand Up @@ -91,7 +87,7 @@ protected void subscribeService(final URL url, final ServiceListener serviceList
childChangeListeners.putIfAbsent(serviceListener, new IZkChildListener() {
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) {
serviceListener.notifyService(url, getUrl(), nodeChildsToUrls(parentPath, currentChilds));
serviceListener.notifyService(url, getUrl(), nodeChildsToUrls(url, parentPath, currentChilds));
LoggerUtil.info(String.format("[ZookeeperRegistry] service list change: path=%s, currentChilds=%s", parentPath, currentChilds.toString()));
}
});
Expand Down Expand Up @@ -195,7 +191,7 @@ protected List<URL> discoverService(URL url) {
if (zkClient.exists(parentPath)) {
currentChilds = zkClient.getChildren(parentPath);
}
return nodeChildsToUrls(parentPath, currentChilds);
return nodeChildsToUrls(url, parentPath, currentChilds);
} catch (Throwable e) {
throw new MotanFrameworkException(String.format("Failed to discover service %s from zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
}
Expand Down Expand Up @@ -287,18 +283,39 @@ protected void doUnavailable(URL url) {
}
}

private List<URL> nodeChildsToUrls(String parentPath, List<String> currentChilds) {
private List<URL> nodeChildsToUrls(URL url, String parentPath, List<String> currentChilds) {
List<URL> urls = new ArrayList<URL>();
if (currentChilds != null) {
for (String node : currentChilds) {
String nodePath = parentPath + MotanConstants.PATH_SEPARATOR + node;
String data = zkClient.readData(nodePath, true);
try {
URL url = URL.valueOf(data);
urls.add(url);
} catch (Exception e) {
LoggerUtil.warn(String.format("Found malformed urls from ZookeeperRegistry, path=%s", nodePath), e);
URL newurl = null;
if (StringUtils.isNotBlank(data)){
try {
newurl = URL.valueOf(data);
} catch (Exception e) {
LoggerUtil.warn(String.format("Found malformed urls from ZookeeperRegistry, path=%s", nodePath), e);
}
}
if (newurl == null){
newurl = url.createCopy();
String host = "";
int port = 80;
if (node.indexOf(":") > -1) {
String[] hp = node.split(":");
if(hp.length > 1){
host = hp[0];
try{
port = Integer.parseInt(hp[1]);
}catch (Exception ignore){}
}
}else{
host = node;
}
newurl.setHost(host);
newurl.setPort(port);
}
urls.add(newurl);
}
}
return urls;
Expand Down

0 comments on commit 141492a

Please sign in to comment.