/// <summary>
 /// 清理指定的缓存依赖节点
 /// </summary>
 /// <param name="node">节点名称</param>
 /// <param name="clearChildren">是否递归清理所有子节点</param>
 /// <returns>Task</returns>
 public Task ClearDependentNode(string node, bool clearChildren = false)
 {
     if (clearChildren)
     {
         return(_zkClient.DeleteRecursiveAsync(node));
     }
     else
     {
         string value = GetDefaultValue();
         return(_zkClient.SetDataAsync(node, Encoding.UTF8.GetBytes(value)));
     }
 }
Beispiel #2
0
        public async Task PublishCommandMessage(string applicationCode, NetworkCommand command)
        {
            var ListenerPath = $"/Bucket.Listener/{applicationCode}";
            var existPath    = await _client.ExistsAsync(ListenerPath);

            if (!existPath)
            {
                await _client.CreateRecursiveAsync(ListenerPath, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(command)));
            }
            await _client.SetDataAsync(ListenerPath, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(command)));
        }
Beispiel #3
0
        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);
            }
        }
Beispiel #4
0
        public async Task SubscribeDataChangeTest()
        {
            var path = $"/{DateTime.Now:yyyy_MM_dd_HH_mm_ss_ff}";

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

                var types     = new List <Watcher.Event.EventType>();
                var waitEvent = new AutoResetEvent(false);

                await _client.SubscribeDataChange(path, (client, args) =>
                {
                    types.Add(args.Type);
                    waitEvent.Set();
                    return(Task.CompletedTask);
                });

                //created
                await _client.CreateEphemeralAsync(path, null);

                waitEvent.WaitOne(10000);

                //Assert.Equal(Watcher.Event.EventType.NodeCreated, types[0]);

                //modify
                await _client.SetDataAsync(path, new byte[] { 1 });

                waitEvent.WaitOne(10000);
                // Assert.Equal(Watcher.Event.EventType.NodeDataChanged, types[1]);

                //deleted
                await _client.DeleteAsync(path);

                waitEvent.WaitOne(10000);
                //  Assert.Equal(Watcher.Event.EventType.NodeDeleted, types[2]);
            }
            finally
            {
                if (await _client.ExistsAsync(path))
                {
                    await _client.DeleteAsync(path);
                }
            }
        }