Spring Data MongoDB 基于集合的多租户 [英] Collection based multitenancy with Spring Data MongoDB

查看:45
本文介绍了Spring Data MongoDB 基于集合的多租户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的 Spring Boot 1.3.3 应用程序使用 Spring Data MongoDB 1.8.4 在 MongoDB (2.6 或 3.2) 上持久化数据.

Our Spring Boot 1.3.3 application persists data on MongoDB (2.6 ou 3.2) using Spring Data MongoDB 1.8.4.

我们需要支持多租户.我们选择使用基于集合"的多租户,即每个租户都有自己的集合集.例如,对于 Article 实体,集合是{tenantName}_articles".

We need to support multitenancy. We chose to use "collection based" multitenancy, i.e. each tenant has its own set of collection. For example for the Article entity, the collections are "{tenantName}_articles".

Oliver Gierke 在 Making spring-data-mongodb multi-租户 例如:

Oliver Gierke kindly explained an implementation in Making spring-data-mongodb multi-tenant using for example :

@Document(collectionName = "#{tenantProvider.getTenantId()}_articles")

这在纸面上非常好,但似乎不适用于现实生活中的应用,因为我发现了两个问题,一个是主要问题:

This is very nice on paper, but does not seem applicable for real life applications as I found two issues, one being major:

问题 1(我可以忍受):在应用程序启动时,Spring Boot 使数据库为具有自定义索引(例如 @Indexed 属性)的实体构建索引.但是在启动时,没有当前租户",因此 Spring Data 创建了不相关的集合,例如_articles".我们如何防止这种情况发生?

Issue 1 (I could live with that): at application startup Spring Boot makes the database build the indexes for entities that have custom indexes (such as @Indexed attributes). But at startup, there is no "current tenant" so Spring Data creates irrelevant collections such as "_articles". How can we prevent this?

问题 2(这里的主要问题):在运行时创建和使用诸如{tenantName}_articles"之类的多租户集合没有自定义索引(除了_id"上的默认 MongoDB 索引).我怀疑 Spring 在运行时会忽略索引,因为它认为它在启动时已经完成了这项工作.这是一个主要的性能问题.我们该如何解决这个问题?

Issue 2 (major probleme here): at runtime the multitenant collections such as "{tenantName}_articles" are created and used without the custom indexes (apart from the default MongoDB index on "_id"). I suspect Spring ignores indexes at runtime because it thinks it already did the job at startup. This is a major performance problem. How can we fix this?

感谢您的宝贵时间.

推荐答案

找到了为给定租户重新创建索引的方法:

Found a way to recreate the indexes for a given tenant:

String tenantName = ...;

MongoMappingContext mappingContext = (MongoMappingContext) mongoTemplate.getConverter().getMappingContext();
MongoPersistentEntityIndexResolver resolver = new MongoPersistentEntityIndexResolver(mappingContext);

for (BasicMongoPersistentEntity entity : mappingContext.getPersistentEntities()) {
    if (entity.findAnnotation(Document.class) == null) {
        // Keep only collection roots
        continue;
    }

    String collectionName = entity.getCollection();
    if (!collectionName.startsWith(tenantName)) {
        // Keep only dynamic entities
        continue;
    }

    IndexOperations indexOperations = mongoTemplate.indexOps(collectionName);
    for (MongoPersistentEntityIndexResolver.IndexDefinitionHolder holder : resolver.resolveIndexForEntity(entity)) {
        indexOperations.ensureIndex(holder.getIndexDefinition());
    }
}

我花了一些时间才弄清楚这一点.希望这会有所帮助.欢迎改进.

Took me some time to figure this out. Hope this will help. Improvements welcome.

这篇关于Spring Data MongoDB 基于集合的多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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