Exemplo n.º 1
0
        private async Task EnterServiceCommands()
        {
            if (_serviceCommands != null)
            {
                return;
            }

            var watcher = new ChildrenMonitorWatcher(_consul, _manager, _configInfo.CommandPath,
                                                     async(oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens),
                                                     (result) => ConvertPaths(result));

            if (_consul.KV.Keys(_configInfo.CommandPath).Result.Response?.Count() > 0)
            {
                var result = await _consul.GetChildrenAsync(_configInfo.CommandPath);

                var keys = await _consul.KV.Keys(_configInfo.CommandPath);

                var childrens = result;
                watcher.SetCurrentData(ConvertPaths(childrens).Select(key => $"{_configInfo.CommandPath}{key}").ToArray());
                _serviceCommands = await GetServiceCommands(keys.Response);
            }
            else
            {
                if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                {
                    _logger.LogWarning($"无法获取服务命令信息,因为节点:{_configInfo.CommandPath},不存在。");
                }
                _serviceCommands = new ServiceCommandDescriptor[0];
            }
        }
Exemplo n.º 2
0
        private async Task EnterRoutes()
        {
            if (_routes != null && _routes.Length > 0)
            {
                return;
            }
            Action <string[]> action = null;
            var client = await GetConsulClient();

            if (_configInfo.EnableChildrenMonitor)
            {
                var watcher = new ChildrenMonitorWatcher(GetConsulClient, _manager, _configInfo.RoutePath,
                                                         async(oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens),
                                                         (result) => ConvertPaths(result).Result);
                action = currentData => watcher.SetCurrentData(currentData);
            }
            if (client.KV.Keys(_configInfo.RoutePath).Result.Response?.Count() > 0)
            {
                var result = await client.GetChildrenAsync(_configInfo.RoutePath);

                var keys = await client.KV.Keys(_configInfo.RoutePath);

                var childrens = result;
                action?.Invoke(ConvertPaths(childrens).Result.Select(key => $"{_configInfo.RoutePath}{key}").ToArray());
                _routes = await GetRoutes(keys.Response);
            }
            else
            {
                if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                {
                    _logger.LogWarning($"无法获取路由信息,因为节点:{_configInfo.RoutePath},不存在。");
                }
                _routes = new ServiceRoute[0];
            }
        }
Exemplo n.º 3
0
        private async Task EnterRoutes()
        {
            if (_routes != null)
            {
                return;
            }
            _connectionWait.WaitOne();

            var watcher = new ChildrenMonitorWatcher(_zooKeeper, _configInfo.RoutePath,
                                                     async(oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens));

            if (await _zooKeeper.existsAsync(_configInfo.RoutePath, watcher) != null)
            {
                var result = await _zooKeeper.getChildrenAsync(_configInfo.RoutePath, watcher);

                var childrens = result.Children.ToArray();
                watcher.SetCurrentData(childrens);
                _routes = await GetRoutes(childrens);
            }
            else
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"无法获取路由信息,因为节点:{_configInfo.RoutePath},不存在。");
                }
                _routes = new ServiceRoute[0];
            }
        }
Exemplo n.º 4
0
        private async Task EnterCaches()
        {
            if (_serviceCaches != null && _serviceCaches.Length > 0)
            {
                return;
            }
            var client = await GetConsulClient();

            var watcher = new ChildrenMonitorWatcher(GetConsulClient, _manager, _configInfo.CachePath,
                                                     async(oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens),
                                                     result => ConvertPaths(result).Result);

            if (client.KV.Keys(_configInfo.CachePath).Result.Response?.Count() > 0)
            {
                var result = await client.GetChildrenAsync(_configInfo.CachePath);

                var keys = await client.KV.Keys(_configInfo.CachePath);

                var childrens = result;
                watcher.SetCurrentData(ConvertPaths(childrens).Result.Select(key => $"{_configInfo.CachePath}{key}")
                                       .ToArray());
                _serviceCaches = await GetCaches(keys.Response);
            }
            else
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"无法获取缓存信息,因为节点:{_configInfo.CachePath},不存在。");
                }
                _serviceCaches = new ServiceCache[0];
            }
        }
