SyZero.ApiGateway
1.1.4
dotnet add package SyZero.ApiGateway --version 1.1.4
NuGet\Install-Package SyZero.ApiGateway -Version 1.1.4
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.ApiGateway" Version="1.1.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.ApiGateway" Version="1.1.4" />
<PackageReference Include="SyZero.ApiGateway" />
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.ApiGateway --version 1.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SyZero.ApiGateway, 1.1.4"
#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.ApiGateway@1.1.4
#: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.ApiGateway&version=1.1.4
#tool nuget:?package=SyZero.ApiGateway&version=1.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SyZero.ApiGateway
基于 Ocelot 的 API 网关模块,支持服务路由、负载均衡和 Swagger 文档聚合。
📦 安装
dotnet add package SyZero.ApiGateway
✨ 特性
- 🚀 服务路由 - 基于 Ocelot 的动态路由配置
- ⚖️ 负载均衡 - 支持多种负载均衡策略
- 📖 Swagger 聚合 - 使用 MMLib.SwaggerForOcelot 聚合下游服务文档
- 🔄 熔断降级 - 集成 Polly 实现熔断和重试
🚀 快速开始
1. 配置 ocelot.json
{
"Routes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{ "Host": "localhost", "Port": 5001 }
],
"UpstreamPathTemplate": "/user-service/{everything}",
"UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:5000"
}
}
2. 注册服务
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加SyZero
builder.AddSyZero();
// 注册服务方式1 - 使用配置文件
builder.Configuration.AddJsonFile("ocelot.json");
builder.Services.AddOcelot();
// 注册服务方式2 - 添加 Polly 熔断
builder.Services.AddOcelot()
.AddPolly();
// 注册服务方式3 - 添加 Consul 服务发现
builder.Services.AddOcelot()
.AddConsul();
var app = builder.Build();
// 使用SyZero
app.UseSyZero();
app.UseOcelot().Wait();
app.Run();
3. 使用示例
// 网关自动转发请求,无需额外代码
// 客户端请求: GET http://gateway:5000/user-service/api/users/1
// 转发到: GET http://user-service:5001/api/users/1
📖 配置选项
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Routes |
array |
[] |
路由配置数组 |
DownstreamPathTemplate |
string |
"" |
下游服务路径模板 |
UpstreamPathTemplate |
string |
"" |
上游请求路径模板 |
LoadBalancerOptions.Type |
string |
"RoundRobin" |
负载均衡类型 |
📖 API 说明
路由配置
| 属性 | 说明 |
|---|---|
DownstreamHostAndPorts |
下游服务地址列表 |
UpstreamHttpMethod |
支持的 HTTP 方法 |
RateLimitOptions |
限流配置 |
QoSOptions |
服务质量配置 |
详细配置请参考 Ocelot 文档
🔧 高级用法
Swagger 文档聚合
builder.Services.AddSwaggerForOcelot(builder.Configuration);
app.UseSwaggerForOcelotUI(opt =>
{
opt.PathToSwaggerGenerator = "/swagger/docs";
});
自定义中间件
builder.Services.AddOcelot()
.AddDelegatingHandler<CustomHandler>();
⚠️ 注意事项
- 配置文件 - ocelot.json 必须单独配置,不能合并到 appsettings.json
- 服务发现 - 使用服务发现时需要配置对应的服务注册中心
- 熔断配置 - QoS 配置需要与 Polly 配合使用
📄 许可证
MIT License - 详见 LICENSE
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- MMLib.SwaggerForOcelot (>= 8.3.2)
- Ocelot (>= 23.4.2)
- Ocelot.Cache.CacheManager (>= 23.4.2)
- Ocelot.Provider.Consul (>= 23.4.2)
- Ocelot.Provider.Polly (>= 23.4.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SyZero.ApiGateway:
| Package | Downloads |
|---|---|
|
SyZero.Gateway
SyZero-Gateway |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.4 | 83 | 1/2/2026 |
| 1.1.4-dev.2 | 38 | 1/2/2026 |
| 1.1.4-dev.1 | 44 | 12/30/2025 |
| 1.1.3 | 86 | 12/30/2025 |
| 1.1.3-dev.6 | 44 | 12/30/2025 |
| 1.1.3-dev.3 | 111 | 1/19/2024 |
| 1.1.3-dev.2 | 174 | 11/3/2023 |
| 1.1.3-dev.1 | 190 | 3/21/2023 |
| 1.1.2 | 403 | 3/15/2023 |
| 1.1.2-dev.108.29344 | 188 | 3/15/2023 |
| 1.1.2-dev.108.28054 | 182 | 3/15/2023 |
| 1.1.2-dev.108.27487 | 184 | 3/15/2023 |
| 1.1.1 | 380 | 3/15/2023 |
| 1.1.1-dev.108.14980 | 182 | 3/15/2023 |
| 1.1.1-dev.108.13289 | 179 | 3/15/2023 |
| 1.1.1-dev.107.27144 | 182 | 3/14/2023 |
| 1.1.0 | 379 | 3/14/2023 |
| 1.1.0-workflow-dev.107.22552 | 172 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21746 | 177 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21506 | 172 | 3/14/2023 |
| 1.1.0-workflow-dev.107.20979 | 180 | 3/14/2023 |
| 1.1.0-dev.107.26364 | 177 | 3/14/2023 |
| 1.1.0-dev.107.24396 | 180 | 3/14/2023 |
| 1.1.0-dev.107.22787 | 183 | 3/14/2023 |
| 1.0.6 | 404 | 3/5/2022 |