Django 上多租户应用程序的最佳架构
我一直在思考创建基于多租户应用程序的正确/最佳方法 关于姜戈。
一些解释:
应用程序可以由多个租户(tenant1、tenant2、...)使用。
必须保护所有租户个人数据,防止其他租户(及其用户)访问。
租户可以选择为应用程序对象创建其他自定义字段。
当然,底层硬件限制了一个“系统”上的租户数量。
1)通过子域等方式分隔每个租户,并在底层使用特定于租户的数据库
2)在模型中使用一些租户 ID 来分隔数据库中的租户数据
我正在考虑部署过程、性能系统部件(网络服务器、数据库服务器、工作节点...)
最好的设置是什么?优点和缺点在哪里?
你怎么认为?
I've been brooding over the right/optimal way to create a multitenancy application based
on Django.
Some explanation:
Application can be used by several tenants (tenant1, tenant2, ...,).
All tenant-individual data has to be secured against access of other tenants (and their users).
Optionally tenants can create additional custom-fields for application-objects.
Of course, underlying hardware limits number of tenants on one "system".
1) Separating each tenant by e.g. sub-domain and using tenant-specific databases in the underlying layer
2) Using some tenant-ID in the model to separate the tenant-data in the database
I am thinking about deployment-processes, performance of the system-parts (web-server(s), database-server(s), working-node(s),...)
What would be the best setup ? Where are the pro's and con's?
What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们使用以下架构构建了一个多租户平台。我希望你能找到一些有用的提示。
(r'^(?P< ;tenant_id>[\w\-]+)
threading.local
)login_required
)、中间件或工厂来保护视图并选择正确的模型关于环境,我们使用以下设置:
从我的角度来看,这个设置有关注专业人士和缺点:
优点:
缺点:
当然,最好的架构很大程度上取决于您的需求,如租户数量、模型增量、安全要求等。
更新:在我们审查架构时,我建议不要重写第 2-3 点中所示的 URL。我认为更好的解决方案是将
tenant_id
作为请求标头,并使用request.META 之类的内容从请求中提取(第 4 点)
。通过这种方式,您可以获得中性 URL,并且可以更轻松地使用 Django 内置函数(例如tenant_id
。获取('TENANT_ID',无){% url ...%}
或reverse()
)或外部应用程序。We built a multitenancy platform using the following architecture. I hope you can find some useful hints.
(r'^(?P<tenant_id>[\w\-]+)
threading.local
)login_required
), middlewares or factories to protect views and select the right modelsRegarding the environment we use the following setup:
From my point of view this setup has the following pro's and con's:
Pro:
Contra:
Of course the best architecture strongly depends on your requirements as number of tenants, the delta of your models, security requirements and so on.
Update: As we reviewed our architecture, I suggest to not rewrite the URL as indicated in point 2-3. I think a better solutions is to put the
tenant_id
as a Request Header and extract (point 4) thetenant_id
out of the request with something likerequest.META.get('TENANT_ID', None)
. This way you get neutral URLs and it's much easier to use Django built-in functions (e.g.{% url ...%}
orreverse()
) or external apps.以下是相关讨论的一些提示:
mezzanine.utils.sites.current_site_id
、mezzanine.core.models.SiteRelated
和mezzanine.core.request
Here are some pointers to related discussions:
mezzanine.utils.sites.current_site_id
,mezzanine.core.models.SiteRelated
andmezzanine.core.request
我建议查看 https://github.com/bcarneiro/django-tenant-schemas。它会解决你的问题,有点像 Reto 提到的,除了它使用 postgresql 模式。
I recommend taking a look at https://github.com/bcarneiro/django-tenant-schemas. It will solve your problems a bit like Reto mentioned, except that it uses postgresql schemas.