import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import lombok.extern.slf4j.Slf4j; import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory; @Slf4j public class MyMongoDbFactory extends SimpleMongoClientDbFactory { public MyMongoDbFactory(MongoClient mongoClient, String databaseName) { super(mongoClient, databaseName); } @Override protected void closeClient() { getMongoClient().close(); } @Override protected MongoDatabase doGetMongoDatabase(String dbName) { //获取租户信息 Long tenantId = TenantContextHandler.getCurrentTenantId(); if (tenantId != null) { MongoClient mongoClient = getMongoClient(); String databaseName = getDBName(tenantId); log.info("切换到租户[{}]的MongoDatabase [{}]", tenantId, databaseName); return mongoClient.getDatabase(databaseName); } else { log.info("切换到[主库]MongoDatabase [{}]", dbName); return getMongoClient().getDatabase(dbName); } } @Override protected MongoClient getMongoClient() { return super.getMongoClient(); } private static final String DBNAME_PREFIX = "TEST_"; /** * 获取当前的DatabaseName * * @return 数据库名 */ private static String getDBName(Long tenantId) { return DBNAME_PREFIX + tenantId; } }
import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; import java.util.regex.Matcher; import java.util.regex.Pattern; @Slf4j @Configuration public class TenantConfiguration { @Autowired private MongoProperties prop; @Autowired private MongoClient mongoClient; @Bean(name = "mongoTemplate") public MongoTemplate mongoTemplate() { return new MongoTemplate(mongoDbFactory()); } //主库工厂 @Bean(name = "mongoDbFactory") public MongoDbFactory mongoDbFactory() { String mongoUri = prop.getUri(); ////mongodb://username:password@hostname:port/dbname return new MyMongoDbFactory(mongoClient, getDefaultDBName(mongoUri)); } @Bean(name = "mongoClient") public MongoClient mongoClient(){ String mongoUri = prop.getUri(); log.info("创建主库mongoClient, uri:{}", mongoUri); return MongoClients.create(mongoUri); } private static final String patternStr = "mongodb://(.*)@(.*):(.*)/(.*)"; private static final Pattern pattern = Pattern.compile(patternStr); private String getDefaultDBName(String mongoUri) { String database = prop.getDatabase(); if (database == null) { Matcher m = pattern.matcher(mongoUri); if (m.find()) { database = m.group(4); } } return database; } }
/** * 租户信息,即用户当前所在的空间id */ public class TenantContextHandler { /** * 空间id信息 */ private static ThreadLocal<Long> tenantContext = new InheritableThreadLocal<>(); /** * 设置空间信息 * @param */ public static void setCurrentTenantId(Long tenantId) { tenantContext.set(tenantId); } /** * 获取当前租户id * * @return */ public static Long getCurrentTenantId() { return tenantContext.get(); } /** * 清除缓存数据 */ public static void clean() { tenantContext.remove(); } public static void refresh() { tenantContext = new InheritableThreadLocal<>(); } }
# yml配置 spring: data: mongodb: uri: mongodb://username:password@hostname:port/dbname database: TEST
spring boot版本:2.2.6
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>标签:功能,return,String,租户,Mongodb,static,mongodb,import,public From: https://www.cnblogs.com/zhangshiwen/p/17070334.html