/// <summary> /// 添加Consul配置中心 /// 需要在Program程序启动时,在配置web主机时调用,如IHostBuilder.ConfigureWebHostDefaults /// 关于key的前辍说明优先顺序说明: /// 1、从传过来的ServiceName查找 /// 2、从appsetting里查找ServiceName的key /// 3、使用当前应用程序名 /// </summary> /// <param name="webBuilder">web主机生成器</param> /// <param name="options">配置回调</param> /// <returns>web主机生成器</returns> public static IWebHostBuilder AddConsulConfigCenter(this IWebHostBuilder webBuilder, Action <ConfigCenterOptions> options) { var configOptions = new ConfigCenterOptions(); if (options != null) { options(configOptions); } webBuilder.ConfigureAppConfiguration((hostingContext, config) => { var configuration = config.Build(); if (string.IsNullOrWhiteSpace(configOptions.ServiceName)) { configOptions.ServiceName = configuration["ServiceName"]; } if (string.IsNullOrWhiteSpace(configOptions.ConsulAddress)) { configOptions.ConsulAddress = configuration["ConsulAddress"]; if (string.IsNullOrWhiteSpace(configOptions.ConsulAddress)) { throw new ArgumentNullException("Consul地址不能为空"); } } var envi = hostingContext.HostingEnvironment; UtilTool.CurrServiceName = configOptions.ServiceName; UtilTool.CurrApplicationName = envi.ApplicationName; var defaultKey = new List <string>(); if (configOptions.AutoLoadCommonKey) { defaultKey.Add("common.json"); } defaultKey.Add(ConfigCenterUtil.GetKeyPath("appsettings.json", configOptions.ServiceName)); defaultKey.Add(ConfigCenterUtil.GetKeyPath($"appsettings.{envi.EnvironmentName}.json", configOptions.ServiceName)); foreach (var k in defaultKey) { if (configOptions.Keys.Contains(k)) { continue; } configOptions.Keys.Add(k); } hostingContext.Configuration = config.AddConsulConfigCenter((o) => { o.FillFrom(configOptions); }) .Build(); }); return(webBuilder); }
/// <summary> /// 添加Consul配置中心 /// </summary> /// <param name="builder">配置生成器</param> /// <param name="options">选项回调</param> /// <returns>配置生成器</returns> public static IConfigurationBuilder AddConsulConfigCenter(this IConfigurationBuilder builder, Action <ConfigCenterOptions> options) { if (options == null) { throw new ArgumentNullException("回调配置不能为null"); } var configOptions = new ConfigCenterOptions(); options(configOptions); if (string.IsNullOrWhiteSpace(configOptions.ConsulAddress)) { throw new ArgumentNullException("Consul地址不能为空"); } if (configOptions.Keys.IsNullOrCount0()) { throw new ArgumentNullException("键不能为空"); } Action <IConsulConfigurationSource> fun = delegate(IConsulConfigurationSource source) { source.ConsulConfigurationOptions = cco => { // 配置Consul cco.Address = new Uri(configOptions.ConsulAddress); cco.Datacenter = configOptions.Datacenter; }; source.Optional = true; // 配置选项 source.ReloadOnChange = true; // 配置文件更新后重新加载 source.OnLoadException = exceptionContext => { exceptionContext.Ignore = true; }; // 忽略异常 }; foreach (var key in configOptions.Keys) { builder.AddConsul(key, fun); } return(builder); }
/// <summary> /// 填充来自配置 /// </summary> /// <param name="source">配置</param> public void FillFrom(ConfigCenterOptions source) { FillFrom(source as ConsulBasicOption); this.Keys = source.Keys; }