Nacos是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
这么优秀的分布式服务管理平台,怎么能不接入呢?
nacos的安装和使用这里就不细说了,可以参考网上教程和官方文档。https://nacos.io/zh-cn/docs/quick-start.html
我们以创建一个webapi项目为例手把手教你使用Nacos进行配置管理和服务注册
<PackageReference Include="nacos-sdk-csharp" Version="1.3.4" />
<PackageReference Include="nacos-sdk-csharp.AspNetCore" Version="1.3.4" />
<PackageReference Include="nacos-sdk-csharp.Extensions.Configuration" Version="1.3.4" />
<PackageReference Include="nacos-sdk-csharp.IniParser" Version="1.3.4" />
<PackageReference Include="nacos-sdk-csharp.YamlParser" Version="1.3.4" />
!
添加nacos配置
"NacosConfig": {
//配置中心
"Listeners": [
{
"Optional": false,
"DataId": "netTestConfig",
"Group": "zltest"
}
],
//命名空间GUID
"Namespace": "bf644fca-1276-415a-89de-428331e96a46",
"ServerAddresses": [ "172.16.9.88:8848" ],
//服务发现
"ServiceName": "netTest",
"GroupName": "zltest"
}
//注册服务
builder.Services.AddNacosAspNet(builder.Configuration,section: "NacosConfig");
//设置nacos配置
builder.Host.UseNacosConfig(section: "NacosConfig", parser: null, logAction: null);
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"NacosConfig": {
//配置中心
"Listeners": [
{
"Optional": false,
"DataId": "netTestConfig",
"Group": "zltest"
}
],
//命名空间GUID
"Namespace": "bf644fca-1276-415a-89de-428331e96a46",
"ServerAddresses": [ "172.16.9.88:8848" ],
//服务发现
"ServiceName": "netTest",
"UserName": "nacos",
"Password": "nacos",
"GroupName": "zltest"
},
"DbConfig":{
"DbType": 0,
//数据库连接字符串
"ConnectionString": "Data Source=localhost;port=3306;Initial Catalog=TNBLOG;uid=root;pwd=1234567;CharSet=utf8mb4;Allow User Variables=true;",
"IsAutoCloseConnection": true
}
}
启动服务
服务列表中显示我们的服务:nettest
接口返回nacos上的配置