简体   繁体   English

ASP .NET Core 搭建多租户环境的方法

[英]Approach of setting up multi-tenant environment with ASP .NET Core

I have been struggling with the way I should set up my multi-tenant environment.我一直在努力设置我的多租户环境。 In a previous project I had set it al up as a monolith where there was a tenant entity to bind al the correct data to each other.在之前的项目中,我将其设置为一个整体,其中有一个租户实体将所有正确的数据相互绑定。 This way is quite easy to set up but the data of all tenants is in one big database.这种方式很容易设置,但所有租户的数据都在一个大数据库中。

My idea now is to set it all up in a different way.我现在的想法是以不同的方式进行设置。 I will be using Postgres because of the possibility to also have 'schema'.我将使用 Postgres,因为也可能有“模式”。 My idea is to have overall data in the public schema and tenant specific data in the tenants own schema.我的想法是在公共模式中拥有整体数据,在租户自己的模式中拥有特定于租户的数据。

There are some struggles that I face where I just can not get my head around.我面临一些挣扎,我无法理解。 In the picture below, you see a quick sketch of the two scenarios.在下图中,您可以看到这两种情况的简图。 The bottom one has everything in the public schema where the above one has for example domain and user in the public schema.底部的所有内容都在公共模式中,而上面的则在公共模式中具有例如域和用户。

My idea with this is that when a customer goes to a tenants specific URL the application does not have to crawl through all possible schemas.我的想法是,当客户访问特定于租户的 URL 时,应用程序不必遍历所有可能的模式。 Same counts for user.对用户来说同样重要。 When a user wants to login to the application we also do not want to crawl through all schema's.当用户想要登录应用程序时,我们也不想爬过所有模式。 But how do I set this up in a proper way?但是我该如何以正确的方式进行设置呢?

Also when the user manages his domains (add/edit/delete), he is actually managing them on the public schema.此外,当用户管理他的域(添加/编辑/删除)时,他实际上是在公共模式上管理它们。 Or should I avoid using public at all?还是我应该完全避免使用 public ? I thought of putting them here because otherwise when a user enters a new domain I have to check them also on other tenants schema.我想把它们放在这里,否则当用户进入一个新域时,我还必须在其他租户架构上检查它们。

Also can I just simple set the schema name of the tenant in the domain or should this be a connectionstring?我也可以简单地设置域中租户的模式名称还是应该是连接字符串?

I also read here and there about multi-tenancy but I'm unsure how the application can get the data of a tenant with the use of a different schema (public) to fetch the correct data from the tenants schema.我还在这里和那里阅读了有关多租户的信息,但我不确定应用程序如何使用不同的模式(公共)从租户模式中获取正确的数据来获取租户的数据。 Should I use Inversion of Control to get a specific implementation depending on calling for Domain (public schema) or customer (tenant specific)?我应该使用控制反转来根据对域(公共模式)或客户(特定于租户)的调用来获得特定的实现吗?

https://nebulab.it/blog/how-to-switch-solidus-ecommerce-to-multi-tenant/ https://nebulab.it/blog/how-to-switch-solidus-ecommerce-to-multi-tenant/

https://michael-mckenna.com/multi-tenant-asp-dot-net-core-application-tenant-resolution https://michael-mckenna.com/multi-tenant-asp-dot-net-core-application-tenant-resolution

Which Multi-tenant approach is recommended 推荐哪种多租户方法

在此处输入图像描述

I personally do something like your first approach + multidatabase using a first database that tells me where each tenant is located.我个人使用第一个数据库执行您的第一个方法 + 多数据库之类的操作,该数据库告诉我每个租户的位置。 I call it routing database.我称之为路由数据库。 That dB has 2 tables.该分贝有2个表。

  • databases (if, connection string)数据库(如果,连接字符串)
  • tenants (tenant info, database if)租户(租户信息,如果数据库)

This way I have the data across multiple databases and still multiple tenants per database.这样我就拥有跨多个数据库的数据,并且每个数据库仍然有多个租户。

Also the database id => connection string is cached usually with redis so no need to hit the dB on every request (you can even use in memory for this) with cachemanagercore library.此外,数据库 id => 连接字符串通常使用 redis 进行缓存,因此无需使用 cachemanagercore 库在每个请求上达到分贝(您甚至可以在 memory 中使用)。

If a tenant requires its own database not shared is easy to handle too.如果租户需要自己的不共享数据库也很容易处理。 Usually a cookie with the dB id picked up on a Middleware and acceded then by a DatabaseResolver class injected where you need to access the dB with a method like DbContext Getdatabse () is all you need通常一个带有 dB id 的 cookie 在中间件上拾取,然后由 DatabaseResolver class 注入,您需要使用 DbContext Getdatabse () 之类的方法访问 dB,这就是您所需要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.Net核心多租户 - Asp.Net Core Multi-Tenant 多租户ASP .NET应用程序中的隔离 - Isolation in a Multi-tenant ASP .NET Application 确保资源由ASP.NET Core中的多租户系统中的租户拥有 - Making sure resource is owned by tenant in a multi-tenant system in ASP.NET Core 在多租户环境中,使用 .Net Core,如何配置 Windows 服务以针对正确的数据库 - In a multi-tenant environment, using .Net Core, how to configure a Windows Service to target the correct database 基于多租户Asp.net核心网站中的参数的JWT身份验证 - JWT authentication based on the Parameter in Multi-tenant Asp.net Core web site ASP.NET Identity + Bearer Token + Multi-Tenant - ASP.NET Identity + Bearer Token + Multi-Tenant 在多租户环境中更快地加载NHibernate - Faster NHibernate loading in multi-tenant environment Autofac多租户ASP.NET应用程序返回租户而不是标识符 - Autofac Multi-Tenant ASP.NET Application return the tenant instead of the identifier Hangfire - 多租户,ASP.NET Core - 解析正确的租户 - Hangfire - Multi tenant, ASP.NET Core - Resolving the correct tenant 使用路由配置在多租户环境中检查ASP.NET Core Health - ASP.NET Core Health checks in multi tenant environment using routing configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM