写点什么

springboot+mongo 多数据源简单配置

用户头像
Mars
关注
发布于: 2021 年 06 月 01 日

背景

由于项目新老库迁移,需要配置 mongo 多数据源。受益于框架采用 springboot,简单实现,步骤如下。

实现

1. 依赖引入

<dependency>	<groupId>org.springframework.boot</groupId>	<artifactId>spring-boot-starter-data-mongodb</artifactId></dependency>
复制代码

2. MongoDBFactory

创建 mongoTemplate 工厂类 MongoDBFactory,通过传入 uri 方式,获得 MongoDBFactory 对象。

@Configurationpublic abstract class AbstractMongoConfig {        public MongoDatabaseFactory mongoDbFactory(String uri) {        // uri = "mongodb://name:pass@localhost:27017/test";        ConnectionString connectionStringObj = new ConnectionString(uri);        return new SimpleMongoClientDatabaseFactory(connectionStringObj);    }
abstract public MongoTemplate getMongoTemplate();}
复制代码

3. 注入 MongoTemplate

注入新数据源,其中 MappingMongoConverter 可自定义 mongo 转换器,主要负责存取 mongo 数据时的操作。比如 converter.setTypeMapper(new DefaultMongoTypeMapper(null)) 会将 mongo 数据中_class 字段去除。

@Slf4j@Configuration@EnableMongoRepositories(mongoTemplateRef = "newMongoTemplate")public class NewMongoConfig extends AbstractMongoConfig{
@Value("${spring.data.mongodb.new.host}") private String host; @Value("${spring.data.mongodb.new.port}") private String port; @Value("${spring.data.mongodb.new.database}") private String database; @Value("${spring.data.mongodb.new.username}") private String username; @Value("${spring.data.mongodb.new.password}") private String password;
@Override public @Bean(name = "newMongoTemplate") MongoTemplate getMongoTemplate() { StringBuffer stringBuffer = new StringBuffer("mongodb://"); stringBuffer.append(username).append(":") .append(password).append("@") .append(host).append(":") .append(port).append("/") .append(database); String uri = stringBuffer.toString(); log.info("mongo >> uri={}", uri); MappingMongoConverter converter = new MappingMongoConverter( new DefaultDbRefResolver(mongoDbFactory(uri)), new MongoMappingContext()); converter.setTypeMapper(new DefaultMongoTypeMapper(null));
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory(uri), converter);
return mongoTemplate; }}
复制代码

通过 @Primary,默认使用旧数据源

@Slf4j@Configuration@EnableMongoRepositories(mongoTemplateRef = "oldMongoTemplate")public class OldMongoConfig extends AbstractMongoConfig{
@Value("${spring.data.mongodb.old.host}") private String host; @Value("${spring.data.mongodb.old.port}") private String port; @Value("${spring.data.mongodb.old.database}") private String database; @Value("${spring.data.mongodb.old.username}") private String username; @Value("${spring.data.mongodb.old.password}") private String password;
@Override @Primary public @Bean(name = "oldMongoTemplate") MongoTemplate getMongoTemplate() { StringBuffer stringBuffer = new StringBuffer("mongodb://"); stringBuffer.append(username).append(":") .append(password).append("@") .append(host).append(":") .append(port).append("/") .append(database); String uri = stringBuffer.toString();
log.info("mongo >> uri={}", uri); MappingMongoConverter converter = new MappingMongoConverter( new DefaultDbRefResolver(mongoDbFactory(uri)), new MongoMappingContext()); converter.setTypeMapper(new DefaultMongoTypeMapper(null));
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory(uri), converter);
return mongoTemplate; }}
复制代码

4. 关闭 mongo 自动配置

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})public class ServerApplication extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(ServerApplication.class); }
public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); }
}
复制代码


发布于: 2021 年 06 月 01 日阅读数: 20
用户头像

Mars

关注

还未添加个人签名 2018.06.12 加入

还未添加个人简介

评论

发布
暂无评论
springboot+mongo多数据源简单配置