制作spring-data-mongodb多租户 [英] Making spring-data-mongodb multi-tenant

查看:690
本文介绍了制作spring-data-mongodb多租户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发布去年八月 sbzoom 提出了一个使spring-data-mongoDB多租户的解决方案:

In a post last august sbzoom proposed a solution to make spring-data-mongoDB multi-tenant:

你必须创建自己的RepositoryFactoryBean。这是 Spring Data MongoDB参考文档。您仍然需要实现自己的MongoTemplate并延迟或删除ensureIndexes()调用。但是你必须重写一些类来确保你的MongoTemplate被调用而不是Spring的。

"You have to make your own RepositoryFactoryBean. Here is the example from the Spring Data MongoDB Reference Docs. You would still have to implement your own MongoTemplate and delay or remove the ensureIndexes() call. But you would have to rewrite a few classes to make sure your MongoTemplate is called instead of Spring's."

有没有人实现这个或类似的东西?

Did anybody implement this or something equivalent?

推荐答案

有很多方法可以让猫在这里。它基本上归结为您希望应用租赁的级别。

There's quite a few ways to skin the cat here. It essentially all boils down to on which level you'd like to apply the tenancy.

基本方法是在每个线程的基础上绑定某种识别客户的密钥,以便您可以了解当前执行线程所处理的客户。这通常通过使用一些身份验证相关信息填充 ThreadLocal 来实现,因为您通常可以从登录用户派生租户。

The basic approach is to bind some kind of key identifying the customer on a per-thread basis, so that you can find out about the customer the current thread of execution deals with. This is usually achieved by populating a ThreadLocal with some authentication related information as you can usually derive the tenant from the logged in user.

现在,如果有的话,有几个选项可以应用租户知识。让我简要概述一下最常见的:

Now if that's in place there's a few options of where to apply the tenant knowledge. Let me briefly outline the most common ones:

分离的一种方法多个客户端的数据是每个租户拥有单独的数据库。 Spring Data MongoDB的核心抽象是 MongoDBFactory 接口。这里最简单的方法是覆盖 SimpleMongoDbFactory.getDb(String name)并使用数据库名称调用父方法,例如由租户前缀等丰富。

One way to separate data for multiple clients is to have individual databases per tenant. Spring Data MongoDB's core abstraction for this is the MongoDBFactory interface. The easiest way here is to override SimpleMongoDbFactory.getDb(String name) and call the parent method with the database name e.g. enriched by the tenant prefix or the like.

另一种选择是有租户特定的集合,例如通过租户预先或后缀。实际上可以通过在 @Document 注释的 collectionName 属性中使用Spring Expression语言(SpEl)来利用此机制。首先,通过Spring bean公开租户前缀:

Another option is to have tenant specific collections, e.g. through tenant pre- or postfixes. This mechanism can actually be leveraged by using the Spring Expression language (SpEl) in the @Document annotation's collectionName attribute. First, expose the tenant prefix through a Spring bean:

 @Component("tenantProvider")
 public class TenantProvider {

   public String getTenantId() {
     // … implement ThreadLocal lookup here
   }
 }

然后在您的域类型中使用SpEL @Document 映射:

Then use SpEL in your domain types @Document mapping:

 @Document(collectionName = "#{tenantProvider.getTenantId()}_accounts"
 public class Account { … }

SpEl允许您按名称引用Spring bean并对它们执行方法。 MongoTemplate (以及存储库抽象是传递的)将使用文档类的映射元数据,映射子系统将评估 collectionName 属性,以找出要与之交互的集合。

SpEl allows you to refer to Spring beans by name and execute methods on them. MongoTemplate (and thus the repository abstraction transitively) will use the mapping metadata of the document class and the mapping subsystem will evaluate the collectionName attribute to find out about the collection to interact with.

这篇关于制作spring-data-mongodb多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