15 KiB
微服务架构
**本文档引用的文件** - [AuthServerModule.cs](file://aspnet-core/services/LY.MicroService.AuthServer/AuthServerModule.cs) - [BackendAdminHttpApiHostModule.cs](file://aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/BackendAdminHttpApiHostModule.cs) - [PlatformManagementHttpApiHostModule.cs](file://aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/PlatformManagementHttpApiHostModule.cs) - [yarp.json](file://gateways/web/LY.MicroService.ApiGateway/yarp.json) - [docker-compose.yml](file://docker-compose.yml) - [PlatformApplicationModule.cs](file://aspnet-core/modules/platform/LINGYUN.Platform.Application/LINGYUN/Platform/PlatformApplicationModule.cs) - [AbpDaprModule.cs](file://aspnet-core/framework/dapr/LINGYUN.Abp.Dapr/LINGYUN/Abp/Dapr/AbpDaprModule.cs) - [AbpDaprClientModule.cs](file://aspnet-core/framework/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/AbpDaprClientModule.cs) - [ILoadBalancerFinder.cs](file://gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/Utils/ILoadBalancerFinder.cs) - [LoadBalancerFinder.cs](file://gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/Utils/LoadBalancerFinder.cs) - [ApiGatewayController.cs](file://gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/Controllers/ApiGatewayController.cs)目录
简介
本项目采用微服务架构设计,将复杂的单体应用拆分为多个独立的、可独立部署的服务。这种架构模式提高了系统的可维护性、可扩展性和灵活性。项目通过Docker Compose进行容器化部署,使用YARP作为反向代理网关来管理服务间的通信。微服务之间通过HTTP API进行交互,同时利用CAP事件总线实现异步消息通信。项目还集成了Elasticsearch用于日志和审计数据的存储与查询,SkyWalking用于分布式追踪和性能监控。
项目结构
graph TD
subgraph "网关层"
APIG[API网关]
end
subgraph "微服务层"
AuthS[认证服务]
BackendA[后台管理服务]
PlatformM[平台管理服务]
LocalM[本地化管理服务]
RealM[实时消息服务]
TaskM[任务管理服务]
WebhookM[Webhook管理服务]
end
subgraph "数据层"
DB[(数据库)]
Redis[(Redis)]
ES[(Elasticsearch)]
end
subgraph "基础设施"
Dapr[Dapr]
CAP[CAP]
SkyWalking[SkyWalking]
end
APIG --> AuthS
APIG --> BackendA
APIG --> PlatformM
APIG --> LocalM
APIG --> RealM
APIG --> TaskM
APIG --> WebhookM
AuthS --> DB
AuthS --> Redis
AuthS --> ES
BackendA --> DB
BackendA --> Redis
BackendA --> ES
PlatformM --> DB
PlatformM --> Redis
PlatformM --> ES
AuthS --> CAP
BackendA --> CAP
PlatformM --> CAP
AuthS --> Dapr
BackendA --> Dapr
PlatformM --> Dapr
AuthS --> SkyWalking
BackendA --> SkyWalking
PlatformM --> SkyWalking
图示来源
- docker-compose.yml
- yarp.json
本节来源
- docker-compose.yml
- yarp.json
核心组件
本项目的核心组件包括AuthServer、BackendAdmin和Platform等微服务,每个服务都有明确的职责边界。AuthServer负责身份认证和授权,BackendAdmin提供后台管理功能,Platform则管理平台级的资源和配置。这些服务通过API网关对外提供统一的接口,内部通过CAP事件总线进行异步通信。服务间的数据一致性通过事件驱动架构保证,每个服务拥有独立的数据库,实现了数据的物理隔离。
本节来源
- AuthServerModule.cs
- BackendAdminHttpApiHostModule.cs
- PlatformManagementHttpApiHostModule.cs
架构概述
graph TB
subgraph "客户端"
UI[用户界面]
end
subgraph "网关层"
YARP[YARP网关]
end
subgraph "微服务层"
subgraph "认证与安全"
AuthS[AuthServer]
end
subgraph "管理服务"
BackendA[BackendAdmin]
PlatformM[Platform]
LocalM[LocalizationManagement]
end
subgraph "业务服务"
TaskM[TaskManagement]
WebhookM[WebhookManagement]
RealM[RealtimeMessage]
end
end
subgraph "数据与中间件"
DB[(数据库集群)]
Redis[(Redis缓存)]
ES[(Elasticsearch)]
CAP[(CAP消息总线)]
Dapr[Dapr边车]
end
UI --> YARP
YARP --> AuthS
YARP --> BackendA
YARP --> PlatformM
YARP --> LocalM
YARP --> TaskM
YARP --> WebhookM
YARP --> RealM
AuthS --> DB
AuthS --> Redis
AuthS --> ES
AuthS --> CAP
AuthS --> Dapr
BackendA --> DB
BackendA --> Redis
BackendA --> ES
BackendA --> CAP
BackendA --> Dapr
PlatformM --> DB
PlatformM --> Redis
PlatformM --> ES
PlatformM --> CAP
PlatformM --> Dapr
TaskM --> DB
TaskM --> Redis
TaskM --> ES
TaskM --> CAP
TaskM --> Dapr
WebhookM --> DB
WebhookM --> Redis
WebhookM --> ES
WebhookM --> CAP
WebhookM --> Dapr
RealM --> DB
RealM --> Redis
RealM --> ES
RealM --> CAP
RealM --> Dapr
图示来源
- docker-compose.yml
- yarp.json
详细组件分析
认证服务分析
classDiagram
class AuthServerModule {
+PreConfigureServices(context)
+ConfigureServices(context)
+OnApplicationInitialization(context)
}
class AbpModule {
<<interface>>
+PreConfigureServices(context)
+ConfigureServices(context)
+OnApplicationInitialization(context)
}
class AbpAccountApplicationModule {
+用户注册
+用户登录
+密码重置
}
class AbpOpenIddictApplicationModule {
+OAuth2.0支持
+OpenID Connect
+令牌管理
}
class AbpIdentityEntityFrameworkCoreModule {
+用户数据存储
+角色管理
+权限管理
}
class AbpAuditLoggingElasticsearchModule {
+审计日志记录
+日志查询
+日志分析
}
AuthServerModule --|> AbpModule : "继承"
AuthServerModule --> AbpAccountApplicationModule : "依赖"
AuthServerModule --> AbpOpenIddictApplicationModule : "依赖"
AuthServerModule --> AbpIdentityEntityFrameworkCoreModule : "依赖"
AuthServerModule --> AbpAuditLoggingElasticsearchModule : "依赖"
图示来源
- AuthServerModule.cs
本节来源
- AuthServerModule.cs
后台管理服务分析
classDiagram
class BackendAdminHttpApiHostModule {
+PreConfigureServices(context)
+ConfigureServices(context)
+OnApplicationInitialization(context)
}
class AbpModule {
<<interface>>
+PreConfigureServices(context)
+ConfigureServices(context)
+OnApplicationInitialization(context)
}
class AbpFeatureManagementApplicationModule {
+功能开关
+特性管理
+权限控制
}
class AbpPermissionManagementApplicationModule {
+权限管理
+角色权限
+用户权限
}
class AbpSettingManagementApplicationModule {
+设置管理
+配置存储
+动态配置
}
class AbpCachingManagementApplicationModule {
+缓存管理
+Redis集成
+缓存策略
}
BackendAdminHttpApiHostModule --|> AbpModule : "继承"
BackendAdminHttpApiHostModule --> AbpFeatureManagementApplicationModule : "依赖"
BackendAdminHttpApiHostModule --> AbpPermissionManagementApplicationModule : "依赖"
BackendAdminHttpApiHostModule --> AbpSettingManagementApplicationModule : "依赖"
BackendAdminHttpApiHostModule --> AbpCachingManagementApplicationModule : "依赖"
图示来源
- BackendAdminHttpApiHostModule.cs
本节来源
- BackendAdminHttpApiHostModule.cs
平台管理服务分析
classDiagram
class PlatformManagementHttpApiHostModule {
+PreConfigureServices(context)
+ConfigureServices(context)
+OnApplicationInitialization(context)
+OnPostApplicationInitializationAsync(context)
}
class AbpModule {
<<interface>>
+PreConfigureServices(context)
+ConfigureServices(context)
+OnApplicationInitialization(context)
}
class PlatformApplicationModule {
+平台应用逻辑
+业务服务
+领域模型
}
class AbpOssManagementApplicationModule {
+对象存储
+文件管理
+存储策略
}
class AbpNotificationsModule {
+通知服务
+消息推送
+事件订阅
}
class AbpFeaturesValidationRedisModule {
+特性验证
+Redis缓存
+限流控制
}
PlatformManagementHttpApiHostModule --|> AbpModule : "继承"
PlatformManagementHttpApiHostModule --> PlatformApplicationModule : "依赖"
PlatformManagementHttpApiHostModule --> AbpOssManagementApplicationModule : "依赖"
PlatformManagementHttpApiHostModule --> AbpNotificationsModule : "依赖"
PlatformManagementHttpApiHostModule --> AbpFeaturesValidationRedisModule : "依赖"
图示来源
- PlatformManagementHttpApiHostModule.cs
本节来源
- PlatformManagementHttpApiHostModule.cs
服务发现与负载均衡分析
sequenceDiagram
participant Client as "客户端"
participant YARP as "YARP网关"
participant LB as "负载均衡器"
participant Service as "微服务实例"
Client->>YARP : HTTP请求
YARP->>LB : 获取负载均衡器
LB-->>YARP : 负载均衡策略
YARP->>Service : 转发请求
Service-->>YARP : 响应结果
YARP-->>Client : 返回响应
Note over LB,Service : 支持多种负载均衡策略<br/>RoundRobin, LeastConnection等
图示来源
- ILoadBalancerFinder.cs
- LoadBalancerFinder.cs
- ApiGatewayController.cs
本节来源
- ILoadBalancerFinder.cs
- LoadBalancerFinder.cs
- ApiGatewayController.cs
依赖分析
graph TD
AuthS[AuthServer] --> EFCore[EntityFrameworkCore]
AuthS --> OpenIddict[OpenIddict]
AuthS --> CAP[CAP]
AuthS --> Redis[StackExchangeRedis]
AuthS --> SkyWalking[SkyWalking]
AuthS --> Elasticsearch[Elasticsearch]
BackendA[BackendAdmin] --> EFCore
BackendA --> CAP
BackendA --> Redis
BackendA --> SkyWalking
BackendA --> Elasticsearch
PlatformM[Platform] --> EFCore
PlatformM --> CAP
PlatformM --> Redis
PlatformM --> SkyWalking
PlatformM --> Minio[Minio]
PlatformM --> FileSystem[本地文件系统]
EFCore --> DB[(数据库)]
Redis --> Cache[(Redis服务器)]
Elasticsearch --> ES[(Elasticsearch集群)]
Minio --> ObjectStorage[(对象存储)]
AuthS --> Dapr[Dapr]
BackendA --> Dapr
PlatformM --> Dapr
图示来源
- AuthServerModule.cs
- BackendAdminHttpApiHostModule.cs
- PlatformManagementHttpApiHostModule.cs
本节来源
- AuthServerModule.cs
- BackendAdminHttpApiHostModule.cs
- PlatformManagementHttpApiHostModule.cs
性能考虑
本项目通过多种机制优化微服务架构的性能。首先,使用Redis作为分布式缓存,减少数据库访问频率。其次,通过CAP事件总线实现异步处理,提高系统的响应速度。再者,利用SkyWalking进行分布式追踪,帮助识别性能瓶颈。此外,每个微服务都配置了健康检查,确保服务的高可用性。API网关层实现了请求的负载均衡,避免单个服务实例过载。最后,通过Elasticsearch对日志进行高效存储和查询,便于性能分析和问题排查。
故障排除指南
当微服务出现故障时,可以按照以下步骤进行排查:首先检查Docker容器的运行状态,确保所有服务都正常启动。然后查看各服务的日志文件,特别是Elasticsearch中的审计日志,定位错误信息。如果涉及服务间通信问题,检查YARP网关的配置和路由规则。对于数据库相关问题,确认数据库连接字符串和迁移状态。若出现性能问题,使用SkyWalking分析调用链,找出瓶颈所在。对于缓存问题,检查Redis的连接和数据一致性。最后,确保Dapr边车和CAP消息总线正常运行,以保证事件驱动架构的正确性。
本节来源
- docker-compose.yml
- yarp.json
- AuthServerModule.cs
结论
本项目成功实现了基于微服务架构的复杂系统。通过将功能拆分为AuthServer、BackendAdmin、Platform等独立服务,提高了系统的可维护性和可扩展性。服务间通过API网关进行通信,利用CAP事件总线实现异步解耦。Dapr的集成增强了服务的分布式能力。尽管微服务架构带来了服务治理、数据一致性和运维复杂性等挑战,但通过合理的架构设计和技术选型,这些问题都得到了有效解决