Beispiel #1
0
        public async Task CreateTest()
        {
            var path = $"/{Guid.NewGuid():N}";

            if (await _client.ExistsAsync(path))
            {
                await _client.DeleteAsync(path);
            }

            await _client.CreateEphemeralAsync(path, Encoding.UTF8.GetBytes("abc"));

            var data = (await _client.GetDataAsync(path)).ToArray();

            await _client.DeleteAsync(path);
        }
        public static async Task <IZookeeperClient> Register(ServiceInformation service)
        {
            IZookeeperClient zk = GetZooKeeper();

            await CheckOrCreateRoot(zk);

            string servicesPath = $"{Root}/{service.Key}";

            //todo 先获取后追加
            if (!await zk.ExistsAsync(servicesPath))
            {
                await zk.CreatePersistentAsync(servicesPath, GetData(string.Empty));
            }

            string serviceNodePaht = $"{servicesPath}/{service.Host}:{service.Port}";
            await zk.CreateEphemeralAsync(serviceNodePaht, GetData(service));

            return(zk);
        }
Beispiel #3
0
        private void CreateHostNode(string servicePath, ServicePublishInfo servicePublishInfo)
        {
            var hostName = Utils.GetHostName(servicePublishInfo.Host, servicePublishInfo.Port);
            var hostPath = GetHostPath(servicePath, hostName);
            var data     = MessagePackSerializer.Serialize(servicePublishInfo);

            if (!client.ExistsAsync(hostPath).Result)
            {
                try
                {
                    client.CreateEphemeralAsync(hostPath, data).Wait();
                }
                catch (AggregateException ex)
                {
                    if (!(ex.InnerException is NodeExistsException))
                    {
                        throw ex;
                    }
                }
            }
        }
 /// <summary>
 /// 创建短暂的节点。
 /// </summary>
 /// <param name="client">ZooKeeper客户端。</param>
 /// <param name="path">节点路径。</param>
 /// <param name="data">节点数据。</param>
 /// <param name="isSequential">是否按顺序创建。</param>
 /// <returns>节点路径。</returns>
 /// <remarks>
 /// 因为使用序列方式创建节点zk会修改节点name,所以需要返回真正的节点路径。
 /// </remarks>
 public static Task <string> CreateEphemeralAsync(this IZookeeperClient client, string path, byte[] data, bool isSequential = false)
 {
     return(client.CreateEphemeralAsync(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, isSequential));
 }
Beispiel #5
0
 public static Task CreateEphemeralAsync(this IZookeeperClient client, string path, byte[] data)
 {
     return(client.CreateEphemeralAsync(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE));
 }