Springboot2.x之Mongodb多数据源配置

Springboot2.x之Mongodb多数据源配置,具体如下:

1. repository 一定要放到对应包里才能生效

2. 需要在 Main 类上增加配置: 

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, MongoDataAutoConfiguration.class})

3. 数据源配置:

#spring.data.mongodb.first.database=node1
spring.data.mongodb.first.uri=mongodb://localhost:27017/node1?minPoolSize=5&maxPoolSize=20

#spring.data.mongodb.second.database=node2
spring.data.mongodb.second.uri=mongodb://localhost:27017/node2?minPoolSize=5&maxPoolSize=20

#spring.data.mongodb.third.database=node3
spring.data.mongodb.third.uri=mongodb://localhost:27017/node3?minPoolSize=5&maxPoolSize=20

#spring.data.mongodb.fourth.database=node4
spring.data.mongodb.fourth.uri=mongodb://localhost:27017/node4?minPoolSize=5&maxPoolSize=20

 

4. 代码实现:

package cn.net.springboot.data.migrate.multimongo;

import com.mongodb.MongoClientURI;
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.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration
public class MultipleMongoConfig {
    // ================== MongoProperties 配置 ===================

    @Bean
    @ConfigurationProperties("spring.data.mongodb.first")
    public MongoProperties firstMongoProperties() {
        System.out.println("================== firstMongoProperties 配置 ===================");
        return new MongoProperties();
    }

    @Bean
    @ConfigurationProperties("spring.data.mongodb.second")
    public MongoProperties secondMongoProperties() {
        System.out.println("================== secondMongoProperties 配置 ===================");
        return new MongoProperties();
    }

    @Bean
    @ConfigurationProperties("spring.data.mongodb.third")
    public MongoProperties thirdMongoProperties() {
        System.out.println("================== thirdMongoProperties 配置 ===================");
        return new MongoProperties();
    }

    @Bean
    @ConfigurationProperties("spring.data.mongodb.fourth")
    public MongoProperties fourthMongoProperties() {
        System.out.println("================== fourthMongoProperties 配置 ===================");
        return new MongoProperties();
    }

    // ================== MongoDbFactory 配置 ===================

    @Bean
    public MongoDbFactory firstMongoDbFactory(MongoProperties firstMongoProperties) {
        System.out.println("================== firstMongoDbFactory 配置 ===================");
        return new SimpleMongoDbFactory(new MongoClientURI(firstMongoProperties.determineUri()));
    }

    @Bean
    public MongoDbFactory secondMongoDbFactory(MongoProperties secondMongoProperties) {
        System.out.println("================== secondMongoDbFactory 配置 ===================");
        return new SimpleMongoDbFactory(new MongoClientURI(secondMongoProperties.determineUri()));
    }

    @Bean
    public MongoDbFactory thirdMongoDbFactory(MongoProperties thirdMongoProperties) {
        System.out.println("================== thirdMongoDbFactory 配置 ===================");
        return new SimpleMongoDbFactory(new MongoClientURI(thirdMongoProperties.determineUri()));
    }

    @Bean
    public MongoDbFactory fourthMongoDbFactory(MongoProperties fourthMongoProperties) {
        System.out.println("================== fourthMongoDbFactory 配置 ===================");
        return new SimpleMongoDbFactory(new MongoClientURI(fourthMongoProperties.determineUri()));
    }

    // ================== MongoTemplate 配置 ===================

    @EnableMongoRepositories(
            basePackages = {
                    "cn.net.springboot.data.migrate.multimongo.dao.first",
                    "cn.net.springboot.data.migrate.convert.repository",
            },
            mongoTemplateRef = "firstMongoTemplate"
    )
    public class FirstMongoTemplate {
        @Bean
        public MongoTemplate firstMongoTemplate() {
            return new MongoTemplate(firstMongoDbFactory(firstMongoProperties()));
        }
    }

    @EnableMongoRepositories(basePackages = "cn.net.springboot.data.migrate.multimongo.dao.second", mongoTemplateRef = "secondMongoTemplate")
    public class SecondMongoTemplate {
        @Bean
        public MongoTemplate secondMongoTemplate() {
            return new MongoTemplate(secondMongoDbFactory(secondMongoProperties()));
        }
    }

    @EnableMongoRepositories(basePackages = "cn.net.springboot.data.migrate.multimongo.dao.third", mongoTemplateRef = "thirdMongoTemplate")
    public class ThirdMongoTemplate {
        @Bean
        public MongoTemplate thirdMongoTemplate() {
            return new MongoTemplate(thirdMongoDbFactory(thirdMongoProperties()));
        }
    }

    @EnableMongoRepositories(basePackages = "cn.net.springboot.data.migrate.multimongo.dao.fourth", mongoTemplateRef = "fourthMongoTemplate")
    public class FourthMongoTemplate {
        @Bean
        public MongoTemplate fourthMongoTemplate() {
            return new MongoTemplate(fourthMongoDbFactory(fourthMongoProperties()));
        }
    }
}

 

你可能感兴趣的:(spring,spring,boot,java)