Spring Boot多个MongoDB配置

时间:2018-02-22 01:44:18

标签: spring mongodb spring-boot configuration

我到处寻找这个,看起来我找不到有效的解决方案。我正在使用spring boot 1.5.10-RELEASE。我试图在同一个应用程序中配置两个不同的mongodb实例。这是我的代码:

主要应用:

@SpringBootApplication(exclude = {MongoAutoConfiguration.class})
@ComponentScan("com.reef.reports")
public class MainApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(MainApplication.class, args);
    }
}

第1实例

@Configuration
@EnableMongoRepositories(basePackages = {"com.reef.repository.mongousa"} , mongoTemplateRef = "USAMongo")
public class MongoUsaConfig
{

    @Value("${usa.mongodb.host}")
    private String host;

    @Value("${usa.mongodb.database:reef}")
    private String database;

    @Value("${usa.mongodb.port:27017}")
    private int port;

    @Value("${usa.mongodb.username:}")
    private String username;

    @Value("${usa.mongodb.password:}")
    private String password;

    @Value("${usa.mongodb.authdb:}")
    private String authdb;

    private final List<MongoCredential> credentials = new ArrayList<>();
    private final List<ServerAddress>   hosts = new ArrayList<>();

    /**
     * Method that creates MongoDbFactory
     * Common to both of the MongoDb connections
     */
    public MongoDbFactory mongoDbFactory()
    {
        return new SimpleMongoDbFactory(getMongoClient(), database);
    }

    /**
     * Method that creates MongoClient
     */
    @Bean(name = "USAClient")
    public MongoClient getMongoClient()
    {

        if ((null != username)&&(!username.isEmpty()))
        {
            hosts.add(new ServerAddress(host, port));
            credentials.add(MongoCredential.createMongoCRCredential(username, authdb, password.toCharArray()));
            return new MongoClient(hosts, credentials);
        }
        else
        {
            return new MongoClient(host, port);
        }
    }

    @Primary
    @Bean(name = "USAMongo")
    public MongoTemplate getMongoTemplate()
    {
        return new MongoTemplate(mongoDbFactory());
    }

}

第二个实例

@Configuration
@EnableMongoRepositories(basePackages = {"com.reef.repository.mongocan"} , mongoTemplateRef = "CANMongo")
public class MongoCanConfig
{

    @Value("${can.mongodb.host}")
    private String host;

    @Value("${can.mongodb.database:reef}")
    private String database;

    @Value("${can.mongodb.port:27017}")
    private int port;

    @Value("${can.mongodb.username:}")
    private String username;

    @Value("${can.mongodb.password:}")
    private String password;

    @Value("${can.mongodb.authdb:}")
    private String authdb;

    private final List<MongoCredential> credentials = new ArrayList<>();
    private final List<ServerAddress>   hosts = new ArrayList<>();

    /**
     * Method that creates MongoDbFactory
     * Common to both of the MongoDb connections
     */
    public MongoDbFactory mongoDbFactory()
    {
        return new SimpleMongoDbFactory(getMongoClient(), database);
    }

    /**
     * Method that creates MongoClient
     */
    @Bean(name = "CANClient")
    public MongoClient getMongoClient()
    {

        if ((null != username)&&(!username.isEmpty()))
        {
            hosts.add(new ServerAddress(host, port));
            credentials.add(MongoCredential.createMongoCRCredential(username, authdb, password.toCharArray()));
            return new MongoClient(hosts, credentials);
        }
        else
        {
            return new MongoClient(host, port);
        }
    }

    @Bean(name = "CANMongo")
    public MongoTemplate getMongoTemplate()
    {
        return new MongoTemplate(mongoDbFactory());
    }
}

当我运行应用程序时,它将运行第一个实例的配置。但是,它不会接收第二个实例。我已经设置断点进行调试,它永远不会遇到配置中的断点。正确加载此包中的存储库:

com.reef.repository.mongousa

此程序包中的存储库发生错误:

com.reef.repository.mongocan

请让我知道我错过了什么。为什么一个配置工作而另一个配置没有?

1 个答案:

答案 0 :(得分:0)

请参阅此博客文章https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7

我尝试了两个MySQL数据库,它运行正常。