Nacos.NET
1.0.1
dotnet add package Nacos.NET --version 1.0.1
NuGet\Install-Package Nacos.NET -Version 1.0.1
<PackageReference Include="Nacos.NET" Version="1.0.1" />
<PackageVersion Include="Nacos.NET" Version="1.0.1" />
<PackageReference Include="Nacos.NET" />
paket add Nacos.NET --version 1.0.1
#r "nuget: Nacos.NET, 1.0.1"
#:package Nacos.NET@1.0.1
#addin nuget:?package=Nacos.NET&version=1.0.1
#tool nuget:?package=Nacos.NET&version=1.0.1
Nacos.NET
Nacos C# SDK 核心库,提供配置中心(INacosConfigService)和服务注册/发现(INacosNamingService)的基础实现。
快速开始
注册服务
// 同时注册配置服务与命名服务
builder.Services.AddNacosV2Config(x =>
{
x.ServerAddresses = new List<string> { "http://localhost:8848/" };
x.Namespace = "your-namespace";
x.UserName = "nacos";
x.Password = "nacos";
});
builder.Services.AddNacosV2Naming(x =>
{
x.ServerAddresses = new List<string> { "http://localhost:8848/" };
x.Namespace = "your-namespace";
x.UserName = "nacos";
x.Password = "nacos";
});
也可以从 appsettings.json 读取配置:
builder.Services.AddNacosV2Config(builder.Configuration);
builder.Services.AddNacosV2Naming(builder.Configuration);
对应 appsettings.json:
{
"NacosConfig": {
"ServerAddresses": ["http://localhost:8848/"],
"Namespace": "your-namespace",
"UserName": "nacos",
"Password": "nacos"
}
}
注入使用
public class MyService
{
private readonly INacosConfigService _config;
private readonly INacosNamingService _naming;
public MyService(INacosConfigService config, INacosNamingService naming)
{
_config = config;
_naming = naming;
}
public async Task<string> GetRemoteConfig()
{
// 读取配置
return await _config.GetConfig("dataId", "DEFAULT_GROUP", 3000);
}
public async Task<string> GetServiceAddress()
{
// 获取服务实例
var instance = await _naming.SelectOneHealthyInstance("my-service", "DEFAULT_GROUP");
return $"http://{instance.Ip}:{instance.Port}";
}
}
配置 Filter(扩展点)
ConfigFilterChainManager 支持通过 ConfigFilterAssemblies 动态加载 IConfigFilter 实现,可在配置读取时对内容进行处理(例如解密)。
自定义 Filter
实现 IConfigFilter 接口(可选实现 ILoggerAware 以接收日志实例):
public class MyConfigFilter : IConfigFilter, ILoggerAware
{
private ILogger _logger = NullLogger.Instance;
public void SetLogger(ILogger logger) => _logger = logger;
public void Init(NacosSdkOptions options)
{
// 从 options.ConfigFilterExtInfo 读取 Filter 配置
_logger.LogInformation("MyConfigFilter initialized.");
}
public void DoFilter(IConfigRequest request, IConfigResponse response, IConfigFilterChain chain)
{
chain.DoFilter(request, response); // 先执行链中其他 Filter
var content = (response as ConfigResponse)?.GetContent();
if (!string.IsNullOrEmpty(content))
{
// 对 content 做处理后写回
(response as ConfigResponse)?.SetContent(content);
}
}
public int GetOrder() => 0;
public string GetFilterName() => nameof(MyConfigFilter);
}
在 appsettings.json 中注册程序集名,SDK 会通过反射自动实例化 Filter:
{
"NacosConfig": {
"ConfigFilterAssemblies": ["MyAssembly.Name"],
"ConfigFilterExtInfo": "传给 Filter 的扩展参数(如加密密钥)"
}
}
项目已内置 AES 加密 Filter,见
Nacos.NET.Config.Encryption。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- Google.Protobuf (>= 3.35.0)
- Grpc.Net.Client (>= 2.80.0)
- Microsoft.Extensions.DependencyModel (>= 10.0.7)
- Microsoft.Extensions.Http (>= 10.0.7)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.7)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Nacos.NET:
| Package | Downloads |
|---|---|
|
Nacos.NET.AspNetCore
ASP.NET Core integration for Nacos.NET — one-line AddNacos() registration, health checks, and hosted service lifecycle management. |
|
|
Nacos.NET.Extensions.Configuration
Microsoft.Extensions.Configuration provider for Nacos — pulls remote config on startup and watches for changes. |
|
|
Nacos.NET.Config.Encryption
AES-256 config decryption filter for Nacos.NET — transparently decrypts cipher values fetched from Nacos configuration center. |
GitHub repositories
This package is not used by any popular GitHub repositories.