如何在Spring Boot应用程序中使用多个mongodb数据库? [英] How to use multiple mongodb databases in spring boot application?

查看:562
本文介绍了如何在Spring Boot应用程序中使用多个mongodb数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要使用两个MongoDB数据库。我不知道如何在spring应用程序的application.properties文件中添加2个MongoDB数据库。

In my application, I need to use two MongoDB databases. I don't know how to add 2 MongoDB databases in the application.properties file in the spring application.

这是我项目的application.properties文件,

Here is the application.properties file of my project,

spring.data.mongodb.database=DB1
spring.data.mongodb.authentication-database=DB1
spring.data.mongodb.host=dev-ng-mongo1.domain.com
spring.data.mongodb.password=9876512
spring.data.mongodb.port=27017 
spring.data.mongodb.username=pavan

但我想为同一项目使用另一个MongoDB数据库。如何在application.properties文件中添加新数据库。

but I want to use another MongoDB database for the same project. How can I add the new database in the application.properties file.

推荐答案

请按照以下步骤设置多个mongodb数据源。

Please follow below steps to setup multiple mongodb data sources.


  1. 在application.properties中定义您的主要和辅助mongodb属性,如下所示,请用它们替换您的数据库详细信息:

  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=******


  • 现在添加多个Mongo Db配置。

  • 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());
    }
    

    }

    现在为您的主数据库启用 EnableMongoRepositories 。请确保您更改 basePackages = com.example.springbootmultipledatasource.primary.repository 您的存储库软件包

    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 {
    
     }
    


  • 二级Mongo模板:请确保在此处更改二级存储库软件包 basePackages = com.example.springbootmultipledatasource.secondary.repository

    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.

    这篇关于如何在Spring Boot应用程序中使用多个mongodb数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