2017-05-09 71 views
1

我在我的應用程序中使用彈簧數據mongo存儲庫和彈簧引導。對於單個數據庫,我在application.properties中添加了數據庫配置。如果我需要添加另一個數據庫,我該如何添加它?我該如何告訴spring哪些數據模型/存儲庫適用於哪個數據庫?我如何連接到多個數據庫使用spring引導和彈簧數據mongodb

謝謝!

+0

我認爲這是已經回答的basePackages屬性中提到的包庫類。 檢查http://stackoverflow.com/questions/27614301/spring-boot-multiple-datasource – Bhargav

+0

請參閱[此Github存儲庫](https://github.com/binarycodifier/multiple-mongodb-databases-using-spring-data )爲例。 – manish

+0

[Spring Boot配置和使用兩個DataSources]的可能重複(http://stackoverflow.com/questions/30337582/spring-boot-configure-and-use-two-datasources) –

回答

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 daabases。

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

有這樣的事情在你的陽明

mongo: 
    product: 
    france : 
     dbName: 
     host: 
     port: 
     dbUser: 
     dbPwd: 
    swiss: 
     dbName: 
     host: 
     port: 
     dbUser: 
     dbPwd: 

請確保您有在annotaion EnableMongoRepositories

相關問題