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 } }
/// <summary> /// 执行 /// </summary> /// <param name="stoppingToken"></param> /// <returns></returns> protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { // Add listener OcelotConfig.json" await _configClient.AddListener(OcelotConfig, OcelotConfigGroup, nacosConfigListener); await _configClient.AddListener(AppConfig, AppConfigGroup, AppConfigListener); } catch (Exception) { } }
static async Task ListenConfig(INacosConfigService svc, IListener listener) { var dataId = "demo-dateid"; var group = "demo-group"; // 添加监听 await svc.AddListener(dataId, group, listener); await Task.Delay(500); // 模拟配置变更,listener会收到变更信息 await PublishConfig(svc); await Task.Delay(500); await PublishConfig(svc); await Task.Delay(500); // 移除监听 await svc.RemoveListener(dataId, group, listener); // 配置变更后,listener不会收到变更信息 await PublishConfig(svc); }
/// <summary> /// 执行 /// </summary> /// <param name="stoppingToken"></param> /// <returns></returns> protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { // Add listener await _configClient.AddListener("blog.Core.Api.json", "DEFAULT_GROUP", nacosConfigListener); } catch (Exception) { } }
/// <summary> /// 执行 /// </summary> /// <param name="stoppingToken"></param> /// <returns></returns> protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { // Add listener await _configClient.AddListener("blog.Core.Api.json", "DEFAULT_GROUP", nacosConfigListener); } catch (Exception ex) { Serilog.Log.Information($"Nacos配置文件获取异常!!! " + ex.ToString()); } }