/// <summary>
 /// Initializes a new instance of the <see cref="ConfigServerConfigurationProvider"/> class from a <see cref="ConfigServerConfigurationSource"/>
 /// </summary>
 /// <param name="source">the <see cref="ConfigServerConfigurationSource"/> the provider uses when accessing the server.</param>
 public ConfigServerConfigurationProvider(ConfigServerConfigurationSource source)
     : this(source.DefaultSettings, source.LogFactory)
 {
     _ = source.Configuration as IConfigurationRoot;
     _configuration = WrapWithPlaceholderResolver(source.Configuration);
     ConfigurationSettingsHelper.Initialize(PREFIX, _settings, _configuration);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConfigServerConfigurationProvider"/> class from a <see cref="ConfigServerConfigurationSource"/>
 /// </summary>
 /// <param name="source">the <see cref="ConfigServerConfigurationSource"/> the provider uses when accessing the server.</param>
 /// <param name="httpClient">the httpClient to use</param>
 public ConfigServerConfigurationProvider(ConfigServerConfigurationSource source, HttpClient httpClient)
     : this(source.DefaultSettings, source.LogFactory)
 {
     _client        = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
     _              = source.Configuration as IConfigurationRoot;
     _configuration = WrapWithPlaceholderResolver(source.Configuration);
     ConfigurationSettingsHelper.Initialize(PREFIX, _settings, _configuration);
 }
        internal ConfigEnvironment LoadInternal(bool updateDictionary = true)
        {
            // Refresh settings with latest configuration values
            ConfigurationSettingsHelper.Initialize(PREFIX, _settings, _configuration);

            if (!_settings.Enabled)
            {
                _logger?.LogInformation("Config Server client disabled, did not fetch configuration!");
                return(null);
            }

            if (IsDiscoveryFirstEnabled())
            {
                var discoveryService = new ConfigServerDiscoveryService(_configuration, _settings, _loggerFactory);
                DiscoverServerInstances(discoveryService);
            }

            // Adds client settings (e.g spring:cloud:config:uri, etc) to the Data dictionary
            AddConfigServerClientSettings();

            if (_settings.RetryEnabled && _settings.FailFast)
            {
                var attempts = 0;
                var backOff  = _settings.RetryInitialInterval;
                do
                {
                    _logger?.LogInformation("Fetching config from server at: {0}", _settings.Uri);
                    try
                    {
                        return(DoLoad(updateDictionary));
                    }
                    catch (ConfigServerException e)
                    {
                        _logger?.LogInformation("Failed fetching config from server at: {0}, Exception: {1}", _settings.Uri, e);
                        attempts++;
                        if (attempts < _settings.RetryAttempts)
                        {
                            Thread.CurrentThread.Join(backOff);
                            var nextBackoff = (int)(backOff * _settings.RetryMultiplier);
                            backOff = Math.Min(nextBackoff, _settings.RetryMaxInterval);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }while (true);
            }
            else
            {
                _logger?.LogInformation("Fetching config from server at: {0}", _settings.Uri);
                return(DoLoad(updateDictionary));
            }
        }
Esempio n. 4
0
        public virtual IConfigurationProvider Build(IConfigurationBuilder builder)
        {
            ConfigurationBuilder config = new ConfigurationBuilder();

            foreach (IConfigurationSource s in builder.Sources)
            {
                if (s == this)
                {
                    break;
                }
                config.Add(s);
            }
            IConfigurationRoot existing = config.Build();

            ConfigurationSettingsHelper.Initialize(PREFIX, _settings, _environment, existing);
            return(this);
        }
        public virtual IConfigurationProvider Build(IConfigurationBuilder builder)
        {
            ConfigurationBuilder config = new ConfigurationBuilder();

            foreach (IConfigurationSource s in builder.Sources)
            {
                if (s == this)
                {
                    break;
                }

                config.Add(s);
            }

            _configuration = WrapWithPlaceholderResolver(config.Build());
            ConfigurationSettingsHelper.Initialize(PREFIX, _settings, _configuration);
            return(this);
        }
        private void OnSettingsChanged()
        {
            var existingPollingInterval = _settings.PollingInterval;

            if (_hasConfiguration)
            {
                ConfigurationSettingsHelper.Initialize(PREFIX, _settings, _configuration);
                _configuration.GetReloadToken().RegisterChangeCallback(_ => OnSettingsChanged(), null);
            }

            if (_settings.PollingInterval == TimeSpan.Zero)
            {
                _refreshTimer?.Dispose();
            }
            else if (_refreshTimer == null)
            {
                _refreshTimer = new Timer(_ => DoLoad(), null, TimeSpan.Zero, _settings.PollingInterval);
            }
            else if (existingPollingInterval != _settings.PollingInterval)
            {
                _refreshTimer.Change(TimeSpan.Zero, _settings.PollingInterval);
            }
        }