首页  编辑  

MongoDB多数据源

Tags: /Java/   Date Created:
参阅: java - Spring Boot Multiple Datasource - Stack Overflow
java - How to use multiple mongodb databases in spring boot application? - Stack Overflow

Please follow below steps to setup multiple mongodb data sources.

  1. Define your primary and secondary mongodb properties like below in application.properties, please replace with them your db details:

    ######Primary Mongo DB########################
    spring.data.mongodb.host=localhost
    spring.data.mongodb.database=primary
    spring.data.mongodb.port=27017
    spring.data.mongodb.password=*******
    spring.data.mongodb.username=*******
    
    ###########Secondary MongoDB#####################
    mongodb.host=localhost
    mongodb.port=27017
    mongodb.database=secondary
    mongodb.username=******
    mongodb.password=******
    
  2. Now add Multiple Mongo Db Configuration..

    @Configuration
    public class MultipleMongoConfig {
    
    @Primary
    @Bean(name = "primary")
    @ConfigurationProperties(prefix = "spring.data.mongodb")
    public MongoProperties getPrimary() {
        return new MongoProperties();
    }
    
    @Bean(name = "secondary")
    @ConfigurationProperties(prefix = "mongodb")
    public MongoProperties getSecondary() {
        return new MongoProperties();
    }
    
    @Primary
    @Bean(name = "primaryMongoTemplate")
    public MongoTemplate primaryMongoTemplate() throws Exception {
        return new MongoTemplate(primaryFactory(getPrimary()));
    }
    
    @Bean(name = "secondaryMongoTemplate")
    public MongoTemplate secondaryMongoTemplate() throws Exception {
        return new MongoTemplate(secondaryFactory(getSecondary()));
    }
    
    @Bean
    @Primary
    public MongoDbFactory primaryFactory(final MongoProperties mongo) throws Exception {
        return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                mongo.getDatabase());
    }
    
    @Bean
    public MongoDbFactory secondaryFactory(final MongoProperties mongo) throws Exception {
        return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                mongo.getDatabase());
    }
    

    }

  3. Now enable EnableMongoRepositories for your primary an secondary.please make sure you change basePackages = "com.example.springbootmultipledatasource.primary.repository" your repository package here

    @Configuration
    @EnableMongoRepositories(basePackages = 
    "com.example.springbootmultipledatasource.primary.repository",
        mongoTemplateRef = "primaryMongoTemplate")
     public class PrimaryMongoConfig {
    
     }
    

Secondary Mongo Template:Please make sure you change your secondary repository package here basePackages = "com.example.springbootmultipledatasource.secondary.repository

    @Configuration
    @EnableMongoRepositories(basePackages = "com.example.springbootmultipledatasource.secondary.repository",
        mongoTemplateRef = "secondaryMongoTemplate")
   public class SecondaryMongoConfig {
   }

Now you can create your document, repository, service, controller and you are good to go.below is my project structure you can create or have different.

Project Structure