dubbo有服务提供者和消费者,二者都会向zookeeper注册,那么他们两个向zookeeper注册了什么,又起到什么作用呢?
服务提供者向zookeeper注册了什么?
在服务启动时,会调用到一个方法:
org.apache.dubbo.registry.zookeeper.ZookeeperRegistry#doRegister
@Override
public void doRegister(URL url) {
try {
checkDestroyed();
zkClient.create(toUrlPath(url), url.getParameter(DYNAMIC_KEY, true));
} catch (Throwable e) {
throw new RpcException("Failed to register " + url + " to zookeeper " + getUrl() + ", cause: " + e.getMessage(), e);
}
}
org.apache.dubbo.remoting.zookeeper.AbstractZookeeperClient#create(String path, boolean ephemeral)
其中ephemeral表示是否创建临时节点,dubbo选择的是创建临时节点。
@Override
public void create(String path, boolean ephemeral) {
if (!ephemeral) {
if (persistentExistNodePath.contains(path)) {
return;
}
if (checkExists(path)) {
persistentExistNodePath.add(path);
return;
}
}
int i = path.lastIndexOf('/');
if (i > 0) {
create(path.substring(0, i), false);
}
if (ephemeral) {
createEphemeral(path);
} else {
createPersistent(path);
persistentExistNodePath.add(path);
}
}
服务消费者向zookeeper注册了什么?