SyZero.Consul
1.1.4-dev.1
This is a prerelease version of SyZero.Consul.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package SyZero.Consul --version 1.1.4-dev.1
NuGet\Install-Package SyZero.Consul -Version 1.1.4-dev.1
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="SyZero.Consul" Version="1.1.4-dev.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.Consul" Version="1.1.4-dev.1" />
<PackageReference Include="SyZero.Consul" />
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 SyZero.Consul --version 1.1.4-dev.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SyZero.Consul, 1.1.4-dev.1"
#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 SyZero.Consul@1.1.4-dev.1
#: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=SyZero.Consul&version=1.1.4-dev.1&prerelease
#tool nuget:?package=SyZero.Consul&version=1.1.4-dev.1&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SyZero.Consul
基于 Consul 的服务注册、发现和配置中心集成组件。
📦 安装
dotnet add package SyZero.Consul
✨ 特性
- 🚀 服务注册 - 自动将服务注册到 Consul,支持健康检查
- 🔍 服务发现 - 从 Consul 获取服务列表,支持缓存优化
- ⚙️ 配置中心 - 从 Consul KV 存储读取配置,支持热更新
- 🔄 自动注销 - 应用程序停止时自动注销服务
- 🛡️ 高可用 - 支持 ACL Token 认证
🚀 快速开始
1. 配置 appsettings.json
{
"Server": {
"Name": "my-service",
"WanIp": "192.168.1.100",
"Port": 5000,
"Protocol": "HTTP",
"InspectInterval": 10
},
"Consul": {
"ConsulAddress": "http://localhost:8500",
"HealthCheck": "/health",
"Token": ""
}
}
2. 注册服务
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 方式一:从 AppConfig 读取配置(默认从 appsettings.json 的 "Consul" 节点读取)
builder.Services.AddConsul();
// 方式二:从 IConfiguration 读取配置
builder.Services.AddConsul(builder.Configuration);
// 方式三:从 IConfiguration 读取配置,并支持额外代码配置
builder.Services.AddConsul(builder.Configuration, options =>
{
options.Token = "your-acl-token";
});
// 方式四:使用配置委托
builder.Services.AddConsul(options =>
{
options.ConsulAddress = "http://localhost:8500";
options.HealthCheck = "/health";
});
// 方式五:直接传入配置对象
builder.Services.AddConsul(new ConsulServiceOptions
{
ConsulAddress = "http://localhost:8500",
HealthCheck = "/health"
});
var app = builder.Build();
// 使用 SyZero(会自动注册服务到 Consul)
app.UseSyZero();
app.Run();
3. 健康检查端点
确保添加健康检查端点:
app.MapGet("/health", () => Results.Ok("Healthy"));
📖 配置选项
ConsulServiceOptions
| 属性 | 类型 | 说明 |
|---|---|---|
ConsulAddress |
string |
Consul 服务地址 |
HealthCheck |
string |
健康检查路径 |
Token |
string |
ACL Token(可选) |
ServiceId |
string |
服务 ID(自动生成) |
Server 配置
| 属性 | 类型 | 说明 |
|---|---|---|
Name |
string |
服务名称 |
WanIp |
string |
服务 IP 地址 |
Port |
int |
服务端口 |
Protocol |
string |
协议类型(HTTP/HTTPS/GRPC) |
InspectInterval |
int |
健康检查间隔(秒) |
🔍 服务发现
使用 IServiceManagement
public class MyService
{
private readonly IServiceManagement _serviceManagement;
public MyService(IServiceManagement serviceManagement)
{
_serviceManagement = serviceManagement;
}
public async Task CallOtherService()
{
// 获取服务列表
var services = await _serviceManagement.GetService("other-service");
// 选择一个服务实例
var service = services.First();
// 构建请求地址
var url = $"{service.ServiceProtocol}://{service.ServiceAddress}:{service.ServicePort}/api/endpoint";
}
}
ServiceInfo 属性
| 属性 | 类型 | 说明 |
|---|---|---|
ServiceID |
string |
服务实例 ID |
ServiceName |
string |
服务名称 |
ServiceAddress |
string |
服务地址 |
ServicePort |
int |
服务端口 |
ServiceProtocol |
ProtocolType |
协议类型 |
⚙️ 配置中心
从 Consul KV 读取配置
var builder = WebApplication.CreateBuilder(args);
// 添加 Consul 配置源
builder.Configuration.AddConsul(cancellationToken);
var app = builder.Build();
自定义配置源
builder.Configuration.AddConsul("my-service-config", cancellationToken, source =>
{
source.ConsulClientConfiguration = config =>
{
config.Address = new Uri("http://localhost:8500");
config.Token = "your-acl-token";
};
source.Optional = true;
source.ReloadOnChange = true; // 启用配置热更新
source.ReloadDelay = 300; // 重新加载延迟(毫秒)
});
配置热更新
当 ReloadOnChange = true 时,配置变更会自动重新加载:
// 使用 IOptionsSnapshot 获取最新配置
public class MyService
{
private readonly IOptionsSnapshot<MyOptions> _options;
public MyService(IOptionsSnapshot<MyOptions> options)
{
_options = options;
}
public void DoSomething()
{
var currentValue = _options.Value.SomeSetting;
}
}
🔒 gRPC 服务支持
对于 gRPC 服务,健康检查会自动使用 gRPC 协议:
{
"Server": {
"Name": "my-grpc-service",
"WanIp": "192.168.1.100",
"Port": 5001,
"Protocol": "GRPC",
"InspectInterval": 10
}
}
🔗 与其他组件集成
与 SyZero.Feign 集成
// 自动从 Consul 发现服务并调用
[FeignClient("other-service")]
public interface IOtherServiceClient
{
[Get("/api/users/{id}")]
Task<User> GetUser(long id);
}
与 SyZero.DynamicGrpc 集成
// gRPC 服务自动注册到 Consul
builder.Services.AddDynamicGrpc();
builder.Services.AddConsul();
var app = builder.Build();
app.MapDynamicGrpcServices();
app.UseSyZero();
⚠️ 注意事项
- 健康检查 - 确保配置的健康检查端点可访问
- 网络 - 确保服务与 Consul 之间网络畅通
- ACL Token - 生产环境建议配置 ACL Token
- 缓存 - 服务发现结果会缓存 30 秒,减少 Consul 压力
📄 许可证
MIT License - 详见 LICENSE
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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 was computed. 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 was computed. 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. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- NConsul (>= 0.8.0)
- SyZero (>= 1.1.4-dev.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.5-dev.1 | 29 | 1/29/2026 |
| 1.1.4 | 93 | 1/2/2026 |
| 1.1.4-dev.2 | 43 | 1/2/2026 |
| 1.1.4-dev.1 | 50 | 12/30/2025 |
| 1.1.3 | 100 | 12/30/2025 |
| 1.1.3-dev.6 | 48 | 12/30/2025 |
| 1.1.3-dev.3 | 119 | 1/19/2024 |
| 1.1.3-dev.2 | 184 | 11/3/2023 |
| 1.1.3-dev.1 | 236 | 3/21/2023 |
| 1.1.2 | 389 | 3/15/2023 |
| 1.1.2-dev.108.29344 | 206 | 3/15/2023 |
| 1.1.2-dev.108.28054 | 191 | 3/15/2023 |
| 1.1.2-dev.108.27487 | 219 | 3/15/2023 |
| 1.1.1 | 360 | 3/15/2023 |
| 1.1.1-dev.108.14980 | 198 | 3/15/2023 |
| 1.1.1-dev.108.13289 | 197 | 3/15/2023 |
| 1.1.1-dev.107.27144 | 189 | 3/14/2023 |
| 1.1.0 | 358 | 3/14/2023 |
| 1.1.0-workflow-dev.107.22552 | 197 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21746 | 229 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21506 | 214 | 3/14/2023 |
| 1.1.0-workflow-dev.107.20979 | 192 | 3/14/2023 |
| 1.1.0-dev.107.26364 | 188 | 3/14/2023 |
| 1.1.0-dev.107.24396 | 198 | 3/14/2023 |
| 1.1.0-dev.107.22787 | 194 | 3/14/2023 |
| 1.0.6 | 466 | 3/5/2022 |