找回密码
 立即注册
首页 业界区 业界 多租户解析与Demo

多租户解析与Demo

广性 2025-6-6 14:11:31
在做Saas应用时,多租户解析往往是很重要的组成部分,也是用户访问网站最先处理的逻辑。
文前介绍:
多租户的数据库实现方式主要有三种:

  • 单一数据库实现,每条数据标识租户Id进行识别数据属于哪个租户
  • 一租户一个数据库,能够做到完全的数据隔离
  • 混合模式,部分数据在一张表上,主要是一些基础数据;其他业务数据分库存储。
无论是哪种方式都要知道租户是谁才能查询数据库。
获取租户的方式也可以有多种:

  • 根据域名或者子域名不同可以获知租户,上面的所有场景都适合使用;(必须要有域名和dns服务)
  • 根据用户id获取租户信息,从而存入前端的cookie中或者header中,只适用于上面的1、3方式,因为需要不同租户的用户都存入一种表中,方便查询对应的租户信息;
 
下文中的例子只是简单的一个例子,没有进一步的业务场景,实现功能如下:
用户根据不同的域名进入系统;
后台拿刀http传入的域名并解析;
查询租户数据库,查找出租户名称返回显示。
 
新建解决方案,并其下新增三个项目:
try_MultiTenantApi:webapi项目,项目的启动项目,为了方方便,租户的Service和IService放入了这个项目中,实际应用时要放入业务层;
MultiTenantApi.Models:类库项目,存放租户对象;
MultiTenantApi.Data:数据层,使用efcore,存放上下文DbContext和迁移文件
1.png

 
MultiTenantApi.Models中新增实体Tenant
  1. public class Tenant
  2. {
  3.     public int Id { get; set; }
  4.     public string Identifier { get; set; } // 租户标识符,例如域名
  5.     public string Name { get; set; }
  6.     public string ConnectionString { get; set; } // 每个租户的独立数据库连接字符串
  7. }
复制代码
MultiTenantApi.Data:
引入nuget包:要注意跟你项目的.net版本相同
2.png

 这些包的具体说明:

  • Microsoft.EntityFrameworkCore

    • 功能: Entity Framework Core 是一个现代的对象-关系映射器(ORM),用于 .NET。它支持 LINQ 查询、变更跟踪、更新和模式迁移。EF Core 可以与多种数据库一起工作,包括 SQL Server、Azure SQL Database、SQLite 和 Azure Cosmos DB。
    • 作用: 提供了核心的 ORM 功能,用于在 .NET 应用程序中与数据库进行交互。

  • Microsoft.EntityFrameworkCore.Design

    • 功能: 提供了 Entity Framework Core 工具的共享设计时组件。这些组件包括用于创建和管理迁移的工具。
    • 作用: 用于在设计时(如运行迁移命令时)提供必要的工具支持,帮助开发者更高效地管理和生成数据库迁移。

  • Microsoft.EntityFrameworkCore.Proxies

    • 功能: 为 Entity Framework Core 提供延迟加载代理。这些代理用于在需要时加载相关对象,从而减少内存使用和提高性能。
    • 作用: 通过代理机制实现延迟加载,提高应用程序的性能和响应速度。

  • Microsoft.EntityFrameworkCore.SqlServer

    • 功能: 提供了针对 Microsoft SQL Server 的数据库提供程序。这使得 Entity Framework Core 能够与 SQL Server 数据库进行交互。
    • 作用: 为 SQL Server 数据库提供特定的数据库提供程序,确保 EF Core 能够正确地与 SQL Server 交互。

  • Microsoft.EntityFrameworkCore.Tools

    • 功能: 提供了 Entity Framework Core 工具,用于 NuGet Package Manager Console 中的 Visual Studio。
    • 作用: 通过 Visual Studio 的 NuGet Package Manager Console 提供 EF Core 工具,帮助开发者更方便地管理和使用 EF Core 的设计时工具。

新增数据上下文:
  1. public class ApplicationDbContext : DbContext
  2. {
  3.     public ApplicationDbContext(DbContextOptions options)
  4.     : base(options)
  5.     {
  6.     }
  7.     public DbSet<Tenant> Tenants { get; set; }
  8.     protected override void OnModelCreating(ModelBuilder modelBuilder)
  9.     {
  10.         base.OnModelCreating(modelBuilder);
  11.         modelBuilder.Entity<Tenant>().HasData(
  12.             new Tenant { Id = 1, Identifier = "tenant1", Name = "Tenant 1", ConnectionString = "Server=(localdb)\\mssqllocaldb;Database=TenantDb1;Trusted_Connection=True;" },
  13.             new Tenant { Id = 2, Identifier = "tenant2", Name = "Tenant 2", ConnectionString = "Server=(localdb)\\mssqllocaldb;Database=TenantDb2;Trusted_Connection=True;" }
  14.         );
  15.     }
  16. }
复制代码
注意实际应用中上面的数据库连接字符串根据你的具体情况变动一下,因为本例子没有用到这个字段所以想要尝试可以不用改。
 
