Cuture.Extensions.Configuration.Nacos
1.1.0
dotnet add package Cuture.Extensions.Configuration.Nacos --version 1.1.0
NuGet\Install-Package Cuture.Extensions.Configuration.Nacos -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cuture.Extensions.Configuration.Nacos" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cuture.Extensions.Configuration.Nacos" Version="1.1.0" />
<PackageReference Include="Cuture.Extensions.Configuration.Nacos" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Cuture.Extensions.Configuration.Nacos --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Cuture.Extensions.Configuration.Nacos, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Cuture.Extensions.Configuration.Nacos@1.1.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Cuture.Extensions.Configuration.Nacos&version=1.1.0
#tool nuget:?package=Cuture.Extensions.Configuration.Nacos&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Cuture.Extensions.Configuration.Nacos
Intro
一个轻量的,基于 Nacos 的 Microsoft.Extensions.Configuration.IConfigurationSource 配置源库;
Background
项目考虑接入Nacos作为配置中心,然后发现Nacos官方有SDK - nacos-sdk-csharp,但是在尝试过程中遇到一些问题(比如Grpc端口号固定为http端口号+1000等)。。。本来想去修改官方SDK,结果完全理不清代码逻辑。。。那就参照着重写一个理想的针对当前使用场景的版本,顺便试着领悟一下官方SDK的设计思路。。。
Features
- 没有历史包袱 (当前仅支持
.net6+); - 便捷的Nacos地址配置,支持自定义Grpc端口号;
- 更轻便直观的配置 (个人觉得);
- 较少的包依赖 (可以在不引用Grpc包的情况下使用Http协议接入);
- 更方便调试 (没有异步Timer...支持SourceLink);
- 支持
阿里云ACM(支持KMS加密配置的自动解密,不依赖阿里云SDK包); - 支持添加配置获取处理中间件,自定义配置解密等;
Note
- 当前仅实现了
Nacos的配置读取功能,不支持服务发现(暂时没有需求...) - 没有实现任何
failover机制 (基于文件缓存的failover对于容器化运行环境不是很有必要...)
** 在Nacos2.0.1 with docker与阿里云ACM环境下开发,其它环境未测试。
** 单元测试代码暂时还没写...
如何使用
1. 安装包
# 使用Http客户端
Install-Package Cuture.Extensions.Configuration.Nacos
# 使用Grpc客户端
Install-Package Cuture.Extensions.Configuration.Nacos.Grpc
2. 配置
2.1 使用拓展方法进行配置
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(builder =>
{
builder.AddNacos(options =>
{
options.AddServerAddress("http://127.0.0.1:18848") //添加Nacos地址
.AddServerAddress("http://127.0.0.1:8848") //添加Nacos地址
//.UseGrpcClient() //使用Grpc客户端(需要安装Grpc包)
.UseHttpClient() //使用Http客户端
.WithUser("username", "password") //使用的用户
.SubscribeConfiguration("test1", "Cfg1") //订阅配置
.SubscribeConfiguration("test1", "Cfg2"); //订阅配置
})
});
2.2 使用配置文件进行配置
- 配置文件示例
{
"ClientType": "Grpc", //可选,客户端类型 - Http 或 Grpc(需要安装Grpc包,并使用对应的配置方法)默认为Http(阿里云ACM暂时不支持GRPC)
"Servers": [ //Nacos服务器地址列表(支持特殊定义的地址)
"http://127.0.0.1:18848/",
"http://127.0.0.1:8848/"
],
"Auth": { //认证信息节点
"User": { //用于Nacos登陆的用户信息
"Account": "username",
"Password": "password"
},
"ACS": { //用于阿里云ACS认证信息
"RegionId": "",
"AccessKeyId": "",
"AccessKeySecret": ""
}
},
"Configuration": { //Configuration配置
"DefaultNamespace": "test1", //默认的命名空间,当订阅项不指定 Namespace 时,使用此值
"DefaultGroup": "DEFAULT_GROUP", //默认组,当订阅项不指定 Group 时,使用此值。不设置时,默认为 DEFAULT_GROUP
"Subscriptions": [ //Configuration订阅列表
{
"Optional": false, //可选,是否为可选配置,默认为false
"ReloadOnChange": true, //可选,是否监听配置变更,默认为true
"DataId": "Cfg1", //配置的DataId
"Group": "DEFAULT_GROUP", //配置所在Group,当不设定时,使用 DefaultGroup
"Namespace": "test1" //配置所在命名空间,当不设定时,使用 DefaultNamespace
},
{
"DataId": "Cfg2"
}
]
},
"HealthCheckInterval": "00:00:05", //可选,客户端健康检查间隔
"ClientIPSubnet": "192.168.1.1/24", //可选,获取客户端IP时,将获取此值所在子网IP
"SpecifyClientIP": "127.0.0.1" //可选,指定客户端IP,优先级高于ClientIPSubnet
}
- 使用配置文件
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(builder =>
{
var configuration = new ConfigurationBuilder().AddJsonFile("nacos.json").Build();
//builder.AddNacos(configuration); //仅使用Http
builder.AddNacosWithGrpcClientAllowed(configuration); //允许使用Grpc
});
Nacos服务地址定义格式
- 标准的Uri格式
- 使用
Scheme和Fragment进行自定义端口号
eg:
http://127.0.0.1:8848/ //解析结果 - HttpPort: 8848 , GrpcPort: 9848 (GrpcPort为HttpPort+1000)
http://127.0.0.1:8848/#GrpcPort=8849 //解析结果 - HttpPort: 8848 , GrpcPort: 8849
http-grpc://127.0.0.1:9848/ //解析结果 - HttpPort: 8848 , GrpcPort: 9848 (HttpPort为GrpcPort-1000)
http-grpc://127.0.0.1:8849/#HttpPort=8848 //解析结果 - HttpPort: 8848 , GrpcPort: 8849
http-endpoint-acm://acm.aliyun.com/ //解析结果 - 使用阿里云acm
http-endpoint://127.0.0.1/list //解析结果 - 使用endpoint模式,内容仍然遵循此解析模式
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- Microsoft.Extensions.Configuration (>= 9.0.11)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.11)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.11)
-
net8.0
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
-
net9.0
- Microsoft.Extensions.Configuration (>= 9.0.11)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.11)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.11)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Cuture.Extensions.Configuration.Nacos:
| Package | Downloads |
|---|---|
|
HYS.Core.Config
Package Description |
|
|
Cuture.Extensions.Configuration.Nacos.Grpc
轻量的Microsoft.Extensions.Configuration的Nacos配置源 - Grpc客户端包 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.0 | 281 | 11/25/2025 |
| 1.0.4 | 53,499 | 12/12/2024 |
| 1.0.3 | 344 | 12/11/2024 |
| 1.0.2 | 49,093 | 5/21/2023 |
| 1.0.1 | 32,480 | 3/15/2022 |
| 1.0.0 | 13,223 | 8/19/2021 |
| 1.0.0-alpha-0013 | 441 | 7/18/2021 |
| 1.0.0-alpha-0012 | 656 | 7/14/2021 |
| 1.0.0-alpha-0011 | 549 | 7/2/2021 |
| 1.0.0-alpha-0010 | 467 | 7/1/2021 |
| 1.0.0-alpha-0009 | 490 | 6/30/2021 |
| 1.0.0-alpha-0008 | 553 | 6/19/2021 |
| 1.0.0-alpha-0007 | 480 | 6/17/2021 |
| 1.0.0-alpha-0006 | 464 | 6/17/2021 |
| 1.0.0-alpha-0005 | 451 | 6/17/2021 |
| 1.0.0-alpha-0004 | 431 | 6/17/2021 |
| 1.0.0-alpha-0003 | 474 | 6/17/2021 |
| 1.0.0-alpha-0002 | 473 | 6/16/2021 |
| 1.0.0-alpha-0001 | 481 | 6/9/2021 |