Exemplo n.º 5
0
            protected override void ProcessImpl(WatchedEvent watchedEvent)
            {
                var path    = Path;
                var watcher = new ChildrenMonitorWatcher(_zooKeeper, path, _action);

                switch (watchedEvent.Type)
                {
                case EventType.NodeCreated:
                case EventType.NodeChildrenChanged:
                    if (_zooKeeper.Exists(path, watcher) != null)
                    {
                        var childrens = _zooKeeper.GetChildren(path, watcher, new Stat());
                        _action(childrens);
                    }
                    else
                    {
                        _action(null);
                    }
                    break;

                case EventType.NodeDeleted:
                    _zooKeeper.Exists(path, watcher);
                    _action(null);
                    break;
                }
            }
        private async Task EnterRoutes(bool needUpdateFromServiceCenter = false)
        {
            if (_routes != null && _routes.Length > 0 && !needUpdateFromServiceCenter)
            {
                return;
            }
            Action <string[]> action = null;
            var client = await GetConsulClient();

            if (client != null)
            {
                await client.ExecuteLocked(GetLockerKey(), async() => {
                    //判断是否启用子监视器
                    if (_configInfo.EnableChildrenMonitor)
                    {
                        if (watcher == null)
                        {
                            //创建子监控类
                            watcher = new ChildrenMonitorWatcher(GetConsulClient, _manager, _configInfo.RoutePath,
                                                                 async(oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens),
                                                                 (result) => ConvertPaths(result).Result);
                        }

                        //对委托绑定方法
                        action = currentData => watcher.SetCurrentData(currentData);
                    }
                    if (client.KV.Keys(_configInfo.RoutePath).Result.Response?.Count() > 0)
                    {
                        var result    = await client.GetChildrenAsync(_configInfo.RoutePath);
                        var keys      = await client.KV.Keys(_configInfo.RoutePath);
                        var childrens = result;
                        //传参数到方法中
                        action?.Invoke(ConvertPaths(childrens).Result.Select(key => $"{_configInfo.RoutePath}{key}").ToArray());
                        //重新赋值到routes中
                        _routes = await GetRoutes(keys.Response);
                    }
                    else
                    {
                        if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                        {
                            _logger.LogWarning($"无法获取路由信息,因为节点:{_configInfo.RoutePath},不存在。");
                        }
                        _routes = new ServiceRoute[0];
                    }
                });
            }
            else
            {
                _logger.LogWarning("没有找到可用的服务注册中心");
            }
        }
Exemplo n.º 7
0
        private async Task EnterRoutes(bool needUpdateFromServiceCenter = false)
        {
            if (_routes != null && _routes.Length > 0 && !needUpdateFromServiceCenter)
            {
                return;
            }
            var zooKeeperClient = await _zookeeperClientProvider.GetZooKeeperClient();

            if (zooKeeperClient == null)
            {
                _logger.LogWarning("没有找到可用的服务注册中心");
                return;
            }
            try
            {
                if (watcher == null)
                {
                    watcher = new ChildrenMonitorWatcher(_configInfo.RoutePath,
                                                         async(oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens));
                }

                await zooKeeperClient.SubscribeChildrenChange(_configInfo.RoutePath, watcher.HandleChildrenChange);

                if (await zooKeeperClient.ExistsAsync(_configInfo.RoutePath))
                {
                    var childrens = (await zooKeeperClient.GetChildrenAsync(_configInfo.RoutePath)).ToArray();
                    if (watcher != null)
                    {
                        watcher.SetCurrentData(childrens);
                    }
                    _routes = await GetRoutes(childrens);
                }
                else
                {
                    if (_logger.IsEnabled(LogLevel.Warning))
                    {
                        _logger.LogWarning($"无法获取路由信息,因为节点:{_configInfo.RoutePath},不存在。");
                    }
                    _routes = new ServiceRoute[0];
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning($"从服务注册中心更新路由缓存错误:原因:{ex.Message}");
                return;
            }
        }
Exemplo n.º 8
0
        private async Task EnterRoutes()
        {
            if (_routes != null && _routes.Any())
            {
                return;
            }
            var zooKeeperClient = await _zookeeperClientProvider.GetZooKeeperClient();

            if (zooKeeperClient == null)
            {
                return;
            }
            watcher = new ChildrenMonitorWatcher(_configInfo.MqttRoutePath, async(oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens));

            await zooKeeperClient.SubscribeChildrenChange(_configInfo.MqttRoutePath, watcher.HandleChildrenChange);


            if (await zooKeeperClient.ExistsAsync(_configInfo.MqttRoutePath))
            {
                var childrens = await zooKeeperClient.GetChildrenAsync(_configInfo.MqttRoutePath);

                _routes = await GetRoutes(childrens);

                if (_routes != null)
                {
                    if (watcher != null)
                    {
                        watcher.SetCurrentData(childrens.ToArray());
                    }
                }
            }
            else
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"无法获取mqtt路由信息,因为节点:{_configInfo.RoutePath},不存在。");
                }
                _routes = new MqttServiceRoute[0];
            }
        }
