基于spring数据的多租户mongodb数据库 [英] Multi-tenant mongodb database based with spring data
问题描述
我想制作使用 mongodb 和 spring-data multitenat 的 Web 应用程序.
I would like to make my web application that is using mongodb and spring-data multitenat.
基本上我想将我所有的实体(集合)复制到不同的数据库中.然后(基于一些规则,例如登录系统的用户)我想将一些实体实例(文档)存储到正确的数据库中.
Basically I would like to replicate all my entities(collections) into different databases. Then (based on some rule as for example the user that is logged in the system) I want to store some entity instance (document) into the right database.
例如,我有一个名为 DBNameProviderService.getDbName() 的服务,它动态地返回数据库的名称.如何使用此服务动态选择正确的 Db?
For example I have a Service Called DBNameProviderService.getDbName() that dynamically returns me the name of the database. How can I use this service to dynamically select the correct Db?
已编辑
如果不够清楚,请见谅.也许举个例子会更清楚.
Sorry if it is not enough clear. Maybe with an example will be more clear.
在我的系统中,我可以注册/登录用户(它们存储在名为 general_db 的数据库中的 User 集合中).
In my system I can registe/login users (they are stored stored in the User collection in database named general_db).
我还有一个实体 A(标记为多租户,只是简单地扩展了一个接口).如果我以 User1 身份登录,我希望始终将 A 实例存储在集合 A 中的数据库 User1_db 中,而如果我以 User2 身份登录并希望将 A 实例存储在集合 A 中的 User2_db 中.
I have also an entity A (marked as multitenant simply extending an interface). If I am logged in as User1 I want to store an A instance always in the Database User1_db in the collection A, while if I am logged in as User2 and I want to store an A instance in User2_db in the collection A.
所以在这种情况下,问题是我不能为每个用户创建一个 mongotemplate bean,因为用户可以在系统中注册,所以它是动态的.我需要 MongoTemplate 动态选择数据库.
So in this scenario the problem is that I cannot create a mongotemplate bean for each user because users can register in the system, so it is dynamic. I need that the MongoTemplate choose dynamically the database.
推荐答案
你应该看看我的 github 项目,我有一个非常相似的问题.
You should take a look at my github project, I had a very similar problem.
https://github.com/Loki-Afro/multi-tenant-spring-mongodb
这篇关于基于spring数据的多租户mongodb数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!