public NacosConfigurationProvider(NacosConfigurationSource configurationSource)
        {
            _configurationSource = configurationSource;

            _parser = configurationSource.NacosConfigurationParser ?? JsonConfigurationStringParser.Instance;

            _client = new NacosMsConfigClient(NullLoggerFactory.Instance, new NacosOptions
            {
                ServerAddresses = configurationSource.ServerAddresses,
                Namespace       = configurationSource.Tenant,
                AccessKey       = configurationSource.AccessKey,
                ClusterName     = configurationSource.ClusterName,
                ContextPath     = configurationSource.ContextPath,
                EndPoint        = configurationSource.EndPoint,
                DefaultTimeOut  = configurationSource.DefaultTimeOut,
                SecretKey       = configurationSource.SecretKey,
                Password        = configurationSource.Password,
                UserName        = configurationSource.UserName,
                ListenInterval  = 5000
            });

            _client.AddListenerAsync(new AddListenerRequest
            {
                DataId    = _configurationSource.DataId,
                Group     = _configurationSource.Group,
                Tenant    = _configurationSource.Tenant,
                Callbacks = new System.Collections.Generic.List <Action <string> >
                {
                    x => CallBackReload(x)
                }
            });
        }
        public NacosConfigurationProvider(NacosConfigurationSource configurationSource)
        {
            _configurationSource = configurationSource;

            _parser = configurationSource.NacosConfigurationParser;

            _client = new NacosMsConfigClient(NullLoggerFactory.Instance, new NacosOptions
            {
                ServerAddresses = configurationSource.ServerAddresses,
                Namespace       = configurationSource.Tenant,
                AccessKey       = configurationSource.AccessKey,
                ClusterName     = configurationSource.ClusterName,
                ContextPath     = configurationSource.ContextPath,
                EndPoint        = configurationSource.EndPoint,
                DefaultTimeOut  = configurationSource.DefaultTimeOut,
                SecretKey       = configurationSource.SecretKey,
                Password        = configurationSource.Password,
                UserName        = configurationSource.UserName,
                ListenInterval  = 20000
            });

            if (configurationSource.Listeners != null && configurationSource.Listeners.Any())
            {
                var tasks = new List <Task>();

                foreach (var item in configurationSource.Listeners)
                {
                    tasks.Add(_client.AddListenerAsync(new AddListenerRequest
                    {
                        DataId    = item.DataId,
                        Group     = item.Group,
                        Tenant    = configurationSource.Tenant,
                        Callbacks = new System.Collections.Generic.List <Action <string> >
                        {
                            x => CallBackReload(x, item.Optional)
                        }
                    }));
                }

                Task.WhenAll(tasks).ConfigureAwait(false);
            }
            else
            {
                _client.AddListenerAsync(new AddListenerRequest
                {
#pragma warning disable CS0618
                    DataId    = _configurationSource.DataId,
                    Group     = _configurationSource.Group,
                    Tenant    = _configurationSource.Tenant,
                    Callbacks = new System.Collections.Generic.List <Action <string> >
                    {
                        x => CallBackReload(x, _configurationSource.Optional)
                    }
#pragma warning restore CS0618
                });
            }
        }
