Nacos 是阿里巴巴推出来的一个项目,这是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。
1、管理 NuGet 包(N)...
nacos-sdk-csharp.Extensions.Configuration
2、appsettings.json
- {
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "AllowedHosts": "*",
- "Nacos": {
- "Listeners": [
- {
- "Optional": false,
- "DataId": "test",
- "Group": "DEFAULT_GROUP"
- }
- ],
- "Namespace": "public", //命名空间 Namespace 名称
- "ServerAddresses": [ "http://localhost:8848" ],
- "UserName": "nacos",
- "Password": "nacos",
- "AccessKey": "",
- "SecretKey": "",
- "ConfigUseRpc": false,
- "NamingUseRpc": false
- }
- }
ConfigUseRpc 和 NamingUseRpc 这2个参数必须存在
若用的是http协议,则都是false,
若用的是grpc协议,则都是true;
3、Nacos 新建配置
4、命名空间(Namespace),Data Id,Group
5、Program.cs
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Hosting;
-
- namespace NacosProject
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- CreateHostBuilder(args).Build().Run();
- }
-
- public static IHostBuilder CreateHostBuilder(string[] args) =>
- Host.CreateDefaultBuilder(args)
- .ConfigureAppConfiguration((context, builder) =>
- {
- var config = builder.Build();
- builder.AddNacosV2Configuration(config.GetSection("Nacos"));
- })
- .ConfigureWebHostDefaults(webBuilder =>
- {
- webBuilder.UseStartup
(); - });
- }
- }
6、Startup.cs
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
-
- namespace NacosProject
- {
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
-
- public IConfiguration Configuration { get; }
-
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- services.Configure
(Configuration.GetSection("AppSettings")); -
- services.AddControllers();
- }
-
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
- app.UseRouting();
-
- app.UseAuthorization();
-
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllers();
- });
- }
- }
- }
7、AppSettings.cs
- namespace NacosProject
- {
- public class AppSettings
- {
- public string Str { get; set; }
-
- public int num { get; set; }
- }
- }
8、ValuesController.cs
之所以 IOptions 没有获取到最新的配置,那是因为它的默认实现不会进行更新操作,也就是从启动到结束,它都是不会变的。
在有配置变更的情景,请尽可能不要用IOptions,用 IOptionsSnapshot 和 IOptionsMonitor 来替代!
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
-
- namespace NacosProject.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class ValuesController : ControllerBase
- {
- private readonly IConfiguration _configuration;
- private readonly AppSettings _settings;
- private readonly AppSettings _sSettings;
- private readonly AppSettings _mSettings;
-
- public ValuesController(IConfiguration configuration, IOptions
options, IOptionsSnapshot sOptions, IOptionsMonitor _mOptions ) - {
- _configuration = configuration;
- _settings = options.Value;
- _sSettings = sOptions.Value;
- _mSettings = _mOptions.CurrentValue;
- }
-
- [HttpGet]
- public IActionResult Get()
- {
- var connection = _configuration.GetConnectionString("Default");
- var version = _configuration["version"];
- var str1 = Newtonsoft.Json.JsonConvert.SerializeObject(_settings);
- var str2 = Newtonsoft.Json.JsonConvert.SerializeObject(_sSettings);
- var str3 = Newtonsoft.Json.JsonConvert.SerializeObject(_mSettings);
-
- return Ok(new
- {
- connection = connection,
- version = version,
- str1 = str1,
- str2 = str2,
- str3 = str3
- });
- }
- }
- }
9、浏览器
地址栏输入:http://localhost:6546/api/values
*
*
*