in client/Apache.ShenYu.Client/Registers/ShenyuZookeeperRegister.cs [43:112]
public override Task Init(ShenyuOptions shenyuOptions)
{
if (string.IsNullOrEmpty(shenyuOptions.Register.ServerList))
{
throw new System.ArgumentException("serverList can not be null.");
}
var serverList = shenyuOptions.Register.ServerList;
this._shenyuOptions = shenyuOptions;
//props
var props = shenyuOptions.Register.Props;
int sessionTimeout = Convert.ToInt32(props.GetValueOrDefault(Constants.RegisterConstants.SessionTimeout, "3000"));
int connectionTimeout = Convert.ToInt32(props.GetValueOrDefault(Constants.RegisterConstants.ConnectionTimeout, "3000"));
int operatingTimeout = Convert.ToInt32(props.GetValueOrDefault(Constants.RegisterConstants.OperatingTimeout, "1000"));
ZkOptions zkConfig = new ZkOptions(serverList);
zkConfig.SetOperatingTimeout(operatingTimeout)
.SetSessionTimeout(sessionTimeout)
.SetConnectionTimeout(connectionTimeout);
props.TryGetValue(Constants.RegisterConstants.Digest, out string digest);
if (!string.IsNullOrEmpty(digest))
{
zkConfig.SetDigest(digest);
}
this._zkClient = new ZookeeperClient(zkConfig);
this._zkClient.SubscribeStatusChange(async (client, connectionStateChangeArgs) =>
{
switch (connectionStateChangeArgs.State)
{
case Watcher.Event.KeeperState.Disconnected:
case Watcher.Event.KeeperState.Expired:
if (client.WaitForKeeperState(Watcher.Event.KeeperState.SyncConnected,
zkConfig.ConnectionSpanTimeout))
{
foreach (var node in _nodeDataMap)
{
var existStat = await _zkClient.ExistsAsync(node.Key);
if (!existStat)
{
var pathArr = node.Key.Trim('/').Split('/');
//create parent
if (pathArr.Length > 1) {
var parentPath = node.Key.TrimEnd('/').Substring(0, node.Key.TrimEnd('/').LastIndexOf("/"));
await _zkClient.CreateWithParentAsync(parentPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
await _zkClient.CreateOrUpdateAsync(node.Key,
Encoding.UTF8.GetBytes(node.Value),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
_logger.LogInformation("zookeeper client register success: {}", node.Value);
}
}
}
else
{
_logger.LogError("zookeeper server disconnected and retry connect fail");
}
break;
case Watcher.Event.KeeperState.AuthFailed:
_logger.LogError("zookeeper server AuthFailed");
break;
case Watcher.Event.KeeperState.SyncConnected:
case Watcher.Event.KeeperState.ConnectedReadOnly:
break;
}
await Task.CompletedTask;
});
return Task.CompletedTask;
}