在C# WebApi 中使用 Nacos02: 配置管理、服务管理实战
一、配置管理
1.添加一个新的命名空间
这里我都填写为publicdemo
2.C#代码配置启动
appsetting.json加上:
(nacos默认是8848端口)
"NacosConfig": {
"ServerAddresses": [ "http://localhost:8848" ], // Nacos 服务器地址
"Namespace": "publicdemo" // 命名空间
}
startup.cs加上(方式一和方式二任选):
public void ConfigureServices(IServiceCollection services)
{services.AddControllers();// 加载 Nacos 配置,启用 Nacos 服务发现//方式一: 需要先在appsetting.json中配置好services.AddNacosV2Config(Configuration, null, "NacosConfig");services.AddNacosV2Naming(Configuration, null, "NacosConfig");//方式二:直接将配置写在这里//services.AddNacosV2Config(x =>//{// x.ServerAddresses = new System.Collections.Generic.List<string> { "http://localhost:8848/" };// x.EndPoint = "";// x.Namespace = "publicdemo";//});//services.AddNacosV2Naming(x =>//{// x.ServerAddresses = new System.Collections.Generic.List<string> { "http://localhost:8848/" };// x.EndPoint =