SyZero.Consul
1.1.5-dev.1
This is a prerelease version of SyZero.Consul.
dotnet add package SyZero.Consul --version 1.1.5-dev.1
NuGet\Install-Package SyZero.Consul -Version 1.1.5-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.5-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.5-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.5-dev.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SyZero.Consul, 1.1.5-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.5-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.5-dev.1&prerelease
#tool nuget:?package=SyZero.Consul&version=1.1.5-dev.1&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SyZero.Consul
SyZero 框架的 Consul 服务注册与发现模块。
📦 安装
dotnet add package SyZero.Consul
✨ 特性
- 🚀 服务注册 - 自动注册服务到 Consul
- 🔍 服务发现 - 从 Consul 发现可用服务
- 💓 健康检查 - 内置健康检查支持
- ⚙️ 配置中心 - 支持从 Consul KV 读取配置
🚀 快速开始
1. 配置 appsettings.json
{
"Consul": {
"ConsulAddress": "http://localhost:8500",
"Token": "",
"ServiceName": "my-service",
"ServiceAddress": "localhost",
"ServicePort": 5000,
"HealthCheckUrl": "/health"
}
}
2. 注册服务
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加SyZero
builder.AddSyZero();
// 注册服务方式1 - 使用配置文件
builder.Services.AddConsul();
// 注册服务方式2 - 使用委托配置
builder.Services.AddConsul(options =>
{
options.ConsulAddress = "http://localhost:8500";
options.ServiceName = "my-service";
options.ServicePort = 5000;
});
// 注册服务方式3 - 指定配置节
builder.Services.AddConsul(builder.Configuration, "Consul");
var app = builder.Build();
// 使用SyZero
app.UseSyZero();
app.Run();
3. 使用示例
public class MyService
{
private readonly IServiceManagement _serviceManagement;
public MyService(IServiceManagement serviceManagement)
{
_serviceManagement = serviceManagement;
}
public async Task<string> GetServiceUrlAsync(string serviceName)
{
var service = await _serviceManagement.GetServiceAsync(serviceName);
return $"{service.Address}:{service.Port}";
}
}
📖 配置选项
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
ConsulAddress |
string |
"" |
Consul 服务地址 |
Token |
string |
"" |
Consul ACL Token |
ServiceName |
string |
"" |
服务名称 |
ServiceAddress |
string |
"" |
服务地址 |
ServicePort |
int |
0 |
服务端口 |
HealthCheckUrl |
string |
"/health" |
健康检查地址 |
📖 API 说明
IServiceManagement 接口
| 方法 | 说明 |
|---|---|
GetServiceAsync(serviceName) |
获取服务实例 |
GetServicesAsync(serviceName) |
获取所有服务实例 |
RegisterAsync() |
注册服务 |
DeregisterAsync() |
注销服务 |
所有方法都有对应的异步版本(带
Async后缀)
🔧 高级用法
从 Consul KV 读取配置
builder.Configuration.AddConsulConfiguration(options =>
{
options.Address = "http://localhost:8500";
options.Key = "config/my-service";
});
服务健康检查
app.MapHealthChecks("/health");
⚠️ 注意事项
- 网络连接 - 确保应用能访问 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.5-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 | 235 | 3/21/2023 |
| 1.1.2 | 388 | 3/15/2023 |
| 1.1.2-dev.108.29344 | 204 | 3/15/2023 |
| 1.1.2-dev.108.28054 | 190 | 3/15/2023 |
| 1.1.2-dev.108.27487 | 218 | 3/15/2023 |
| 1.1.1 | 359 | 3/15/2023 |
| 1.1.1-dev.108.14980 | 197 | 3/15/2023 |
| 1.1.1-dev.108.13289 | 196 | 3/15/2023 |
| 1.1.1-dev.107.27144 | 188 | 3/14/2023 |
| 1.1.0 | 357 | 3/14/2023 |
| 1.1.0-workflow-dev.107.22552 | 196 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21746 | 228 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21506 | 213 | 3/14/2023 |
| 1.1.0-workflow-dev.107.20979 | 190 | 3/14/2023 |
| 1.1.0-dev.107.26364 | 187 | 3/14/2023 |
| 1.1.0-dev.107.24396 | 197 | 3/14/2023 |
| 1.1.0-dev.107.22787 | 193 | 3/14/2023 |
| 1.0.6 | 466 | 3/5/2022 |