Ejemplo n.º 1
0
        private Task ConnectConfig(ConfigOption option)
        {
            async Task Start()
            {
                try
                {
                    await _configHub.StartAsync();

                    if (option.ConfigModules != null && option.ConfigModules.Length > 0)
                    {
                        //订阅配置更新
                        var model = string.IsNullOrWhiteSpace(option.Mode) ? "dev" : option.Mode.ToLower();
                        await _configHub.SendAsync("Subscript", option.ConfigModules, model);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"{_option.Url}:{ex.Message}");
                }
            };

            _retryTimer = new Timer(async obj =>
            {
                if (_configHub.State == HubConnectionState.Connected)
                {
                    _retryTimer.Dispose();
                    return;
                }
                _logger.LogInformation("retry connect");
                await Start();
            }, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        public async Task StartConfig(ConfigOption option)
        {
            _configHub = Connect(SpearType.Config);
            var provider = new SpearConfigProvider();

            ConfigHelper.Instance.Builder.Sources.Insert(0, provider);
            //订阅配置更新
            _configHub.On <IDictionary <string, object> >("UPDATE", configs =>
            {
                _logger.LogInformation(configs.ToJson());
                foreach (var config in configs)
                {
                    provider.LoadConfig(config.Key, config.Value);
                    ConfigChange?.Invoke(config);
                }
            });
            _configHub.Closed += async ex =>
            {
                _logger.LogInformation("connect closed");
                await ConnectConfig(option);
            };
            await ConnectConfig(option);
        }