try_MultiTenantApi:
引入nuget包:
3.png

 新建类TenantService:
  1. public interface ITenantService
  2. {
  3.     Task<Tenant> GetTenantByIdentifierAsync(string identifier);
  4. }
  5. public class TenantService : ITenantService
  6. {
  7.     private readonly ApplicationDbContext _context;
  8.     public TenantService(ApplicationDbContext context)
  9.     {
  10.         _context = context;
  11.     }
  12.     public async Task<Tenant> GetTenantByIdentifierAsync(string identifier)
  13.     {
  14.         return await _context.Tenants.FirstOrDefaultAsync(t => t.Identifier == identifier);
  15.     }
  16. }
复制代码
新建一个中间件TenantMiddleware:
用于每次的请求解析出租户信息
  1. namespace try_MultiTenantApi
  2. {
  3.     public class TenantMiddleware
  4.     {
  5.         private readonly RequestDelegate _next;
  6.         public TenantMiddleware(RequestDelegate next)
  7.         {
  8.             _next = next;
  9.         }
  10.         public async Task InvokeAsync(HttpContext context, ITenantService tenantService)
  11.         {
  12.             var subdomain = GetSubdomain(context.Request.Host.Value);
  13.             if (!string.IsNullOrEmpty(subdomain))
  14.             {
  15.                 var tenant = await tenantService.GetTenantByIdentifierAsync(subdomain);
  16.                 if (tenant != null)
  17.                 {
  18.                     context.Items["Tenant"] = tenant;
  19.                 }
  20.             }
  21.             await _next(context);
  22.         }
  23.         private string GetSubdomain(string host)
  24.         {
  25.             var parts = host.Split('.');
  26.             return parts.Length > 2 ? parts[0] : null;
  27.         }
  28.     }
  29. }
  30. public static class TenantMiddlewareExtensions
  31. {
  32.     public static IApplicationBuilder UseTenantMiddleware(this IApplicationBuilder builder)
  33.     {
  34.         return builder.UseMiddleware<TenantMiddleware>();
  35.     }
  36. }
复制代码
配置appsetting文件,配置租户数据库连接字符串:
  1. "ConnectionStrings": {
  2.   "DefaultConnection": "Data Source=localhost;Initial Catalog=try_MultiTenant;User ID=sa;Password=******;Encrypt=False;"
  3. },
复制代码
修改Program:
  1. var builder = WebApplication.CreateBuilder(args);
  2. builder.Services.AddControllers();
  3. builder.Services.AddDbContext(options =>
  4.     options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
  5. builder.Services.AddScoped<ITenantService, TenantService>();
  6. builder.Services.AddEndpointsApiExplorer();
  7. builder.Services.AddSwaggerGen();
  8. var app = builder.Build();
  9. // Configure the HTTP request pipeline.
  10. if (app.Environment.IsDevelopment())
  11. {
  12.     app.UseSwagger();
  13.     app.UseSwaggerUI();
  14. }
  15. app.UseHttpsRedirection();
  16. app.UseRouting();
  17. app.UseAuthorization();
  18. app.UseTenantMiddleware(); // 使用租户解析中间件
  19. app.MapControllers();
  20. app.Run();
复制代码
新建一个controller:ValuesController
  1. [ApiController]
  2. [Route("[controller]")]
  3. public class ValuesController : ControllerBase
  4. {
  5.     private readonly ITenantService _tenantService;
  6.     public ValuesController(ITenantService tenantService)
  7.     {
  8.         _tenantService = tenantService;
  9.     }
  10.     [HttpGet(nameof(Get1))]
  11.     public async Task<IActionResult> Get1()
  12.     {
  13.         var tenant = HttpContext.Items["Tenant"] as Tenant;
  14.         if (tenant == null)
  15.         {
  16.             return NotFound("Tenant not found");
  17.         }
  18.         return Ok(new { Message = $"Hello from {tenant.Name}" });
  19.     }
  20. }
复制代码
 
OK,至此项目就编码完成,接下来就是进行数据迁移。因为前面上下文中已经设置了出事测试数据
add-migration init
update-database
到此,租户的数据库已经创建,并且也有了初始的测试数据:
4.png

 为了测试方便咱们在launch启动文件中配置两个租户的访问地址:
  1. "profiles": {
  2.   "http": {
  3.     "commandName": "Project",
  4.     "dotnetRunMessages": true,
  5.     "launchBrowser": true,
  6.     "launchUrl": "swagger",
  7.     "applicationUrl": "http://localhost:5252;http://tenant1.Paas.JBWL.com:5252",
  8.     "environmentVariables": {
  9.       "ASPNETCORE_ENVIRONMENT": "Development"
  10.     }
  11.   },
  12. }
复制代码
接下来设置try_MultiTenantApi项目为启动项目,启来后出swagger页面:
5.png

 页面地址是默认第一个http://localhost:5252/
下面我们就可以修改一下本地的host文件映射两个域名,可以使用swichhost,也可以找到本地的host文件,增加地址映射
  1. 127.0.0.1 tenant1.Paas.JBWL.com
  2. 127.0.0.1 tenant2.Paas.JBWL.com
复制代码
启用后访问
  1. tenant1.Paas.JBWL.com:5252
复制代码
出现前面一模一样的页面,访问一下测试接口看是否解析成功:
6.png

 成功!
接下来试一下tenant2.paas.jbwl.com:5252
7.png

 
至此,这个测试demo也就完成了,只要解析出租户信息,接下来Saas的租户数据就能获取了。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册