Skip to content

Commit

Permalink
make zookeeper registry configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
qdaxb committed Apr 27, 2016
1 parent 07b1048 commit 65d33de
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Registry getRegistry(URL url) {
registries.put(registryUri, registry);
return registry;
} catch (Exception e) {
throw new MotanFrameworkException("Create registry false for url:" + url, MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
throw new MotanFrameworkException("Create registry false for url:" + url, e, MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
} finally {
lock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,55 @@
package com.weibo.api.motan.registry.zookeeper;

import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.common.URLParamType;
import com.weibo.api.motan.exception.MotanFrameworkException;
import com.weibo.api.motan.registry.NotifyListener;
import com.weibo.api.motan.registry.support.FailbackRegistry;
import com.weibo.api.motan.rpc.URL;
import com.weibo.api.motan.util.LoggerUtil;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ZookeeperRegistry extends FailbackRegistry {
private final URL registryUrl;
private ZkClient zkClient;
private ConcurrentHashMap<URL, ConcurrentHashMap<NotifyListener, IZkChildListener>> urlListeners = new ConcurrentHashMap<URL, ConcurrentHashMap<NotifyListener, IZkChildListener>>();
private Object lock = new Object();

public ZookeeperRegistry(URL url, ZkClient client) {
public ZookeeperRegistry(URL url) {
super(url);
this.zkClient = client;
this.registryUrl = url;
}

public ConcurrentHashMap<URL, ConcurrentHashMap<NotifyListener, IZkChildListener>> getUrlListeners() {
return urlListeners;
}

private void tryInit() {

// ignore performance issues, keep it safe
if (zkClient == null) {
try {
synchronized (lock) {
if (zkClient == null) {
int timeout = registryUrl.getIntParameter(URLParamType.requestTimeout.getName(), URLParamType.requestTimeout.getIntValue());
int sessionTimeout = registryUrl.getIntParameter(URLParamType.registrySessionTimeout.getName(), URLParamType.registrySessionTimeout.getIntValue());
zkClient = new ZkClient(registryUrl.getParameter("address"), sessionTimeout, timeout);
}
}
} catch (ZkException e) {
LoggerUtil.error("[ZookeeperRegistry] fail to connect zookeeper, cause: " + e.getMessage());
throw e;
}
}
}

@Override
protected void doRegister(URL url) {
try {
Expand Down Expand Up @@ -201,6 +224,7 @@ private String toNodePath(URL url, ZkNodeType nodeType) {
}

private void createNode(URL url, ZkNodeType nodeType) {
tryInit();
String nodeTypePath = toNodeTypePath(url, nodeType);
if (!zkClient.exists(nodeTypePath)) {
zkClient.createPersistent(nodeTypePath, true);
Expand All @@ -209,6 +233,7 @@ private void createNode(URL url, ZkNodeType nodeType) {
}

private void removeNode(URL url, ZkNodeType nodeType) {
tryInit();
String nodePath = toNodePath(url, nodeType);
if (zkClient.exists(nodePath)) {
zkClient.delete(nodePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ public class ZookeeperRegistryFactory extends AbstractRegistryFactory {

@Override
protected Registry createRegistry(URL url) {
ZkClient client = null;
try {
client = new ZkClient(url.getParameter("address"));
} catch (ZkException e) {
LoggerUtil.error("[ZookeeperRegistry] fail to connect zookeeper, cause: " + e.getMessage());
}
return new ZookeeperRegistry(url, client);

return new ZookeeperRegistry(url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void setUp() throws Exception {
};

ZkClient mockZkClient = mockery.mock(ZkClient.class);
registry = new ZookeeperRegistry(zkUrl, mockZkClient);
registry = new ZookeeperRegistry(zkUrl);

url = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8001, "com.weibo.motan.demo.service.MotanDemoService");
clientUrl = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 0, "com.weibo.motan.demo.service.MotanDemoService");
Expand Down

0 comments on commit 65d33de

Please sign in to comment.