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
                    
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="Galosys.Foundation.Yarp.Database" Version="26.5.19.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.Yarp.Database" Version="26.5.19.1" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.Yarp.Database" />
                    
Project file
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 Galosys.Foundation.Yarp.Database --version 26.5.19.1
                    
#r "nuget: Galosys.Foundation.Yarp.Database, 26.5.19.1"
                    
#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 Galosys.Foundation.Yarp.Database@26.5.19.1
                    
#: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=Galosys.Foundation.Yarp.Database&version=26.5.19.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.Yarp.Database&version=26.5.19.1
                    
Install as a Cake Tool

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 字段覆盖默认超时。

批量写入机制

  • DockingLogBatchConsumerBackgroundService)从独立 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_bodyresponse_bodyrequest_headersrequest_query_params 列,新增 status_codeelapsedcluster_iderror_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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
Loading failed