前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MongoDB实现Multi tenant的方法

MongoDB实现Multi tenant的方法

作者头像
Jerry Wang
发布2019-11-29 10:26:20
9500
发布2019-11-29 10:26:20
举报

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://jerry.blog.csdn.net/article/details/103296329

this is an example on how to implement the multi tenancy by using MongoDB. Tenant separation is done by schema separation.

Tenant resolver to get the tenant for current request

代码语言:javascript
复制
@Component
 public class TenantResolver {
     public static final String HEADER_HYBRIS_TENANT = "hybris-tenant";
 
     private String defaultTenant;
 
     public String getCurrentTenant() {
         return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader(HEADER_HYBRIS_TENANT);
 
     }
 
 }
  1. Document annotation to specify the collection name
代码语言:javascript
复制
@Document(collection = "#{tenantResolver.getCurrentTenant()}_config")
public class Config {
...
}

Test:

Created different collections according to the tenant:

db.getCollectionNames()

[ “config”, “t1_config”, “t2_config”, “t3_config”, “users” ]

Data from different tenant is stored into the corresponding collection:

db.t1_config.find()

{ “_id” : “test1”, “_class” : “com.example.model.Config”, “value” : “value” }

db.t2_config.find()

{ “_id” : “test2”, “_class” : “com.example.model.Config”, “value” : “value” }

db.t3_config.find()

{ “_id” : “test3”, “_class” : “com.example.model.Config”, “value” : “value” }

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-11-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MongoDB
腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档