引用nuget包
winton.extensions.configuration.consul
配置
控制台程序
var configbuild = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true); string consuleurl="http://127.0.0.1:8500"; var cancellationTokenSource = new CancellationTokenSource(); configbuild.AddConsul($"{PlatformServices.Default.Application.ApplicationName}/appsettings.json", cancellationTokenSource.Token, options => { options.Optional = true; options.ReloadOnChange = true; options.OnLoadException = exceptionContext => { exceptionContext.Ignore = true; }; options.ConsulConfigurationOptions = cco => { cco.Address = new Uri(consuleurl); };//若不设置 默认http://127.0.0.1:8500 }); IConfiguration config = configbuild.Build();
|
asp.net core
public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) { var cancellationTokenSource = new CancellationTokenSource(); return WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; hostingContext.Configuration = config.Build(); string consul_url = hostingContext.Configuration["Consul_Url"]; config.AddConsul( $"{env.ApplicationName}/appsettings.{env.EnvironmentName}.json", cancellationTokenSource.Token, options => { options.Optional = true; options.ReloadOnChange = true; options.OnLoadException = exceptionContext => { exceptionContext.Ignore = true; }; options.ConsulConfigurationOptions = cco => { cco.Address = new Uri(consul_url); }; } ).AddEnvironmentVariables(); hostingContext.Configuration = config.Build(); }).UseUrls("http://localhost:5012") .UseStartup<Startup>(); }
|
consul配置
配置同物理json文件一致
使用
使用方式同IConfiguration,consul配置更改会自动更新IConfiguration里的数据
配置里获取实体类
nuget引用Microsoft.Extensions.Configuration.Binder
config.GetSection("FileCleanup:Config").Get< T >();
|