长沙推广网站,建站平台社区,电子商务网站建设财务预算,网站可信认证必须做吗背景 之前老黄写过一篇《ASP.NET Core结合Nacos来完成配置管理和服务发现》简单介绍了如何让.NET Core程序接入Nacos#xff0c;之前的SDK里面更多的是对Nacos的Open API进行了封装以及对服务注册和发现的封装。配置这一块当时并没有过多的处理#xff0c;用起来有时感觉不会… 背景 之前老黄写过一篇《ASP.NET Core结合Nacos来完成配置管理和服务发现》简单介绍了如何让.NET Core程序接入Nacos之前的SDK里面更多的是对Nacos的Open API进行了封装以及对服务注册和发现的封装。配置这一块当时并没有过多的处理用起来有时感觉不会特别顺手所以将它和.NET Core的配置结合起来了让它用起来更简便。怎么个简便法呢可以说除了多添加一下provider其他的操作都是和最原始的一模一样你想用IConfiguration就用IConfiguration想用IOptions系列就用IOptions系列。更容易做到无缝迁移当然这个SDK出自老黄的手难免会有一些坑和bug这个就请各位多多包涵前提条件 启动Nacos Server最简单的方式用docker启动一个单机版的。docker-compose -f example/standalone-mysql-8.yaml up
创建一个.NET Core项目并安装相应nuget包这里将用ASP.NET Core Web Api做示例同时要安装下面的nuget包dotnet add package nacos-sdk-csharp-unofficial.Extensions.Configuration --version 0.2.5
更直接点直接修改csprojItemGroupPackageReference Includenacos-sdk-csharp-unofficial.Extensions.Configuration Version0.2.5 /
/ItemGroup
进行配置 打开Program.cs在CreateHostBuilder加入Nacos的provider配置都是Nacos的一些基础配置。public static IHostBuilder CreateHostBuilder(string[] args) Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((context, builder) {var c builder.Build();var dataId c.GetValuestring(nacosconfig:DataId);var group c.GetValuestring(nacosconfig:Group);var tenant c.GetValuestring(nacosconfig:Tenant);var optional c.GetValuebool(nacosconfig:Optional);var serverAddresses c.GetSection(nacosconfig:ServerAddresses).GetListstring();builder.AddNacosConfiguration(x {x.DataId dataId;x.Group group;x.Tenant tenant;x.Optional optional;x.ServerAddresses serverAddresses;});}).ConfigureWebHostDefaults(webBuilder {webBuilder.UseStartupStartup();});
同样的我们还要修改appsettings.json把Nacos的配置写进去主要是用来区分不同环境的配置来源。{Logging: {LogLevel: {Default: Warning,Microsoft: Warning,Microsoft.Hosting.Lifetime :Information} },nacosconfig:{Optional: false,DataId: msconfigapp,Group: ,Tenant: ca31c37e-478c-46ed-b7ea-d0ebaa080221,ServerAddresses: [localhost:8848]}
}
好了到这里用于配置Nacos相关的内容就结束了。接下来要做的就是在nacos控制台进行配置的维护。配置使用 新建一个配置添加一个对应的实体类public class AppSettings
{public string Str { get; set; }public int Num { get; set; }public Listint Arr { get; set; }public SubObj SubObj { get; set; }
}public class SubObj
{public string a { get; set; }
}
因为要验证IOptions模式所以要在Startup中加点代码public void ConfigureServices(IServiceCollection services)
{services.ConfigureAppSettings(Configuration.GetSection(AppSettings));services.AddControllers();
}
下面就是真正的使用了[ApiController]
[Route(api/[controller])]
public class ConfigController : ControllerBase
{private readonly IConfiguration _configuration;private readonly AppSettings _settings;private readonly AppSettings _sSettings;private readonly AppSettings _mSettings;public ConfigController(IConfiguration configuration,IOptionsAppSettings options,IOptionsSnapshotAppSettings sOptions,IOptionsMonitorAppSettings _mOptions){_configuration configuration;_settings options.Value;_sSettings sOptions.Value;_mSettings _mOptions.CurrentValue;}[HttpGet]public string Get(){string id Guid.NewGuid().ToString(N);Console.WriteLine($ begin {id} );var conn _configuration.GetConnectionString(Default);Console.WriteLine(${id} conn {conn});var version _configuration[version];Console.WriteLine(${id} version {version});var str1 Newtonsoft.Json.JsonConvert.SerializeObject(_settings);Console.WriteLine(${id} IOptions {str1});var str2 Newtonsoft.Json.JsonConvert.SerializeObject(_sSettings);Console.WriteLine(${id} IOptionsSnapshot {str2});var str3 Newtonsoft.Json.JsonConvert.SerializeObject(_mSettings);Console.WriteLine(${id} IOptionsMonitor {str3});Console.WriteLine($);return ok;}
}
从上面的代码看上去应该熟悉的不能再熟悉了吧这些配置的用法就是.NET Core里面提供的最原始的原汁原味。启动访问这个接口可以看到下面的输出。在控制台修改这个配置。再次访问可以发现除了IOptions之外都读取到了新的配置。之所以IOptions没有获取到最新的配置那是因为它的默认实现不会进行更新操作也就是从启动到结束它都是不会变的。在有配置变更的情景请尽可能不要用IOptions用IOptionsSnapshot和IOptionsMonitor来替代总结 这里介绍了如何让.NET Core更容易对接Nacos配置的方法希望对各位有所帮助。如果您对 nacos-sdk-charp 这个项目感兴趣也欢迎一起开发和维护这个项目。