数据库切库支持 #

mall3s微服务项目底层采用sqlsugar orm组件,在项目开发中可以很方便做数据库切库支持。

数据库自动切库 #

  1. nacos配置好数据库连接.

    netcore-datasource.json****上新增配置

image-20230310221346206

image-20230310221403122

{
    "DemoConnectionStrings": {
        "ConfigId": "blt_demo",//租户ID,通常使用数据库名不带环境,如blt_oms_test/blt_oms_pro,租户ID使用blt_oms
        "DBName": "blt_demo",//数据库名
        "DBType": "MySql",//数据库类型
        "DefaultConnection": "server=sh-cdb-s261yedo.sql.tencentcdb.com;port=59347;uid=erpdba;pwd=erp123!@#;database={0}"//数据库链接
    }
}

名词解释:

  • ConfigId为关键点,为了避免冲突,请使用数据库名不带环境,如blt_oms_test/blt_oms_pro,租户ID使用blt_oms,同一次启动服务,切勿同时注入正式和测试环境

  • 如在netcore-项目-common.json(如netcore-demo-common.json)上配置了**ExtraDB****,**如下,则直接调用services.AddMicroService(_configuration)即可

    {
       "ExtraDB":["DemoConnectionStrings"] //扩展库为DemoConnectionStrings
    }
    
  1. 代码配置

    • netcore-datasource.json

      {
          "DemoConnectionStrings": {
              "ConfigId": "blt_demo",
              "DBName": "blt_demo",
              "DBType": "MySql",
              "DefaultConnection": "server=sh-cdb-s261yedo.sql.tencentcdb.com;port=59347;uid=erpdba;pwd=erp123!@#;database={0}"
          }
      }
      
    • netcore-demo-common.json

      {
      "ExtraDB":["DemoConnectionStrings"] //扩展库为DemoConnectionStrings
      }
      
    • Startup.cs

      启动代码

      public void ConfigureServices(IServiceCollection services)
      {
                  //除了默认DB和租户DB外的DB
      #if DEBUG
                   //添加微服务(debug不验证token)
                  services.AddMicroService(_configuration, false);
      #else
                   //添加微服务
                  services.AddMicroService(_configuration, true);
      #endif
                  ....
      }
       public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      {
                    ....          
                  //注入微服务
                  app.UseMicroService(_configuration);
                  ....
      
      }
      

      微服务有四个实现,下面两个为自定义扩展库,可灵活选择

      //实现一:nacos配置好DB链接信息,直接告知微服务需要额外加入的DB的key
      public static IServiceCollection AddMicroService(this IServiceCollection services, IConfiguration configuration, bool enableGlobalAuthorize = false, List<string> connKeys = null)
      
      //实现二:自行配置DB配置后赋值
      public static IServiceCollection AddMicroService(this IServiceCollection services, IConfiguration configuration,bool enableGlobalAuthorize= false, List<ConnectionConfig> connectionConfigs = null)
      
  2. 实体类

TenantAttribute 中的 configId值“blt_demo”对应nacos配置中的ConfigId,切记,不可乱填。

实体类没有TenantAttribute属性的,依旧需要手动切库

[SugarTable("exchange_rate")]
[Tenant("blt_demo")] //blt_demo对应nacos配置中的ConfigId
public class ExchangeRateEntity
{
  ...
}

4、代码调用

必须使用ISqlSugarRepository<TEntity>,才能实现自动切库

private readonly ISqlSugarRepository<ExchangeRateEntity> _exchangeRateRepository;
private readonly ISqlSugarRepository<DbLinkEntity> _dbLinkRepository;

public async Task Test()
{
//无需切库直接调用即可
    var dbs = await _dbLinkRepository.ToListAsync();
    var exchangeRate = await _exchangeRateRepository.ToListAsync();
}
上次更新: 3/10/2023, 5:03:49 PM