ABP VNext + 多数据库混合:SQL Server+PostgreSQL+MySQL

发布于:2025-06-29 ⋅ 阅读:(23) ⋅ 点赞:(0)

🚀 ABP VNext + 多数据库混合:SQL Server+PostgreSQL+MySQL



一、引言 🚀

✨ TL;DR

  1. 在同一 ABP VNext 应用中并行驱动 SQL ServerPostgreSQLMySQL
  2. 按模块或租户动态路由至不同 DbContext,支持多租户多库隔离
  3. 实现跨库事务(TransactionScope / Saga 模式)与最终一致性(Outbox + 消息补偿)
  4. 高性能与高可用:池化 DbContext连接池调优重试策略健康检查全面覆盖

📚 背景与动机

  • 差异化业务需求

    • 报表 BI 场景需复杂查询,推荐 PostgreSQL 🛠️
    • 日志审计关注吞吐,倾向 MySQL 📝
    • 核心用户数据要求强一致性,落地 SQL Server 🔒
  • 多租户隔离

    • 不同租户或模块走独立数据库
  • 一致性难题

    • 跨库写入如何原子提交?MSDTC 两阶段 vs Saga/最终一致性如何抉择? 🤔

二、环境与依赖 🧩

  • 🛠️ 运行平台

    • .NET 6
    • ABP VNext
    • EF Core
  • 📦 NuGet 包 & 版本

    <ItemGroup>
      <PackageReference Include="Volo.Abp.EntityFrameworkCore.SqlServer" Version="6.*" />
      <PackageReference Include="Volo.Abp.EntityFrameworkCore.PostgreSql" Version="7.*" />
      <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.*" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
      <PackageReference Include="System.Transactions" Version="6.*" />
      <PackageReference Include="DotNetCore.CAP" Version="6.*" />
      <PackageReference Include="BenchmarkDotNet" Version="0.13.*" />
    </ItemGroup>
    
  • 🔒 连接字符串安全

    • 生产环境建议使用 Azure Key VaultHashiCorp Vault环境变量 管理密钥
    • appsettings.json 中仅写占位符,并通过环境变量注入
{
  "ConnectionStrings": {
    "Default":   "%SQLSERVER_CONN%",
    "Reporting": "%POSTGRES_CONN%",
    "Analytics": "%MYSQL_CONN%"
  }
}

三、架构概览 🏗️

  • 模块—数据库映射

    • 核心用户模块 → SQL Server
    • 报表 BI 模块 → PostgreSQL
    • 日志分析模块 → MySQL
UserModule
ReportModule
LogModule
Controller
SqlServerDbContext
PostgresDbContext
MySqlDbContext

四、注册多 DbContext(池化 + 重试)✨

在你的 YourProjectModule : AbpModuleConfigureServices 中:

public override void ConfigureServices(ServiceConfigurationContext context)
{
    var config     = context.Services.GetConfiguration();
    var sqlConn    = config.GetConnectionString("Default");
    var pgConn     = config.GetConnectionString("Reporting");
    var mySqlConn  = config.GetConnectionString("Analytics");

    // 1. 原生 EF Core 池化注册
    context.Services.AddDbContextPool<MainDbContext>(
        opts => opts.UseSqlServer(
            sqlConn,
            sql => sql.EnableRetryOnFailure()
        ),
        poolSize: 128
    );
    context.Services.AddDbContextPool<ReportDbContext>(
        opts => opts.UseNpgsql(
            pgConn,
            npgsql => npgsql.EnableRetryOnFailure()
        ),
        poolSize: 64
    );
    context.Services.AddDbContextPool<AnalyticsDbContext>(
        opts => opts.UseMySql(
            mySqlConn,
            ServerVersion.AutoDetect(mySqlConn),
            my => my.EnableRetryOnFailure()
        ),
        poolSize: 64
    );

    // 2. ABP 默认仓储注册
    Configure<AbpDbContextOptions>(opts =>
    {
        opts.AddDefaultRepositories<MainDbContext>(includeAllEntities: true);
        opts.AddDefaultRepositories<ReportDbContext>(includeAllEntities: true);
        opts.AddDefaultRepositories<AnalyticsDbContext>(includeAllEntities: true);
    });
}

💡 Tips

  • AddDbContextPool 降低高并发下 DbContext 对象创建成本
  • EnableRetryOnFailure() 探测瞬时故障、自动重试
  • 可在连接串中追加 Max Pool SizeMin Pool SizeConnection Idle Lifetime

五、按模块路由 🧭

1. ConventionalController(推荐)✅

Configure<AbpConventionalControllerOptions>(opts =>
{
    opts.Create(typeof(UserModule).Assembly);
    opts.Create(typeof(ReportModule).Assembly);
    opts.Create(typeof(LogModule).Assembly);
});
  • 依赖注入

    • UserModule 注入 MainDbContext
    • ReportModule 注入 ReportDbContext
    • LogModule 注入 AnalyticsDbContext

2. 属性切换(特例)🔄

[DbContext(typeof(ReportDbContext))]
public class ReportAppService : ApplicationService
{
    public ReportAppService(ReportDbContext reportDb) {}
}

💡 Tips

  • 大多数场景使用 ConventionalController 即可
  • 属性切换仅在少量服务需要手动绑定时使用

六、按租户路由 🌐

1. 实现 MultiTenantConnectionStringResolver

