如何使用spring boot和spring-data为mongodb连接到多个数据库

时间:2017-05-09 19:03:58

标签: mongodb spring-boot spring-data-mongodb

我在我的应用程序中使用spring-data mongo存储库和spring boot。对于单个数据库,我在application.properties中添加db配置。如果我需要添加另一个数据库,我该如何添加它?我如何告诉spring哪个数据模型/存储库适用于哪个数据库?

谢谢!

1 个答案:

答案 0 :(得分:0)

这是一个示例。

//This is like a base class for all your mongoconfigs.   

    public class MongoConfig  {
    private static final Logger LOG = LoggerFactory.getLogger(MongoConfig.class);

    @NoArgsConstructor
    @Getter
    @Setter
    class MongoDbProperties{
        private String host;
        private int port;
        private String dbName;
        private String dbUser;
        private String dbPwd;
    }

    Mongo createMongoClient(ServerAddress serverAddress, MongoCredential... credentials) {

        if (credentials != null && credentials.length > 0) {

            final List<MongoCredential> credentialList = new ArrayList<>();
            credentialList.addAll(Arrays.asList(credentials));

            LOG.info("Creating connection with credentials: " + credentialList);

            return new MongoClient(serverAddress, credentialList;

        } else {
            return new MongoClient(serverAddress);
        }
    }

    Mongo getMongoClient(MongoDbProperties mongoDbProperties) {
        LOG.info("Initializing mongo client for {}. Host={}, Port={} ", mongoDbProperties.getDbName(), mongoDbProperties.getHost(), mongoDbProperties.getPort());
        Mongo mongoClient;
        if(!mongoDbProperties.getDbUser().isEmpty() && !mongoDbProperties.getDbPwd().isEmpty()) {
            MongoCredential credential = MongoCredential.createCredential(mongoDbProperties.getDbUser(), mongoDbProperties.getDbName(), mongoDbProperties.getDbPwd().toCharArray());
            mongoClient = createMongoClient(new ServerAddress(mongoDbProperties.getHost(), mongoDbProperties.getPort()), credential);
        }
        else
            mongoClient = createMongoClient(new ServerAddress(mongoDbProperties.getHost(), mongoDbProperties.getPort()));

        LOG.info("Initializing mongo template for products dataBase={} ", mongoDbProperties.getDbName());
        return mongoClient;
    }


    @Bean
    @ConfigurationProperties(prefix = "mongo.default")
    public MongoOptionsProperties defaultMongoOptionsProperties() {
        return new MongoOptionsProperties();
    }
 }

然后像你想要连接的mongo数据库那样拥有尽可能多的配置文件:

@Configuration
@EnableMongoRepositories(basePackages = "com.product.repository.shop.swiss", mongoTemplateRef = "swissMongoProductTemplate")
class SWissProductMongoConfig extends MongoConfig {

    @Bean(name = "swissMongoProductTemplate")
    public MongoTemplate swissMongoProductTemplate() throws Exception {
        MongoDbProperties mongoDbProperties = shopSWissProductMongoProperties();
        Mongo mongoClient = getMongoClient(mongoDbProperties);
        return new MongoTemplate(mongoClient, mongoDbProperties.getDbName());
    }

        @Bean
        @ConfigurationProperties(prefix = "mongo.product.swiss")
        public MongoDbProperties shopSWissProductMongoProperties(){
            return new MongoDbProperties();
        }
    }

@Configuration
@EnableMongoRepositories(basePackages = "com.product.repository.shop.france", mongoTemplateRef = "franceMongoProductTemplate")
class SWissProductMongoConfig extends MongoConfig {

    @Bean(name = "franceMongoProductTemplate")
    public MongoTemplate franceMongoProductTemplate() throws Exception {
        MongoDbProperties mongoDbProperties = shopFranceProductMongoProperties();
        Mongo mongoClient = getMongoClient(mongoDbProperties);
        return new MongoTemplate(mongoClient, mongoDbProperties.getDbName());
    }

    @Bean
    @ConfigurationProperties(prefix = "mongo.product.france")
    public MongoDbProperties shopFranceProductMongoProperties() {
        return new MongoDbProperties();
    }
}

yml

中有这样的内容
mongo:
  product:
    france :
      dbName: 
      host: 
      port: 
      dbUser:
      dbPwd:
    swiss:
      dbName: 
      host: 
      port: 
      dbUser: 
      dbPwd: 

确保在注释basePackages的{​​{1}}属性中提到的包中包含存储库类