dubbo默认使用Zookeeper作为注册中心。我们看下dubbo-remoting-zookeeper块的设计结构
对应的包结构
可以很清楚的看出内部逻辑:
利用ZookeeperTransporter创建一个zkClient。
@SPI("curator") //默认curator实现
public interface ZookeeperTransporter {
@Adaptive({"client", "transporter"}) //加载ZookeeperTransporter对应实现类
ZookeeperClient connect(URL url);// 创建ZookeeperClient对象。url 注册中心地址
}
复制代码
基于CuratorZookeeperTransporter创建
public class CuratorZookeeperTransporter implements ZookeeperTransporter {
@Override
public ZookeeperClient connect(URL url) {
return new CuratorZookeeperClient(url);
}
}
复制代码
CuratorZookeeperClient
基于curator的实现方式。它实现抽象类AbstractZookeeperClient。
public class CuratorZookeeperClient extends AbstractZookeeperClient<CuratorWatcher> {
private final CuratorFramework client;
public CuratorZookeeperClient(URL url) {
super(url);
try {
int timeout = url.getParameter(Constants.TIMEOUT_KEY, 5000);
//创建client对象
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
.connectString(url.getBackupAddress())//连接地址
.retryPolicy(new RetryNTimes(1, 1000))//重试策略,1 次,间隔 1000 ms
.connectionTimeoutMs(timeout);// 连接超时时间
String authority = url.getAuthority();
if (authority != null && authority.length() > 0) {
builder = builder.authorization("digest", authority.getBytes());
}
client = builder.build();
// 添加连接监听器
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState state) {
if (state == ConnectionState.LOST) {
CuratorZookeeperClient.this.stateChanged(StateListener.DISCONNECTED);
} else if (state == ConnectionState.CONNECTED) {
CuratorZookeeperClient.this.stateChanged(StateListener.CONNECTED);
} else if (state == ConnectionState.RECONNECTED) {
CuratorZookeeperClient.this.stateChanged(StateListener.RECONNECTED);
}
}
});// 启动 client
client.start();
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
复制代码
下面看下基于ZkClient的实现方式。
ZkclientZookeeperTransporter
public class ZkclientZookeeperTransporter implements ZookeeperTransporter {
@Override
public ZookeeperClient connect(URL url) {
return new ZkclientZookeeperClient(url);
}
}
复制代码
ZkclientZookeeperClient
public class ZkclientZookeeperClient extends AbstractZookeeperClient<IZkChildListener> {
private final ZkClientWrapper client;//包装
private volatile KeeperState state = KeeperState.SyncConnected;
public ZkclientZookeeperClient(URL url) {
super(url);
long timeout = url.getParameter(Constants.TIMEOUT_KEY, 30000L);
client = new ZkClientWrapper(url.getBackupAddress(), timeout);
client.addListener(new IZkStateListener() {
@Override
public void handleStateChanged(KeeperState state) throws Exception {
ZkclientZookeeperClient.this.state = state;
if (state == KeeperState.Disconnected) {
stateChanged(StateListener.DISCONNECTED);
} else if (state == KeeperState.SyncConnected) {
stateChanged(StateListener.CONNECTED);
}
}
@Override
public void handleNewSession() throws Exception {
stateChanged(StateListener.RECONNECTED);
}
});
client.start();
}
//ZkClientWrapper
public class ZkClientWrapper {
private long timeout;
private ZkClient client;
private volatile KeeperState state;
private CompletableFuture<ZkClient> completableFuture;
private volatile boolean started = false;
public ZkClientWrapper(final String serverAddr, long timeout) {
this.timeout = timeout;
completableFuture = CompletableFuture.supplyAsync(() -> new ZkClient(serverAddr, Integer.MAX_VALUE));
}
复制代码
AbstractZookeeperClient
ZookeeperClient的抽象实现类。一些抽象逻辑放在这个类中。