protected override void LazyInitialize(string name, NameValueCollection config) { base.LazyInitialize(name, config); var sectionName = string.IsNullOrWhiteSpace(config["nacosConfig"]) ? "nacos" : config["nacosConfig"]; if (!ClientCache.TryGetValue(sectionName, out var cache)) { lock (ClientCache) { if (!ClientCache.TryGetValue(sectionName, out cache)) { var nacosConfig = NacosConfigurationSection.GetConfig(sectionName); if (nacosConfig == null) { LoggerFactory?.CreateLogger <NacosConfigurationBuilder>().LogWarning($"Can't found `{sectionName}` config"); Trace.TraceWarning($"Can't found `{sectionName}` config"); ClientCache[sectionName] = null; return; } if (nacosConfig.UseGrpc) { var client = new NacosConfigService(LoggerFactory ?? NullLoggerFactory.Instance, Options.Create(new NacosSdkOptions { ServerAddresses = nacosConfig.ServerAddresses.Split(';', ',').ToList(), Namespace = nacosConfig.Tenant, AccessKey = nacosConfig.AccessKey, ContextPath = nacosConfig.ContextPath, EndPoint = nacosConfig.EndPoint, DefaultTimeOut = nacosConfig.DefaultTimeOut, SecretKey = nacosConfig.SecretKey, Password = nacosConfig.Password, UserName = nacosConfig.UserName, ListenInterval = 20000 })); ClientCache[sectionName] = cache = Tuple.Create(nacosConfig, (object)client); } else { var client = new NacosMsConfigClient(LoggerFactory ?? NullLoggerFactory.Instance, new NacosOptions { ServerAddresses = nacosConfig.ServerAddresses.Split(';', ',').ToList(), Namespace = nacosConfig.Tenant, AccessKey = nacosConfig.AccessKey, ClusterName = nacosConfig.ClusterName, ContextPath = nacosConfig.ContextPath, EndPoint = nacosConfig.EndPoint, DefaultTimeOut = nacosConfig.DefaultTimeOut, SecretKey = nacosConfig.SecretKey, Password = nacosConfig.Password, UserName = nacosConfig.UserName, ListenInterval = 20000 }); ClientCache[sectionName] = cache = Tuple.Create(nacosConfig, (object)client); } if (nacosConfig.Listeners != null && nacosConfig.Listeners.Count > 0) { try { _ = Task.WhenAll(nacosConfig.Listeners .OfType <ConfigListener>() .Select(item => cache.Item2 is INacosConfigClient ncc ? ncc.AddListenerAsync(new AddListenerRequest { DataId = item.DataId, Group = item.Group, Tenant = nacosConfig.Tenant, Callbacks = new List <Action <string> > { x => CallBackReload($"{nacosConfig.Tenant}#{item.Group}#{item.DataId}", x) } }) : ((INacosConfigService)cache.Item2).AddListener(item.DataId, item.Group ?? ConstValue.DefaultGroup, new MsConfigListener($"{nacosConfig.Tenant}#{item.Group}#{item.DataId}")))); } catch (Exception ex) { LoggerFactory?.CreateLogger <NacosConfigurationBuilder>().LogError(ex, "AddListener fail."); Trace.TraceError("AddListener fail" + Environment.NewLine + ex); } } } } } _data = cache == null?Task.FromResult(Array.Empty <IDictionary <string, string> >()) : GetConfig(cache.Item1, cache.Item2); }
private static Task <IDictionary <string, string>[]> GetConfig(NacosConfigurationSection config, object client) => Task.WhenAll(config.Listeners.OfType <ConfigListener>()