Galosys.Foundation.Yarp.Database
26.5.19.1
dotnet add package Galosys.Foundation.Yarp.Database --version 26.5.19.1
NuGet\Install-Package Galosys.Foundation.Yarp.Database -Version 26.5.19.1
<PackageReference Include="Galosys.Foundation.Yarp.Database" Version="26.5.19.1" />
<PackageVersion Include="Galosys.Foundation.Yarp.Database" Version="26.5.19.1" />
<PackageReference Include="Galosys.Foundation.Yarp.Database" />
paket add Galosys.Foundation.Yarp.Database --version 26.5.19.1
#r "nuget: Galosys.Foundation.Yarp.Database, 26.5.19.1"
#:package Galosys.Foundation.Yarp.Database@26.5.19.1
#addin nuget:?package=Galosys.Foundation.Yarp.Database&version=26.5.19.1
#tool nuget:?package=Galosys.Foundation.Yarp.Database&version=26.5.19.1
Galosys.Foundation.Yarp.Database
基于 EntityFrameworkCore 的 YARP 网关数据库持久化模块,提供路由模板管理和对接日志批量写入。
成熟度: 🟡 可用 — 功能完整,测试或文档可能不完善
安装
dotnet add package Galosys.Foundation.Yarp.Database
使用
builder.Services.AddYarpCore(builder.Environment);
builder.Services.AddYarbConfigProvider<DatabaseYarpConfigProvider>(); // 数据库配置
引入此模块后,DockingLog 日志将通过独立 BoundedChannel + BackgroundService 批量写入数据库,不再通过全局事件总线逐条写入。
路由模板自动应用 Gateway:Ha 高可用配置(健康检查、超时),详见 Galosys.Foundation.Yarp 模块 README。每条路由可通过 activity_timeout 字段覆盖默认超时。
批量写入机制
DockingLogBatchConsumer(BackgroundService)从独立BoundedChannel<DockingLog>消费日志- 触发条件:累积
BatchSize条(默认 200)或超过BatchIntervalMs(默认 500ms) - 批量操作:
db.AddRangeAsync(batch)+db.SaveChangesAsync() - Channel 满时策略:
DropOldest(丢弃最旧日志,不阻塞网关转发)
批量写入参数通过 Gateway:Metric 配置节控制,详见 Galosys.Foundation.Yarp 模块 README。
DockingLogEntityTypeConfiguration
DockingLog 实体的 EFCore 映射已从 Data Annotations 迁移到 Fluent API,由 DockingLogEntityTypeConfiguration 统一管理:
// 位于 Microsoft/EntityFrameworkCore/Metadata/Builders/DockingLogEntityTypeConfiguration.cs
// 继承 EntityTypeConfigurationBase<DockingLog, long>,自动获得:
// - snake_case 列名映射(无需手动 [Column] 标注)
// - TenantAppEntity 的复合索引(TenantId + AppId)
// - ICreator/ILastModifier 的字段长度约束
// 额外配置:
builder.ToTable("docking_log", "base"); // 表名 + Schema
builder.HasIndex(e => e.OccurredOn); // 事件时间索引
builder.HasIndex(e => e.ClusterId); // 集群标识索引
builder.HasIndex(e => new { e.OccurredOn, e.ClusterId }); // 复合索引
自动发现机制: DockingCenterDbContext 继承 DbContext<T>,OnModelCreating 通过 DependencyContext.Default.GetTypes() 扫描所有已加载程序集中的 IEntityTypeConfiguration<> 实现,自动应用配置。无需手动在 DbContext 中注册或重写 OnModelCreating。
此模式可作为跨模块使用 EntityTypeConfigurationBase 的示范——即使实体定义在 Galosys.Foundation.Yarp,配置类定义在 Galosys.Foundation.Yarp.Database,自动发现仍然生效。
数据库表结构
docking_log(对接日志)
CREATE TABLE docking_center.docking_log (
id bigint NOT NULL,
request_path varchar(64) NOT NULL, -- 请求路径
request_url varchar(64) NOT NULL, -- 请求完整 URL
request_method varchar(64) NULL, -- HTTP 方法
router_template_id bigint NULL, -- 关联路由模板 ID
execute_result bit DEFAULT 0 NOT NULL, -- 转发是否成功
occurred_on datetime2 NULL, -- 事件发生时间
status_code int DEFAULT 0 NOT NULL, -- HTTP 响应状态码
elapsed bigint DEFAULT 0 NOT NULL, -- 转发耗时(毫秒)
cluster_id varchar(64) NULL, -- YARP 目标集群标识
error_message varchar(2000) NULL, -- 转发错误信息
deleted bit DEFAULT 0 NOT NULL,
creator_id bigint DEFAULT 1 NULL,
creator_name varchar(64) DEFAULT '1' NOT NULL,
created_at datetime2 NOT NULL,
last_modifier_id bigint NULL,
last_modifier_name varchar(64) NULL,
last_modified_at datetime2 NULL,
tenant_id bigint NULL,
app_id bigint NULL,
CONSTRAINT PK_docking_center_docking_log PRIMARY KEY (id)
);
router_template(路由模板)
CREATE TABLE docking_center.router_template (
id bigint NOT NULL,
template_code varchar(64) NOT NULL,
request_path varchar(64) NOT NULL,
router_uri varchar(512) NULL,
path_prefix varchar(64) NULL,
path_remove_prefix varchar(64) NULL,
request_headers_copy bit DEFAULT 1 NOT NULL,
deleted bit DEFAULT 0 NOT NULL,
creator_id bigint DEFAULT 1 NULL,
creator_name varchar(64) DEFAULT '1' NOT NULL,
created_at datetime2 NOT NULL,
last_modifier_id bigint NULL,
last_modifier_name varchar(64) NULL,
last_modified_at datetime2 NULL,
remark varchar(128) NULL,
max_retries int DEFAULT 5 NOT NULL,
load_balancer varchar(64) NULL, -- 负载均衡策略(RoundRobin、LeastRequests 等)
activity_timeout int NULL, -- 请求超时毫秒数(NULL 时使用 Gateway:Ha:DefaultActivityTimeoutMs 默认值)
[source] varchar(255) NULL,
target varchar(255) NULL,
CONSTRAINT PK_docking_center_router_template PRIMARY KEY (id)
);
CREATE UNIQUE NONCLUSTERED INDEX UIDX_router_template_code
ON docking_center.router_template (template_code ASC);
路由模板 LoadBalancer 配置
router_template 表的 load_balancer 字段控制每条路由的负载均衡策略:
| 值 | 说明 |
|---|---|
NULL |
使用默认值 RoundRobin |
RoundRobin |
轮询(默认) |
LeastRequests |
最少请求 |
PowerOfTwoChoices |
二选一 |
| 其他 YARP 支持的策略 | 透传给 YARP |
路由模板 ActivityTimeout 配置
router_template 表的 activity_timeout 字段控制每条路由的请求超时:
| 值 | 说明 |
|---|---|
NULL |
使用 Gateway:Ha:DefaultActivityTimeoutMs 默认值(30s) |
5000 |
该路由超时 5s |
60000 |
该路由超时 60s |
需同步更新数据库表结构:
ALTER TABLE docking_center.router_template ADD activity_timeout int NULL;
BREAKING CHANGES
- v5.0:
DockingLog实体移除所有[Column]和[Table]Data Annotations,改由DockingLogEntityTypeConfiguration使用 Fluent API 配置(表名base.docking_log、列名 snake_case 自动映射、索引)。DockingCenterDbContext通过EntityTypeConfigurationBase自动发现机制加载配置,无需手动注册。此变更对数据库表结构无影响,仅改变 EFCore 映射方式。 - v2.0:
DockingLog实体移除了request_body、response_body、request_headers、request_query_params列,新增status_code、elapsed、cluster_id、error_message列。需同步更新数据库表结构。 - v2.0:
DockingLogCreateHandler(逐条写入)已替换为DockingLogBatchConsumer(批量写入)。 - v3.0:
router_template新增load_balancer字段,支持按路由配置负载均衡策略。需同步更新数据库表结构(ALTER TABLE docking_center.router_template ADD load_balancer varchar(64) NULL)。 - v4.0:
router_template新增activity_timeout字段,支持按路由配置请求超时。路由自动应用Gateway:Ha高可用配置(健康检查、超时)。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. |
-
net8.0
- Galosys.Foundation.EntityFrameworkCore (>= 26.5.19.1)
- Galosys.Foundation.Yarp (>= 26.5.19.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 |
|---|---|---|
| 26.5.19.1 | 95 | 5/19/2026 |
| 26.5.18.1 | 97 | 5/18/2026 |
| 26.5.15.1 | 89 | 5/15/2026 |
| 26.5.12.3 | 90 | 5/12/2026 |
| 26.4.27.1-rc1 | 95 | 4/26/2026 |
| 26.4.25.1-rc1 | 89 | 4/25/2026 |
| 26.4.22.2-rc7 | 93 | 4/22/2026 |
| 26.4.22.2-rc6 | 91 | 4/22/2026 |
| 26.4.22.2-rc4 | 82 | 4/22/2026 |
| 26.4.22.2-rc3 | 87 | 4/22/2026 |
| 26.4.12.8-rc1 | 95 | 4/12/2026 |
| 26.4.12.7-rc1 | 95 | 4/12/2026 |
| 26.1.30.1-rc1 | 114 | 1/30/2026 |
| 26.1.29.1 | 115 | 1/29/2026 |
| 26.1.28.5 | 111 | 1/28/2026 |
| 26.1.28.4 | 113 | 1/28/2026 |
| 26.1.28.2 | 112 | 1/28/2026 |
| 26.1.23.6 | 109 | 1/23/2026 |
| 26.1.21.1 | 111 | 1/21/2026 |
| 26.1.2.1 | 140 | 1/2/2026 |