private async Task CreateSubdirectory(IZookeeperClient zooKeeperClient, string path) { if (await zooKeeperClient.ExistsAsync(path)) { return; } if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation($"节点{path}不存在,将进行创建。"); } var childrens = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var nodePath = "/"; foreach (var children in childrens) { nodePath += children; if (!await zooKeeperClient.ExistsAsync(nodePath)) { await zooKeeperClient.CreateAsync(nodePath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } nodePath += "/"; } }
private void CreateNode(string node) { if (!_client.ExistsAsync(node).GetAwaiter().GetResult()) { _client.CreateAsync(node, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).GetAwaiter().GetResult(); } }
public static async Task CreateRecursiveAsync(this IZookeeperClient client, string path, byte[] data, List <ACL> acls, CreateMode createMode) { try { await client.CreateAsync(path, data, acls, createMode); } catch (KeeperException.NodeExistsException) { } catch (KeeperException.NoNodeException) { var parentDir = path.Substring(0, path.LastIndexOf('/')); await CreateRecursiveAsync(client, parentDir, null, acls, CreateMode.PERSISTENT); await client.CreateAsync(path, data, acls, createMode); } }
/// <summary> /// 递归创建该节点下的所有子节点和该节点本身。 /// </summary> /// <param name="client">ZooKeeper客户端。</param> /// <param name="path">节点路径。</param> /// <param name="getNodeData">获取当前被创建节点数据的委托。</param> /// <param name="getNodeAcls">获取当前被创建节点权限的委托。</param> public static async Task CreateRecursiveAsync(this IZookeeperClient client, string path, Func <string, byte[]> getNodeData, Func <string, List <ACL> > getNodeAcls) { var data = getNodeData(path); var acls = getNodeAcls(path); try { await client.CreateAsync(path, data, acls, CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException) { } catch (KeeperException.NoNodeException) { var parentDir = path.Substring(0, path.LastIndexOf('/')); await CreateRecursiveAsync(client, parentDir, getNodeData, getNodeAcls); await client.CreateAsync(path, data, acls, CreateMode.PERSISTENT); } }
private async Task <bool> SetRouteAsync(ServiceRouteDescriptor route, IZookeeperClient zooKeeperClient) { try { bool isSetRoute = false; _logger.LogDebug($"准备添加{route.ServiceDescriptor.Id}服务路由。"); var zooKeeperClients = await _zookeeperClientProvider.GetZooKeeperClients(); await CreateSubdirectory(zooKeeperClient, _configInfo.RoutePath); var path = _configInfo.RoutePath; if (!path.EndsWith("/")) { path += "/"; } var nodePath = $"{path}{route.ServiceDescriptor.Id}"; var nodeData = _serializer.Serialize(route); _logger.LogDebug($"服务路由内容为:{Encoding.UTF8.GetString(nodeData)}。"); if (!nodeWatchers.ContainsKey(nodePath)) { var watcher = nodeWatchers.GetOrAdd(nodePath, f => new NodeMonitorWatcher(path, async(oldData, newData) => await NodeChange(oldData, newData))); await zooKeeperClient.SubscribeDataChange(nodePath, watcher.HandleNodeDataChange); } if (!await zooKeeperClient.ExistsAsync(nodePath)) { _logger.LogDebug($"节点:{nodePath}不存在将进行创建。"); await zooKeeperClient.CreateAsync(nodePath, nodeData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else { var onlineData = (await zooKeeperClient.GetDataAsync(nodePath)).ToArray(); if (!DataEquals(nodeData, onlineData)) { await zooKeeperClient.SetDataAsync(nodePath, nodeData); _logger.LogDebug($"{nodePath}节点的缓存的服务路由与服务注册中心不一致,路由数据已被更新。"); isSetRoute = true; } } return(isSetRoute); } catch (Exception ex) { _logger.LogError($"{route.ServiceDescriptor.Id}服务的路由注册失败,原因:{ex.Message}"); return(false); } }
private async Task CreatePath(string path, bool isParent) { if (isParent) { var exists = await client.ExistsAsync(path); if (exists) { return; } } var index = path.LastIndexOf('/'); if (index > 0) { await CreatePath(path.Substring(0, index), true); } await client.CreateAsync(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, isParent?CreateMode.PERSISTENT : CreateMode.EPHEMERAL); }
/// <summary> /// 创建节点。 /// </summary> /// <param name="client">ZooKeeper客户端。</param> /// <param name="path">节点路径。</param> /// <param name="data">节点数据。</param> /// <param name="acls">权限。</param> /// <param name="isSequential">是否按顺序创建。</param> /// <returns>节点路径。</returns> /// <remarks> /// 因为使用序列方式创建节点zk会修改节点name,所以需要返回真正的节点路径。 /// </remarks> public static Task <string> CreatePersistentAsync(this IZookeeperClient client, string path, byte[] data, List <ACL> acls, bool isSequential = false) { return(client.CreateAsync(path, data, acls, isSequential ? CreateMode.PERSISTENT_SEQUENTIAL : CreateMode.PERSISTENT)); }
/// <summary> /// 创建短暂的节点。 /// </summary> /// <param name="client">ZooKeeper客户端。</param> /// <param name="path">节点路径。</param> /// <param name="data">节点数据。</param> /// <param name="acls">权限。</param> /// <param name="isSequential">是否按顺序创建。</param> /// <returns>节点路径。</returns> /// <remarks> /// 因为使用序列方式创建节点zk会修改节点name,所以需要返回真正的节点路径。 /// </remarks> public static Task <string> CreateEphemeralAsync(this IZookeeperClient client, string path, byte[] data, List <ACL> acls, bool isSequential = false) { return(client.CreateAsync(path, data, acls, isSequential ? CreateMode.EPHEMERAL_SEQUENTIAL : CreateMode.EPHEMERAL)); }
public static Task CreatePersistentAsync(this IZookeeperClient client, string path, byte[] data, List <ACL> acls) { return(client.CreateAsync(path, data, acls, CreateMode.PERSISTENT)); }
public static Task CreateEphemeralAsync(this IZookeeperClient client, string path, byte[] data, List <ACL> acls) { return(client.CreateAsync(path, data, acls, CreateMode.EPHEMERAL)); }