Exemplo n.º 9
0
        private async Task EnterServiceCommands()
        {
            if (_serviceCommands != null && _serviceCommands.Any())
            {
                return;
            }
            var zooKeeperClient = await _zookeeperClientProvider.GetZooKeeperClient();

            if (zooKeeperClient == null)
            {
                return;
            }
            if (watcher == null)
            {
                watcher = new ChildrenMonitorWatcher(_configInfo.CommandPath,
                                                     async(oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens));
            }

            await zooKeeperClient.SubscribeChildrenChange(_configInfo.CommandPath, watcher.HandleChildrenChange);

            if (await zooKeeperClient.ExistsAsync(_configInfo.CommandPath))
            {
                var childrens = (await zooKeeperClient.GetChildrenAsync(_configInfo.CommandPath)).ToArray();

                if (watcher != null)
                {
                    watcher.SetCurrentData(childrens);
                }
                _serviceCommands = await GetServiceCommands(childrens);
            }
            else
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"无法获取服务命令信息,因为节点:{_configInfo.CommandPath},不存在。");
                }
                _serviceCommands = new ServiceCommandDescriptor[0];
            }
        }
Exemplo n.º 10
0
        private async Task EnterServiceCommands()
        {
            if (_serviceCommands != null)
            {
                return;
            }
            Action <string[]> action = null;
            var client = await GetConsulClient();

            if (_configInfo.EnableChildrenMonitor)
            {
                var watcher = new ChildrenMonitorWatcher(GetConsulClient, _manager, _configInfo.CommandPath,
                                                         async(oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens),
                                                         result => ConvertPaths(result));
                action = currentData => watcher.SetCurrentData(currentData);
            }

            if (client.KV.Keys(_configInfo.CommandPath).Result.Response?.Count() > 0)
            {
                var result = await client.GetChildrenAsync(_configInfo.CommandPath);

                var keys = await client.KV.Keys(_configInfo.CommandPath);

                var childrens = result;
                action?.Invoke(ConvertPaths(childrens).Select(key => $"{_configInfo.CommandPath}{key}").ToArray());
                _serviceCommands = await GetServiceCommands(keys.Response);
            }
            else
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"无法获取服务命令信息,因为节点:{_configInfo.CommandPath},不存在。");
                }
                _serviceCommands = new ServiceCommandDescriptor[0];
            }
        }