继承 ABP 的 MultiTenantConnectionStringResolver,重写 ResolveAsync

public class MyTenantConnectionStringResolver 
    : MultiTenantConnectionStringResolver
{
    private readonly IConfiguration _config;
    public MyTenantConnectionStringResolver(
        ICurrentTenant currentTenant,
        IConfiguration config)
        : base(currentTenant, config)
    {
        _config = config;
    }

    public override Task<string> ResolveAsync(string name)
    {
        return CurrentTenant.Id switch
        {
            1 => Task.FromResult(_config.GetConnectionString("Default")),
            2 => Task.FromResult(_config.GetConnectionString("Reporting")),
            _ => base.ResolveAsync(name)
        };
    }
}

2. 替换默认解析器

public override void ConfigureServices(ServiceConfigurationContext context)
{
    // ... 其他注册

    context.Services.Replace(
        ServiceDescriptor.Singleton<IConnectionStringResolver, MyTenantConnectionStringResolver>()
    );
}

💡 Tips

  • 使用 IConnectionStringResolver 可动态解析连接串
  • 确保已依赖 Volo.Abp.MultiTenancy 模块并初始化 ICurrentTenant

七、跨数据库事务模式 🔄

1. 两阶段提交(TransactionScope

using var ts = new TransactionScope(
    TransactionScopeAsyncFlowOption.Enabled);

await _mainDbContext.SaveChangesAsync();
await _reportDbContext.SaveChangesAsync();
ts.Complete();

注意

  • Windows & Linux 下均需配置 MSDTC 或等效组件
  • 网络、防火墙、权限需互通

2. Saga / 最终一致性(CAP + Outbox)📚

YourProjectModule.ConfigureServices 中注册 CAP:

public override void ConfigureServices(ServiceConfigurationContext context)
{
    // ... 其他注册

    context.Services.AddCap(cap =>
    {
        cap.UseEntityFramework<MainDbContext>();     // Outbox 表
        cap.UseRabbitMQ(r => { /* RabbitMQ 配置 */ });
        cap.UseDashboard();                          // 可视化监控
    });
}
// 发布事件
await _capPublisher.PublishAsync("myapp.report.created", reportDto);

// 消费者
[CapSubscribe("myapp.report.created")]
public async Task HandleReportCreated(ReportDto dto)
{
    _reportDbContext.Reports.Add(dto.ToEntity());
    await _reportDbContext.SaveChangesAsync();
}

💡 Tips

  • 定期清理 Outbox 表 🧹
  • CAP Dashboard 可实时监控消息流转 📊

八、处理跨库 UOW 🔧

ABP 默认 IUnitOfWork 已支持同一作用域内多 DbContext 统一提交。仅在需手动补偿或非事务边界定制时可扩展:

public class MultiDbUnitOfWork : IUnitOfWork
{
    // 注入 MainDbContext、ReportDbContext、AnalyticsDbContext…

    public async Task SaveChangesAsync()
    {
        await _mainDb.SaveChangesAsync();
        await _reportDb.SaveChangesAsync();
        await _analyticsDb.SaveChangesAsync();
    }
}

💡 Tips

  • 大多数场景使用 ABP 内置 UOW 即可 👍
  • 自定义 UOW 适合细粒度补偿或非事务场景

九、性能与最佳实践 📈

  • 连接池调优Max Pool SizeMin Pool SizeConnection Idle Lifetime
  • DbContext 生命周期:Scoped(短生命周期),避免 Singleton/Transient 导致泄漏
  • 性能基准测试(BenchmarkDotNet 示例)
  • 健康检查(Kubernetes Readiness/Liveness)
services.AddHealthChecks()
    .AddSqlServer(sqlConn, name: "sqlserver", tags: new[] { "db","primary" }, timeout: TimeSpan.FromSeconds(5))
    .AddNpgSql(pgConn, name: "postgres", tags: new[] { "db","report" }, timeout: TimeSpan.FromSeconds(5))
    .AddMySql(mySqlConn, name: "mysql", tags: new[] { "db","analytics" }, timeout: TimeSpan.FromSeconds(5));
  • 容器化示例(Kubernetes Secret + envFrom)
apiVersion: v1
kind: Secret
metadata: { name: db-credentials }
type: Opaque
data:
  SQLSERVER_CONN: <base64>
  POSTGRES_CONN:  <base64>
  MYSQL_CONN:     <base64>
---
apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - name: myapp
        envFrom:
        - secretRef: { name: db-credentials }

十、项目示例 🎬

项目结构

src/
 ├─ MyCompany.MyApp.Domain.Shared
 ├─ MyCompany.MyApp.Domain
 ├─ MyCompany.MyApp.Application
 ├─ MyCompany.MyApp.EntityFrameworkCore
 |    ├─ MainDbContext.cs
 |    ├─ ReportDbContext.cs
 |    └─ AnalyticsDbContext.cs
 ├─ MyCompany.MyApp.HttpApi
 └─ MyCompany.MyApp.Web

示例流程

  1. 创建用户 → 写入 MainDbContext (SQL Server)

  2. 生成报表任务 → 写入 ReportDbContext (PostgreSQL)

  3. 记录操作日志 → 写入 AnalyticsDbContext (MySQL)

  4. 事务 & 故障模拟

    • 关闭某库验证 TransactionScope 回滚
    • 触发 CAP 补偿流程

参考文档



网站公告

今日签到

点亮在社区的每一天
去签到