springboot MongoDB 多数据源配置

转载自:https://javacfox.github.io/2019/06/08/%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90mongodb%E7%9A%84%E4%BD%BF%E7%94%A8/

叙述

下面给出基于 springboot 的 MongoDB 多数据源配置解决方案

解决方案

pom.xml配置



    org.projectlombok
    lombok
    1.18.6
    provided



    org.springframework.boot
    spring-boot-starter-data-mongodb



    org.springframework.boot
    spring-boot-configuration-processor
    true



    org.springframework.boot
    spring-boot-autoconfigure
    2.0.3.RELEASE

数据源配置

mongodb:
  primary:
    host: localhost
    port: 27017
    database: test
  secondary:
    host: localhost
    port: 27017
    database: data

配置实体类

import lombok.Data;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;

@Data //生成get、set方法等
public class MultipleMongoProperties {

    private MongoProperties primary = new MongoProperties();
    private MongoProperties secondary = new MongoProperties();

}

配置第一个数据源

@Configuration
@EnableMongoRepositories(basePackages = "com.tedu.huawei.repository.primary",
        mongoTemplateRef = PrimaryMongoConfig.MONGO_TEMPLATE) //basePackages对应第一个库的repository所在的地址
public class PrimaryMongoConfig {
    protected static final String MONGO_TEMPLATE = "primaryMongoTemplate";
}

配置第二个数据源

@Configuration
@EnableMongoRepositories(basePackages = "com.tedu.huawei.repository.secondary",
        mongoTemplateRef = SecondaryMongoConfig.MONGO_TEMPLATE)
public class SecondaryMongoConfig {
    protected static final String MONGO_TEMPLATE = "secondaryMongoTemplate";
}

整合多数据源到MongoTemplate

import com.mongodb.MongoClient;
import com.tedu.huawei.entity.MultipleMongoProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

@Configuration
public class MultipleMongoConfig {

    @Autowired
    private MultipleMongoProperties mongoProperties;

    @Bean
    @ConfigurationProperties(prefix = "mongodb")
    public MultipleMongoProperties connectionSettings(){
        return new MultipleMongoProperties();

    }

    @Primary
    @Bean(name = PrimaryMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate primaryMongoTemplate() throws Exception {
        return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
    }

    @Bean
    @Qualifier(SecondaryMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate secondaryMongoTemplate() throws Exception {
        return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
    }

    @Bean
    @Primary
    public MongoDbFactory primaryFactory(MongoProperties mongo) throws Exception {
        return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                mongo.getDatabase());
    }

    @Bean
    public MongoDbFactory secondaryFactory(MongoProperties mongo) throws Exception {
        return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                mongo.getDatabase());
    }
}

创建两个repository

springboot MongoDB 多数据源配置_第1张图片

你可能感兴趣的:(Spring相关)