SyZero.ApiGateway
1.1.9
dotnet add package SyZero.ApiGateway --version 1.1.9
NuGet\Install-Package SyZero.ApiGateway -Version 1.1.9
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.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.ApiGateway" Version="1.1.9" />
<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.9
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SyZero.ApiGateway, 1.1.9"
#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.9
#: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.9
#tool nuget:?package=SyZero.ApiGateway&version=1.1.9
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);
builder.AddSyZero();
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddSyZeroApiGateway(options =>
{
options.SwaggerTitle = "SyZero.Gateway";
}, builder.Configuration);
builder.Services.AddControllers();
var app = builder.Build();
app.UseSyZero();
app.UseRouting();
app.UseCors(SyZeroApiGatewayOptions.DefaultCorsPolicyName);
app.MapControllers();
await app.UseSyZeroApiGatewayAsync();
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.AddSyZeroApiGateway(options =>
{
options.SwaggerTitle = "SyZero.Gateway";
options.SwaggerGeneratorPath = "/swagger/docs";
}, builder.Configuration);
await app.UseSyZeroApiGatewayAsync();
自定义中间件
builder.Services.AddOcelot()
.AddDelegatingHandler<CustomHandler>();
⚠️ 注意事项
- 配置文件 - 建议将网关路由单独放在
ocelot.json或configuration.json - CORS -
SyZero:CorsOrigins会自动注册命名 CORS 策略,未配置时默认退化为匿名跨域 - 服务发现 - 启用 Consul 时会优先使用服务实例地址作为下游主机名
📄 许可证
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)
- Swashbuckle.AspNetCore (>= 6.4.0)
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.9 | 86 | 4/19/2026 |
| 1.1.9-dev.2 | 39 | 4/19/2026 |
| 1.1.9-dev.1 | 45 | 4/17/2026 |
| 1.1.8 | 88 | 4/17/2026 |
| 1.1.6 | 83 | 4/17/2026 |
| 1.1.6-dev.1 | 43 | 4/17/2026 |
| 1.1.5 | 102 | 4/13/2026 |
| 1.1.5-dev.3 | 48 | 4/13/2026 |
| 1.1.5-dev.2 | 62 | 2/11/2026 |
| 1.1.5-dev.1 | 59 | 1/29/2026 |
| 1.1.4 | 125 | 1/2/2026 |
| 1.1.4-dev.2 | 66 | 1/2/2026 |
| 1.1.4-dev.1 | 67 | 12/30/2025 |
| 1.1.3 | 120 | 12/30/2025 |
| 1.1.3-dev.6 | 67 | 12/30/2025 |
| 1.1.3-dev.3 | 131 | 1/19/2024 |
| 1.1.3-dev.2 | 196 | 11/3/2023 |
| 1.1.3-dev.1 | 209 | 3/21/2023 |
| 1.1.2 | 430 | 3/15/2023 |
| 1.0.0 | 101 | 4/17/2026 |
Loading failed