Exemplo n.º 3
0
        public NacosV2ConfigurationProvider(NacosV2ConfigurationSource configurationSource)
        {
            _configurationSource = configurationSource;
            _parser       = configurationSource.NacosConfigurationParser;
            _configDict   = new ConcurrentDictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            _listenerDict = new Dictionary <string, MsConfigListener>();

            var options = Options.Create(new NacosSdkOptions()
            {
                ServerAddresses        = configurationSource.ServerAddresses,
                Namespace              = configurationSource.GetNamespace(),
                AccessKey              = configurationSource.AccessKey,
                ContextPath            = configurationSource.ContextPath,
                EndPoint               = configurationSource.EndPoint,
                DefaultTimeOut         = configurationSource.DefaultTimeOut,
                SecretKey              = configurationSource.SecretKey,
                Password               = configurationSource.Password,
                UserName               = configurationSource.UserName,
                ListenInterval         = 20000,
                ConfigUseRpc           = configurationSource.ConfigUseRpc,
                ConfigFilterAssemblies = configurationSource.ConfigFilterAssemblies,
                ConfigFilterExtInfo    = configurationSource.ConfigFilterExtInfo,
            });

            var nacosLoggerFactory = Nacos.Microsoft.Extensions.Configuration.NacosLog.NacosLoggerFactory.GetInstance(configurationSource.LoggingBuilder);

            _logger = nacosLoggerFactory.CreateLogger <NacosV2ConfigurationProvider>();
            _client = new NacosConfigService(nacosLoggerFactory, options);
            if (configurationSource.Listeners != null && configurationSource.Listeners.Any())
            {
                var tasks = new List <Task>();

                foreach (var item in configurationSource.Listeners)
                {
                    var listener = new MsConfigListener(item.DataId, item.Group, item.Optional, this, _logger);

                    tasks.Add(_client.AddListener(item.DataId, item.Group, listener));

                    _listenerDict.Add($"{item.DataId}#{item.Group}", listener);
                }

                Task.WaitAll(tasks.ToArray());
            }
            else
            {
#pragma warning disable CS0618
                var listener = new MsConfigListener(_configurationSource.DataId, _configurationSource.Group, _configurationSource.Optional, this, _logger);
                _client.AddListener(_configurationSource.DataId, _configurationSource.Group, listener);
                _listenerDict.Add($"{_configurationSource.DataId}#{_configurationSource.Group}", listener);
#pragma warning restore CS0618
            }
        }
        public NacosConfigurationProvider(NacosConfigurationSource configurationSource)
        {
            _configurationSource = configurationSource;

            _parser = configurationSource.NacosConfigurationParser ?? new JsonConfigurationParser();

            _configurationSource.NacosConfigClient.AddListenerAsync(new AddListenerRequest
            {
                DataId    = _configurationSource.DataId,
                Group     = _configurationSource.Group,
                Tenant    = _configurationSource.Tenant,
                Callbacks = new System.Collections.Generic.List <Action <string> >
                {
                    x => CallBackReload(x)
                }
            });
        }
        public static IConfigurationBuilder AddNacosV2Configuration(
            this IConfigurationBuilder builder,
            IConfiguration configuration,
            INacosConfigurationParser parser = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var source = new NacosV2ConfigurationSource();

            configuration.Bind(source);
            source.NacosConfigurationParser = parser ?? DefaultJsonConfigurationStringParser.Instance;

            return(builder.Add(source));
        }
        public NacosConfigurationProvider(NacosConfigurationSource configurationSource)
        {
            _configurationSource = configurationSource;

            _parser = configurationSource.NacosConfigurationParser ?? JsonConfigurationStringParser.Instance;

            _client = new NacosMsConfigClient(NullLoggerFactory.Instance, new NacosOptions
            {
                ServerAddresses = configurationSource.ServerAddresses,
                Namespace       = configurationSource.Tenant,
                ListenInterval  = 5000
            });

            _client.AddListenerAsync(new AddListenerRequest
            {
                DataId    = _configurationSource.DataId,
                Group     = _configurationSource.Group,
                Tenant    = _configurationSource.Tenant,
                Callbacks = new System.Collections.Generic.List <Action <string> >
                {
                    x => CallBackReload(x)
                }
            });
        }
        public NacosConfigurationProvider(NacosConfigurationSource configurationSource)
        {
            _configurationSource = configurationSource;
            _parser     = configurationSource.NacosConfigurationParser;
            _configDict = new ConcurrentDictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            var nacosLoggerFactory = Nacos.Microsoft.Extensions.Configuration.NacosLog.NacosLoggerFactory.GetInstance(configurationSource.LoggingBuilder);

            _logger = nacosLoggerFactory.CreateLogger <NacosConfigurationProvider>();

            _client = new NacosMsConfigClient(nacosLoggerFactory, new NacosOptions
            {
                ServerAddresses = configurationSource.ServerAddresses,
                Namespace       = configurationSource.Tenant,
                AccessKey       = configurationSource.AccessKey,
                ClusterName     = configurationSource.ClusterName,
                ContextPath     = configurationSource.ContextPath,
                EndPoint        = configurationSource.EndPoint,
                DefaultTimeOut  = configurationSource.DefaultTimeOut,
                SecretKey       = configurationSource.SecretKey,
                Password        = configurationSource.Password,
                UserName        = configurationSource.UserName,
                ListenInterval  = 20000
            });

            if (configurationSource.Listeners != null && configurationSource.Listeners.Any())
            {
                var tasks = new List <Task>();

                foreach (var item in configurationSource.Listeners)
                {
                    tasks.Add(_client.AddListenerAsync(new AddListenerRequest
                    {
                        DataId    = item.DataId,
                        Group     = item.Group,
                        Tenant    = configurationSource.Tenant,
                        Callbacks = new System.Collections.Generic.List <Action <string> >
                        {
                            x => CallBackReload($"{configurationSource.Tenant}#{item.Group}#{item.DataId}", x, item.Optional)
                        }
                    }));
                }

                Task.WhenAll(tasks).ConfigureAwait(false);
            }
            else
            {
                _client.AddListenerAsync(new AddListenerRequest
                {
#pragma warning disable CS0618
                    DataId    = _configurationSource.DataId,
                    Group     = _configurationSource.Group,
                    Tenant    = _configurationSource.Tenant,
                    Callbacks = new System.Collections.Generic.List <Action <string> >
                    {
                        x => CallBackReload($"{_configurationSource.Tenant}#{_configurationSource.Group}#{_configurationSource.DataId}", x, _configurationSource.Optional)
                    }
#pragma warning restore CS0618
                });
            }
        }