Пример #1
0
        public void Execute()
        {
            var configContent = _nacosConfigClient.GetConfigAsync(new GetConfigRequest
            {
                DataId = _nacosConfigurationSource.DataId,
                Group  = _nacosConfigurationSource.Group,
                Tenant = _nacosConfigurationSource.Tenant
            }).ConfigureAwait(false).GetAwaiter().GetResult();

            if (configContent == null)
            {
                var filename = "appsettings";

                if (_hostEnvironment.IsDevelopment())
                {
                    filename += $".{_hostEnvironment.EnvironmentName}";
                }

                var content = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, $"{filename}.ini"));

                _nacosConfigClient.PublishConfigAsync(new PublishConfigRequest
                {
                    DataId  = _nacosConfigurationSource.DataId,
                    Group   = _nacosConfigurationSource.Group,
                    Tenant  = _nacosConfigurationSource.Tenant,
                    Content = content,
                    Type    = "text"
                }).ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #2
0
        public async Task <string> Get([FromQuery] string key)
        {
            var res = await _configClient.GetConfigAsync(new GetConfigRequest
            {
                DataId = key,
                Group  = "DEFAULT_GROUP",
                //Tenant = "tenant"
            });

            return(string.IsNullOrWhiteSpace(res) ? "Not Found" : res);
        }
        protected internal async Task <string> RemoteLoadAsync(string dataId, string group, bool isRefreable)
        {
            // Get client if not already set
            if (_client == null)
            {
                _client = GetNacosClient(_settings, _clientFactory, _processor, _loggerFactory);
            }

            GetConfigRequest request = new GetConfigRequest()
            {
                DataId = dataId,
                Group  = group,
            };

            if (isRefreable)
            {
                AddListenerRequest addListenerRequest = new AddListenerRequest()
                {
                    DataId = dataId, Group = group, Callbacks = new List <Action <string> > {
                        x => { AddPropertySource(Deserialize(dataId, x)); }
                    }
                };

                _client.AddListenerAsync(addListenerRequest);
            }

            Exception error = null;

            try
            {
                var result = await _client.GetConfigAsync(request);

                return(result);
            }
            catch (Exception e)
            {
                error = e;
            }


            if (error != null)
            {
                throw error;
            }

            return(string.Empty);
        }
        public override void Load()
        {
            try
            {
                var config = _client.GetConfigAsync(new GetConfigRequest
                {
                    DataId = _configurationSource.DataId,
                    Group  = _configurationSource.Group,
                    Tenant = _configurationSource.Tenant
                }).ConfigureAwait(false).GetAwaiter().GetResult();

                var data = _parser.Parse(config);

                Data = data;
            }
            catch
            {
                if (!_configurationSource.Optional)
                {
                    throw;
                }
            }
        }
        public override void Load()
        {
            try
            {
                if (_configurationSource.Listeners != null && _configurationSource.Listeners.Any())
                {
                    var dict = new Dictionary <string, string>();

                    foreach (var listener in _configurationSource.Listeners)
                    {
                        try
                        {
                            var config = _client.GetConfigAsync(new GetConfigRequest
                            {
                                DataId = listener.DataId,
                                Group  = listener.Group,
                                Tenant = _configurationSource.Tenant
                            }).ConfigureAwait(false).GetAwaiter().GetResult();

                            var data = _parser.Parse(config);

                            foreach (var item in data)
                            {
                                dict[item.Key] = item.Value;
                            }
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Trace.WriteLine($"MS Config Query config error, {listener.DataId} ,{ex.Message}");
                            if (!listener.Optional)
                            {
                                throw;
                            }
                        }
                    }

                    Data = dict;
                }
                else
                {
#pragma warning disable CS0618
                    try
                    {
                        var config = _client.GetConfigAsync(new GetConfigRequest
                        {
                            DataId = _configurationSource.DataId,
                            Group  = _configurationSource.Group,
                            Tenant = _configurationSource.Tenant
                        }).ConfigureAwait(false).GetAwaiter().GetResult();

                        var data = _parser.Parse(config);

                        Data = data;
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Trace.WriteLine($"MS Config Query config error, {_configurationSource.DataId} ,{ex.Message}");
                        if (!_configurationSource.Optional)
                        {
                            throw;
                        }
                    }
#pragma warning restore CS0618
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine($"Load config error, {ex.Message}");
            }
        }
        public override void Load()
        {
            try
            {
                if (_configurationSource.Listeners != null && _configurationSource.Listeners.Any())
                {
                    var dict = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

                    foreach (var listener in _configurationSource.Listeners)
                    {
                        try
                        {
                            var config = _client.GetConfigAsync(new GetConfigRequest
                            {
                                DataId = listener.DataId,
                                Group  = listener.Group,
                                Tenant = _configurationSource.Tenant
                            }).ConfigureAwait(false).GetAwaiter().GetResult();

                            _configDict.AddOrUpdate($"{_configurationSource.Tenant}#{listener.Group}#{listener.DataId}", config, (x, y) => config);

                            var data = _parser.Parse(config);

                            foreach (var item in data)
                            {
                                dict[item.Key] = item.Value;
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger?.LogWarning(ex, "MS Config Query config error, dataid={0}, group={1}, tenant={2}", listener.DataId, listener.Group, listener.Tenant);
                            if (!listener.Optional)
                            {
                                throw;
                            }
                        }
                    }

                    Data = dict;
                }
                else
                {
#pragma warning disable CS0618
                    try
                    {
                        var config = _client.GetConfigAsync(new GetConfigRequest
                        {
                            DataId = _configurationSource.DataId,
                            Group  = _configurationSource.Group,
                            Tenant = _configurationSource.Tenant
                        }).ConfigureAwait(false).GetAwaiter().GetResult();

                        _configDict.AddOrUpdate($"{_configurationSource.Tenant}#{_configurationSource.Group}#{_configurationSource.DataId}", config, (x, y) => config);

                        var data = _parser.Parse(config);

                        Data = data;
                    }
                    catch (Exception ex)
                    {
                        _logger?.LogWarning(ex, "MS Config Query config error, dataid={0}, group={1}, tenant={2}", _configurationSource.DataId, _configurationSource.Group, _configurationSource.Tenant);
                        if (!_configurationSource.Optional)
                        {
                            throw;
                        }
                    }
#pragma warning restore CS0618
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "Load config error");
            }
        }