这是基于vue-vben-admin 模板适用于abp vNext的前端管理项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

15 KiB

内部API网关

**本文档中引用的文件** - [InternalApiGatewayModule.cs](file://gateways/web/LY.MicroService.ApiGateway/InternalApiGatewayModule.cs) - [InternalApiGatewayOptions.cs](file://gateways/web/LY.MicroService.ApiGateway/InternalApiGatewayOptions.cs) - [ocelot.json](file://gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/ocelot.json) - [yarp.json](file://gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.Gateway/yarp.json) - [AbpResponseMergeAggregator.cs](file://gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/Ocelot/Multiplexer/AbpResponseMergeAggregator.cs) - [ApiGatewayController.cs](file://gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/Controllers/ApiGatewayController.cs) - [LoadBalancerFinder.cs](file://gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/Utils/LoadBalancerFinder.cs) - [appsettings.json](file://gateways/web/LY.MicroService.ApiGateway/appsettings.json)

目录

  1. 简介
  2. 项目结构
  3. 核心组件
  4. 架构概述
  5. 详细组件分析
  6. 依赖分析
  7. 性能考虑
  8. 故障排查指南
  9. 结论

简介

内部API网关是微服务架构中的核心组件,负责服务间通信的路由配置、认证集成、请求聚合和安全防护。本文档详细解释了内部网关在微服务架构中的核心作用,包括路由配置、认证机制、请求聚合模式和安全策略。文档还详细说明了ocelot.json配置文件的结构,包括ReRoutes、GlobalConfiguration、AuthenticationOptions等关键配置项的含义和使用方法。同时描述了InternalApiGatewayModule的初始化流程和依赖注入配置,并为开发人员提供了监控指标、日志记录和故障排查指南。

项目结构

内部API网关位于gateways/internal目录下,主要由两个核心项目组成:LINGYUN.MicroService.Internal.ApiGatewayLINGYUN.MicroService.Internal.Gateway。前者基于Ocelot实现API网关功能,后者基于YARP实现反向代理功能。网关通过ocelot.json和yarp.json配置文件定义路由规则和集群配置,将外部请求转发到相应的微服务。

graph TD
subgraph "内部API网关"
Ocelot[Ocelot网关]
YARP[YARP反向代理]
end
subgraph "微服务集群"
AuthServer[认证服务器]
AdminAPI[管理API]
LocalizationAPI[本地化API]
MessagesAPI[消息API]
WebhooksAPI[Webhooks API]
TasksAPI[任务API]
PlatformAPI[平台API]
end
Client[客户端] --> Ocelot
Ocelot --> YARP
YARP --> AuthServer
YARP --> AdminAPI
YARP --> LocalizationAPI
YARP --> MessagesAPI
YARP --> WebhooksAPI
YARP --> TasksAPI
YARP --> PlatformAPI
style Ocelot fill:#f9f,stroke:#333
style YARP fill:#bbf,stroke:#333

图源

  • ocelot.json
  • yarp.json

节源

  • ocelot.json
  • yarp.json

核心组件

内部API网关的核心组件包括路由配置、聚合器、负载均衡器和安全防护机制。网关使用Ocelot作为主要的API网关框架,通过ocelot.json文件配置路由规则,将请求转发到后端微服务。同时,网关集成了YARP反向代理,通过yarp.json文件配置更复杂的路由和集群管理。InternalApiGatewayModule负责网关的初始化和依赖注入配置,AbpResponseMergeAggregator实现了响应聚合功能,LoadBalancerFinder提供了负载均衡器发现机制。

节源

  • InternalApiGatewayModule.cs
  • AbpResponseMergeAggregator.cs
  • LoadBalancerFinder.cs

架构概述

内部API网关采用分层架构设计,前端使用Ocelot处理API路由和聚合,后端使用YARP进行反向代理和负载均衡。网关通过ocelot.json配置文件定义路由规则,包括路径匹配、HTTP方法限制、请求头转换等。YARP通过yarp.json文件配置集群和路由,实现更灵活的流量管理。网关还集成了Swagger UI,提供统一的API文档访问入口。

graph TB
subgraph "客户端"
Browser[浏览器]
Mobile[移动设备]
end
subgraph "API网关层"
Ocelot[Ocelot网关]
YARP[YARP反向代理]
Swagger[Swagger UI]
end
subgraph "微服务层"
Auth[认证服务]
Admin[管理服务]
Localization[本地化服务]
Messages[消息服务]
Webhooks[Webhooks服务]
Tasks[任务服务]
Platform[平台服务]
end
Browser --> Ocelot
Mobile --> Ocelot
Ocelot --> YARP
YARP --> Auth
YARP --> Admin
YARP --> Localization
YARP --> Messages
YARP --> Webhooks
YARP --> Tasks
YARP --> Platform
Ocelot --> Swagger
style Ocelot fill:#f96,stroke:#333
style YARP fill:#69f,stroke:#333
style Swagger fill:#6f9,stroke:#333

图源

  • InternalApiGatewayModule.cs
  • ocelot.json
  • yarp.json

详细组件分析

路由配置分析

内部API网关使用ocelot.json文件配置路由规则,每个路由定义了上游路径模板、下游路径模板、HTTP方法等属性。路由配置支持路径参数、请求头转换、查询参数添加等功能。网关还支持基于服务名的路由发现,可以动态获取服务实例。

classDiagram
class RouteConfiguration {
+string DownstreamPathTemplate
+string UpstreamPathTemplate
+string[] UpstreamHttpMethod
+bool RouteIsCaseSensitive
+string DownstreamScheme
+DownstreamHostAndPort[] DownstreamHostAndPorts
+string Key
+int Priority
}
class ReRoute {
+RouteConfiguration Route
+LoadBalancerOptions LoadBalancer
+QoSOptions QoS
+RateLimitOptions RateLimit
+AuthenticationOptions Authentication
+HttpHandlerOptions HttpHandler
}
class GlobalConfiguration {
+string BaseUrl
+string[] ServiceDiscoveryProvider
+string[] RateLimitOptions
+string[] SecurityOptions
}
ReRoute --> RouteConfiguration : "包含"
ReRoute --> LoadBalancerOptions : "使用"
ReRoute --> QoSOptions : "使用"
ReRoute --> RateLimitOptions : "使用"
ReRoute --> AuthenticationOptions : "使用"
ReRoute --> HttpHandlerOptions : "使用"

图源

  • ocelot.json

节源

  • ocelot.json

认证集成机制

内部API网关通过AuthenticationOptions配置项集成认证机制,支持OAuth2、JWT等认证方式。网关在转发请求前验证用户身份,确保只有经过认证的请求才能访问后端服务。认证配置包括认证提供者键、允许的作用域等属性。

sequenceDiagram
participant Client as "客户端"
participant Gateway as "API网关"
participant AuthServer as "认证服务器"
participant Backend as "后端服务"
Client->>Gateway : 发送API请求
Gateway->>AuthServer : 验证JWT令牌
AuthServer-->>Gateway : 返回验证结果
alt 令牌有效
Gateway->>Backend : 转发请求
Backend-->>Gateway : 返回响应
Gateway-->>Client : 返回响应
else 令牌无效
Gateway-->>Client : 返回401错误
end

图源

  • InternalApiGatewayModule.cs

节源

  • InternalApiGatewayModule.cs

请求聚合模式

内部API网关通过自定义聚合器AbpResponseMergeAggregator实现请求聚合功能。当多个微服务需要同时响应时,网关将各个服务的响应结果合并为一个统一的响应。聚合器支持JSON对象合并,可以处理数组的并集操作。

flowchart TD
Start([开始]) --> ReceiveRequest["接收客户端请求"]
ReceiveRequest --> FindRoutes["查找匹配的路由"]
FindRoutes --> CallServices["并行调用多个后端服务"]
CallServices --> CheckErrors["检查服务调用错误"]
CheckErrors --> |有错误| HandleError["处理错误"]
CheckErrors --> |无错误| MergeResponses["合并响应结果"]
MergeResponses --> ReturnResponse["返回聚合响应"]
HandleError --> ReturnError["返回错误响应"]
ReturnResponse --> End([结束])
ReturnError --> End

图源

  • AbpResponseMergeAggregator.cs

节源

  • AbpResponseMergeAggregator.cs

安全防护策略

内部API网关实现了多层次的安全防护策略,包括IP白名单/黑名单、速率限制、QoS熔断等。通过SecurityOptions配置IP访问控制,RateLimitOptions配置请求频率限制,QoSOptions配置服务质量保障。

classDiagram
class SecurityOptions {
+string[] IPAllowedList
+string[] IPBlockedList
}
class RateLimitOptions {
+string[] ClientWhitelist
+bool EnableRateLimiting
+string Period
+double PeriodTimespan
+int Limit
}
class QoSOptions {
+int ExceptionsAllowedBeforeBreaking
+int DurationOfBreak
+int TimeoutValue
}
class HttpHandlerOptions {
+bool AllowAutoRedirect
+bool UseCookieContainer
+bool UseTracing
+bool UseProxy
+int MaxConnectionsPerServer
}
SecurityOptions --> ReRoute
RateLimitOptions --> ReRoute
QoSOptions --> ReRoute
HttpHandlerOptions --> ReRoute

图源

  • ocelot.json

节源

  • ocelot.json

依赖分析

内部API网关依赖于多个ABP框架模块和第三方库。核心依赖包括AbpAutofacModule(依赖注入)、AbpSwashbuckleModule(Swagger集成)、AbpAspNetCoreSerilogModule(日志记录)等。网关还依赖Ocelot框架进行API路由和YARP进行反向代理。

graph TD
InternalApiGateway[内部API网关] --> AbpAutofac[AbpAutofacModule]
InternalApiGateway --> AbpSwashbuckle[AbpSwashbuckleModule]
InternalApiGateway --> AbpSerilog[AbpAspNetCoreSerilogModule]
InternalApiGateway --> AbpMvcWrapper[AbpAspNetCoreMvcWrapperModule]
InternalApiGateway --> Ocelot[Ocelot]
InternalApiGateway --> YARP[YARP]
InternalApiGateway --> Redis[StackExchangeRedis]
style InternalApiGateway fill:#f96,stroke:#333
style AbpAutofac fill:#69f,stroke:#333
style AbpSwashbuckle fill:#69f,stroke:#333
style AbpSerilog fill:#69f,stroke:#333
style AbpMvcWrapper fill:#69f,stroke:#333
style Ocelot fill:#69f,stroke:#333
style YARP fill:#69f,stroke:#333
style Redis fill:#69f,stroke:#333

图源

  • InternalApiGatewayModule.cs

节源

  • InternalApiGatewayModule.cs

性能考虑

内部API网关在性能方面进行了多项优化。通过配置合理的超时值、连接池大小和负载均衡策略,确保网关在高并发场景下的稳定性。网关还支持缓存机制,可以缓存频繁访问的API响应,减少后端服务的压力。

graph TD
subgraph "性能优化"
Timeout[超时配置]
ConnectionPool[连接池]
LoadBalance[负载均衡]
Cache[缓存机制]
RateLimit[速率限制]
end
Timeout --> |减少等待时间| Performance
ConnectionPool --> |提高连接复用| Performance
LoadBalance --> |均衡负载| Performance
Cache --> |减少后端压力| Performance
RateLimit --> |防止滥用| Performance
Performance((性能提升))

图源

  • ocelot.json
  • yarp.json

故障排查指南

当遇到内部API网关问题时,可以按照以下步骤进行排查:

  1. 检查日志:查看网关的日志文件,定位错误信息
  2. 验证路由配置:确认ocelot.json中的路由配置是否正确
  3. 测试后端服务:直接访问后端服务,确认服务是否正常运行
  4. 检查网络连接:确保网关与后端服务之间的网络连接正常
  5. 验证认证配置:确认认证提供者和作用域配置是否正确
flowchart TD
Start([开始]) --> CheckLogs["检查网关日志"]
CheckLogs --> AnalyzeError["分析错误信息"]
AnalyzeError --> |配置错误| FixConfig["修正配置文件"]
AnalyzeError --> |服务不可用| CheckBackend["检查后端服务"]
AnalyzeError --> |网络问题| CheckNetwork["检查网络连接"]
AnalyzeError --> |认证失败| CheckAuth["检查认证配置"]
FixConfig --> TestFix["测试修复"]
CheckBackend --> RestartService["重启服务"]
CheckNetwork --> FixNetwork["修复网络"]
CheckAuth --> UpdateAuth["更新认证"]
TestFix --> VerifySuccess["验证成功"]
RestartService --> VerifySuccess
FixNetwork --> VerifySuccess
UpdateAuth --> VerifySuccess
VerifySuccess --> End([结束])

图源

  • appsettings.json

节源

  • appsettings.json

结论

内部API网关作为微服务架构的核心组件,提供了路由、认证、聚合和安全等关键功能。通过ocelot.json和yarp.json配置文件,可以灵活地定义路由规则和集群配置。InternalApiGatewayModule负责网关的初始化和依赖注入,确保各个组件正确加载。网关还提供了丰富的监控和日志功能,便于开发人员进行故障排查和性能优化。建议在生产环境中启用速率限制和QoS熔断机制,确保系统的稳定性和可靠性。