Exemplo n.º 11
0
        private async Task EnterRoutes()
        {
            if (_routes != null && _routes.Length > 0)
            {
                return;
            }
            Action <string[]> action = null;
            var client = await GetConsulClient();

            //判断是否启用子监视器
            if (_configInfo.EnableChildrenMonitor)
            {
                //创建子监控类
                var watcher = new ChildrenMonitorWatcher(GetConsulClient, _manager, _configInfo.RoutePath,
                                                         async(oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens),
                                                         (result) => ConvertPaths(result).Result);
                //对委托绑定方法
                action = currentData => watcher.SetCurrentData(currentData);
            }
            if (client.KV.Keys(_configInfo.RoutePath).Result.Response?.Count() > 0)
            {
                var result = await client.GetChildrenAsync(_configInfo.RoutePath);

                var keys = await client.KV.Keys(_configInfo.RoutePath);

                var childrens = result;
                //传参数到方法中
                action?.Invoke(ConvertPaths(childrens).Result.Select(key => $"{_configInfo.RoutePath}{key}").ToArray());
                //重新赋值到routes中
                _routes = await GetRoutes(keys.Response);
            }
            else
            {
                _routes = new ServiceRoute[0];
            }
        }
Exemplo n.º 12
0
            protected override void ProcessImpl(WatchedEvent watchedEvent)
            {
                var path = Path;
                var watcher = new ChildrenMonitorWatcher(_zooKeeper, path, _action);
                switch (watchedEvent.Type)
                {
                    case EventType.NodeCreated:
                    case EventType.NodeChildrenChanged:
                        if (_zooKeeper.Exists(path, watcher) != null)
                        {
                            var childrens = _zooKeeper.GetChildren(path, watcher, new Stat());
                            _action(childrens);
                        }
                        else
                        {
                            _action(null);
                        }
                        break;

                    case EventType.NodeDeleted:
                        _zooKeeper.Exists(path, watcher);
                        _action(null);
                        break;
                }
            }
Exemplo n.º 13
0
        private void EnterRoutes()
        {
            if (_routes != null)
                return;
            _connectionWait.WaitOne();

            var watcher = new ChildrenMonitorWatcher(_zooKeeper, _configInfo.RoutePath, newChildrens =>
            {
                if (_logger.IsEnabled(LogLevel.Information))
                    _logger.Information("路由节点发生了变更,将更新数据。");
                if (newChildrens == null)
                {
                    if (_logger.IsEnabled(LogLevel.Warning))
                        _logger.Warning("没有任何路由节点,路由数据将被清空。");
                    _routes = Enumerable.Empty<ServiceRoute>();
                    return;
                }
                //最新的节点数据。
                newChildrens = newChildrens.ToArray();
                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.Debug($"最新的节点信息:{string.Join(",", newChildrens)}");

                //旧的节点数据。
                var outChildrens = _routes.Select(i => i.ServiceDescriptor.Id).ToArray();

                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.Debug($"旧的节点信息:{string.Join(",", outChildrens)}");

                //计算出已被删除的节点。
                var deletedChildrens = outChildrens.Except(newChildrens).ToArray();
                //结算出新增的节点。
                var createdChildrens = newChildrens.Except(outChildrens).ToArray();

                if (_logger.IsEnabled(LogLevel.Information))
                    _logger.Information($"需要被删除的路由节点:{string.Join(",", deletedChildrens)}");
                if (_logger.IsEnabled(LogLevel.Information))
                    _logger.Information($"需要被添加的路由节点:{string.Join(",", createdChildrens)}");

                //删除无效的节点路由。
                _routes = _routes.Where(i => !deletedChildrens.Contains(i.ServiceDescriptor.Id));
                //获取新增的路由信息。
                var newRoutes = GetRoutes(createdChildrens);
                _routes = _routes.Concat(newRoutes);

                _routes = _routes.ToArray();
                if (_logger.IsEnabled(LogLevel.Information))
                    _logger.Information("路由数据更新成功。");
            });
            if (_zooKeeper.Exists(_configInfo.RoutePath, watcher) != null)
            {
                var childrens = _zooKeeper.GetChildren(_configInfo.RoutePath, watcher, new Stat());
                _routes = GetRoutes(childrens);
            }
            else
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                    _logger.Warning($"无法获取路由信息,因为节点:{_configInfo.RoutePath},不存在。");
                _routes = Enumerable.Empty<ServiceRoute>();
            }
        }
