【愚公系列】2022年03月 .NET架構班 015-ABP vNext 多租戶模塊

      網友投稿 2029 2025-03-31

      一、租戶模塊

      【愚公系列】2022年03月 .NET架構班 015-ABP vNext 多租戶模塊

      1.租戶和多租戶

      多租戶定義:多租戶技術或稱多重租賃技術,簡稱SaaS,是一種軟件架構技術,是實現如何在多用戶環境下(此處的多用戶一般是面向企業用戶)共用相同的系統或程序組件,并且可確保各用戶間數據的隔離性。簡單講:在一臺服務器上運行單個應用實例,它為多個租戶(客戶)提供服務。從定義中我們可以理解:多租戶是一種架構,目的是為了讓多用戶環境下使用同一套程序,且保證用戶間數據隔離。那么重點就很淺顯易懂了,多租戶的重點就是同一套程序下實現多用戶數據的隔離。

      2. 獨立數據庫

      這是第一種方案,即一個租戶一個數據庫,這種方案的用戶數據隔離級別最高,安全性最好,但成本較高。

      3. 共享數據庫,獨立 Schema

      這是第二種方案,即多個或所有租戶共享Database,但是每個租戶一個Schema(也可叫做一個user)。底層庫比如是:DB2、ORACLE等,一個數據庫下可以有多個SCHEMA

      4. 共享數據庫,共享 Schema,共享數據表

      這是第三種方案,即租戶共享同一個Database、同一個Schema,但在表中增加TenantID多租戶的數據字段。這是共享程度最高、隔離級別最低的模式。即每插入一條數據時都需要有一個客戶的標識。這樣才能在同一張表中區分出不同客戶的數據。

      5.租戶模塊的源碼

      -:

      https://github.com/abpframework/abp/tree/dev/modules/TenantManagement

      二、集成權限模塊

      1.EBusiness.Domain.Shared集成多租戶模塊

      1、先在項目中通過Nuget下載

      Volo.Abp.TenantManagement.Domain.Shared

      2、然后在EBusinessDomainSharedModule文件上增加

      [DependsOn( typeof(AbpTenantManagementDomainSharedModule) )] public class EBusinessDomainSharedModule : AbpModule {}

      2.EBusiness.Domain集成多租戶模塊

      1、先在項目中通過Nuget下載

      Volo.Abp.TenantManagement.Domain

      2、然后在EBusinessDomainModule文件上增加

      [DependsOn( typeof(AbpTenantManagementDomainModule) )] public class EBusinessDomainSharedModule : AbpModule {}

      3.EBusiness.EntityFrameworkCore集成多租戶模塊

      1、先在項目中通過Nuget下載

      Volo.Abp.TenantManagement.EntityFrameworkCore

      2、然后在EBusinessEntityFrameworkCoreModule文件上增加

      [DependsOn( typeof(AbpTenantManagementEntityFrameworkCoreModule) )] public class EBusinessEntityFrameworkCoreModule: AbpModule {}

      3、然后在EBusinessDbContext類上實現ITenantManagementDbContext上下文

      public DbSet TenantGrants { get; }

      4、然后在EBusinessDbContext類上添加

      [ReplaceDbContext(typeof(ITenantManagementDbContext))] [ConnectionStringName("Default")] public class EBusinessDbContext : AbpDbContext, ITenantManagementDbContext {}

      5、然后在OnModelCreating方法中添加

      protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); /* Include modules to your migration db context */ ..... builder.ConfigureTenantManagement(); ..... }

      7、最后啟動YDT.EBusiness.DbMigrator遷移項目,生成租戶表

      4.EBusiness.Application.Contracts集成多租戶模塊

      1、先在項目中通過Nuget下載

      Volo.Abp.TenantManagement.Application.Contracts

      2、然后在EBusinessApplicationContractsModule文件上增加

      [DependsOn( typeof(AbpTenantManagementApplicationContractsModule) )] public class EBusinessApplicationContractsModule: AbpModule {}

      5.EBusiness.Application集成多租戶模塊

      1、先在項目中通過Nuget下載

      Volo.Abp.TenantManagement.Application.Contracts

      2、然后在EBusinessApplicationModule文件上增加

      [DependsOn( typeof(AbpTenantManagementApplicationModule) )] public class EBusinessApplicationContractsModule: AbpModule {}

      6.EBusiness.HttpApi集成多租戶模塊

      1、先在項目中通過Nuget下載

      Volo.Abp.TenantManagement.Application.Contracts

      2、然后在EBusinessHttpApiModule文件上增加

      [DependsOn( typeof(AbpTenantManagementHttpApiModule) )] public class EBusinessHttpApiModule: AbpModule {}

      7.EBusinessHttpApiHostModule使用多租戶

      if (MultiTenancyConsts.IsEnabled) { app.UseMultiTenancy(); }

      三、繼承租戶模塊

      1、先在EBusiness.Domain模塊中通過Nuget引入

      Volo.Abp.MultiTenancy

      2、然后在EBusiness.Domain模塊中Product類上添加IMultiTenant接口

      public class Product : FullAuditedAggregateRoot, IMultiTenant { public Guid? TenantId { get; protected set; } // 租戶Id public string ProductCode { set; get; } //商品編碼 public string ProductUrl { set; get; } // 商品主圖 public string ProductTitle { set; get; } //商品標題 public string ProductDescription { set; get; } // 圖文描述 public decimal ProductVirtualprice { set; get; } //商品虛擬價格 public decimal ProductPrice { set; get; } //價格 public int ProductSort { set; get; } //商品序號 public int ProductSold { set; get; } //已售件數 public int ProductStock { set; get; } //商品庫存 public string ProductStatus { set; get; } // 商品狀態 .... }

      3、然后運行EBusiness.DbMigrator項目生成商品多租戶表,如下圖所示

      4、然后運行項目

      四、使用租戶模塊

      1.租戶創建

      1、Tenant(租戶)接口

      2、使用post接口添加租戶信息

      2.租戶轉換(切換)存儲

      1、獲取租戶

      2、TenantSwitch接口

      ///

      /// 租戶轉換服務 /// public class TenantSwitchAppService : EBusinessAppService, ITenantSwitchAppService { public ITenantStore TenantStore { set; get; } public IHttpContextAccessor httpContextAccessor { get; set; } public void SwitchTenant(TenantSwitchInputDto tenantSwitchInputDto) { var guid = CurrentTenant.Id; Guid? tenantId = null; if (!tenantSwitchInputDto.Name.IsNullOrEmpty()) { var tenant = TenantStore.Find(tenantSwitchInputDto.Name); if (tenant == null) { throw new BusinessException($"{tenantSwitchInputDto.Name},租戶不存在"); } if (!tenant.IsActive) { throw new BusinessException($"{tenantSwitchInputDto.Name},租戶未激活"); } tenantId = tenant.Id; } MultiTenancyCookieHelper.SetTenantCookie(httpContextAccessor.HttpContext, tenantId, TenantResolverConsts.DefaultTenantKey); } }

      public static class MultiTenancyCookieHelper { public static void SetTenantCookie( HttpContext context, Guid? tenantId, string tenantKey) { if (tenantId != null) { context.Response.Cookies.Append( tenantKey, tenantId.ToString(), new CookieOptions { Path = "/", HttpOnly = false, Expires = DateTimeOffset.Now.AddYears(10) } ); } else { context.Response.Cookies.Delete(tenantKey); } } }

      3、在post租戶轉換轉換租戶,存儲到cookie

      3.租戶取值

      1、在EBusiness.Application模塊中ProductService類中創建商品使用ICurrentTenant獲取租戶信息

      public class ProductService : EBusinessAppService, IProductService { public void Create(CreateProductDto createProductDto) { var tenantId = CurrentTenant.Id; .... } }

      4.自定義多租戶解析

      ///

      /// 多租戶 redis自定義解析 /// public class RedisTenantResolveContributor : TenantResolveContributorBase { public override string Name => "redis"; public override Task ResolveAsync(ITenantResolveContext context) { // 1、獲取tanentKey string tanentKey = TenantResolverConsts.DefaultTenantKey; // 2、從redis取值 // 3、把值設置到上下文 context.TenantIdOrName = ""; return Task.CompletedTask; } }

      在EBusinessApplicationModule中配置

      Configure(options => { options.TenantResolvers.Add(new RedisTenantResolveContributor()); });

      五、租戶模模塊原理

      1.多租戶管理原理

      條件

      1、Volo.Abp.MultiTenancy模塊

      接口

      1、TenantDefinition 2、TenantDefinitionProvider 3、TenantDefinitionManager

      說明

      1、TenantDefinition執行定義 2、TenantDefinitionProvider提供多租戶。 3、TenantDefinitionManager 核心執行

      2.多租戶轉換存儲原理

      條件

      1、YDT.EBusiness.Application模塊 2、Volo.Abp.TenantManagement.Domain模塊

      接口

      1、TenantSwitchAppService 2、TenantManager 3、TenantManagementProvider 4、ITenantGrantRepository

      說明

      1、TenantSwitchAppService負責入口 2、TenantManager負責管理轉換 3、TenantManagementProvider負責轉換 4、ITenantGrantRepository負責租戶取值

      3.多租戶取值原理

      條件

      1、Volo.Abp.MultiTenancy模塊 2、Volo.Abp.AspNetCore.MultiTenancy模塊

      接口

      1、MultiTenancyMiddleware 2、TenantConfigurationProvider 3、TenantResolver 4、ITenantResolveContributor

      說明

      1、MultiTenancyMiddleware負責從請求中獲取 2、TenantConfigurationProvider負責提供多租戶所有信息發 3、TenantResolver負責統一解析租戶 4、ITenantResolveContributor負責具體解析租戶

      4.多租戶EFCore原理

      條件

      1、Volo.Abp.EntityFrameworkCore模塊

      接口

      1、AbpDbContext

      說明

      1、AbpDbContext負責從實體中過濾租戶IMultiTenant接口中TenantId租戶Id

      六、租戶模塊其他客戶端使用

      1.EBusiness.Web頁面訪問權限模塊

      1、先在YDT.EBusiness.Web模塊中通過Nuget引入

      Volo.Abp.TenantManagement.Web

      2、然后在YDT.EBusiness.Web模塊中EBusinessWebModule類上添加

      [DependsOn( .... typeof(AbpTenantManagementWebModule), .... )] public class EBusinessWebModule : AbpModule {}

      2.OA系統調用租戶模塊

      1、先在項目中通過Nuget下載

      YDT.EBusiness.HttpApi.Client

      2、然后在YDT.OA文件上增加

      [DependsOn( typeof(AbpTenantManagementHttpApiClientModule) )] public class OAModule: AbpModule {}

      七、租戶數據庫使用

      1.租戶數據庫添加操作

      1、使用數據庫腳本創建ydt_ebusiness_14數據庫

      2、添加租戶數據庫字符串

      2.租戶數據庫切換原理

      條件

      1、Volo.Abp.EntityFrameworkCore模塊 2、Volo.Abp.MultiTenancy模塊

      接口

      1、DbContextOptionsFactory 2、MultiTenantConnectionStringResolver

      說明

      1、DbContextOptionsFactory負責動態獲取數據庫連接 2、MultiTenantConnectionStringResolver負責解析租戶數據庫字符串

      .NET 數據庫

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:什么是數字化客戶體驗?
      下一篇:在表格如何查找自己要找的東西(在表格中如何查找出自己想要的)
      相關文章
      亚洲AV无码专区电影在线观看| 国产精品亚洲玖玖玖在线观看| 亚洲精品网站在线观看不卡无广告| 成人区精品一区二区不卡亚洲| 亚洲国产av高清无码| 亚洲午夜一区二区电影院| 亚洲沟沟美女亚洲沟沟| 亚洲国产模特在线播放| 亚洲中文久久精品无码1| 亚洲国产av一区二区三区丶| 亚洲乱码在线视频| 国产精品高清视亚洲精品| 亚洲人成电影网站久久| 亚洲成_人网站图片| 亚洲色偷偷色噜噜狠狠99| 亚洲成av人片天堂网无码】| 国产AV无码专区亚洲AV蜜芽| 亚洲av中文无码| 国产午夜亚洲不卡| 亚洲中文字幕无码一区| 亚洲国产AV无码专区亚洲AV | 国产AV无码专区亚洲A∨毛片| 亚洲精品成人网站在线观看| 亚洲国产成人片在线观看无码| 亚洲av无码不卡| 亚洲精品国产免费| 亚洲人成小说网站色| 亚洲爆乳无码精品AAA片蜜桃| 国产精品亚洲二区在线| 综合亚洲伊人午夜网| 亚洲国产精品高清久久久| 日韩精品亚洲人成在线观看| 亚洲最大的视频网站| 亚洲一区中文字幕在线观看| 亚洲日韩看片无码电影| 国产av无码专区亚洲av毛片搜| 国产精品V亚洲精品V日韩精品 | 亚洲人成图片小说网站| 亚洲高清在线视频| 亚洲中文字幕在线无码一区二区| 亚洲色少妇熟女11p|