Exemplo n.º 14
0
            protected override async Task ProcessImpl(WatchedEvent watchedEvent)
            {
                var path = Path;
                var watcher = new ChildrenMonitorWatcher(_zooKeeper, path, _action);
                switch (watchedEvent.get_Type())
                {
                    case Event.EventType.NodeCreated:
                    case Event.EventType.NodeChildrenChanged:
                        if (await _zooKeeper.existsAsync(path, watcher) != null)
                        {
                            var result = await _zooKeeper.getChildrenAsync(path, watcher);
                            var childrens = result.Children;
                            _action(childrens);
                        }
                        else
                        {
                            _action(null);
                        }
                        break;

                    case Event.EventType.NodeDeleted:
                        await _zooKeeper.existsAsync(path, watcher);
                        _action(null);
                        break;
                }
            }
Exemplo n.º 15
0
        private async Task EnterRoutes()
        {
            if (_routes != null)
                return;
            _connectionWait.WaitOne();

            var watcher = new ChildrenMonitorWatcher(_zooKeeper, _configInfo.RoutePath,
                async (oldChildrens, newChildrens) => await ChildrenChange(oldChildrens, newChildrens));
            if (await _zooKeeper.existsAsync(_configInfo.RoutePath, watcher) != null)
            {
                var result = await _zooKeeper.getChildrenAsync(_configInfo.RoutePath, watcher);
                var childrens = result.Children.ToArray();
                watcher.SetCurrentData(childrens);
                _routes = await GetRoutes(childrens);
            }
            else
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                    _logger.LogWarning($"无法获取路由信息,因为节点:{_configInfo.RoutePath},不存在。");
                _routes = new ServiceRoute[0];
            }
        }
Exemplo n.º 16
0
        private void EnterRoutes()
        {
            if (_routes != null)
            {
                return;
            }
            _connectionWait.WaitOne();

            var watcher = new ChildrenMonitorWatcher(_zooKeeper, _configInfo.RoutePath, newChildrens =>
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.Information("路由节点发生了变更,将更新数据。");
                }
                if (newChildrens == null)
                {
                    if (_logger.IsEnabled(LogLevel.Warning))
                    {
                        _logger.Warning("没有任何路由节点,路由数据将被清空。");
                    }
                    _routes = Enumerable.Empty <ServiceRoute>();
                    return;
                }
                //最新的节点数据。
                newChildrens = newChildrens.ToArray();
                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.Debug($"最新的节点信息:{string.Join(",", newChildrens)}");
                }

                //旧的节点数据。
                var outChildrens = _routes.Select(i => i.ServiceDescriptor.Id).ToArray();

                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.Debug($"旧的节点信息:{string.Join(",", outChildrens)}");
                }

                //计算出已被删除的节点。
                var deletedChildrens = outChildrens.Except(newChildrens).ToArray();
                //结算出新增的节点。
                var createdChildrens = newChildrens.Except(outChildrens).ToArray();

                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.Information($"需要被删除的路由节点:{string.Join(",", deletedChildrens)}");
                }
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.Information($"需要被添加的路由节点:{string.Join(",", createdChildrens)}");
                }

                //删除无效的节点路由。
                _routes = _routes.Where(i => !deletedChildrens.Contains(i.ServiceDescriptor.Id));
                //获取新增的路由信息。
                var newRoutes = GetRoutes(createdChildrens);
                _routes       = _routes.Concat(newRoutes);

                _routes = _routes.ToArray();
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.Information("路由数据更新成功。");
                }
            });

            if (_zooKeeper.Exists(_configInfo.RoutePath, watcher) != null)
            {
                var childrens = _zooKeeper.GetChildren(_configInfo.RoutePath, watcher, new Stat());
                _routes = GetRoutes(childrens);
            }
            else
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.Warning($"无法获取路由信息,因为节点:{_configInfo.RoutePath},不存在。");
                }
                _routes = Enumerable.Empty <ServiceRoute>();
            }
        }