简体   繁体   English

Spring 引导仅使用具有两个数据源的主数据源

[英]Spring boot only uses primary datasource with two datasources

I have two MySQL datasources in Spring Boot, therefore I have two config classes.我在 Spring 引导中有两个 MySQL 数据源,因此我有两个配置类。 But it looks like its only using the primary datasource.但看起来它只使用主数据源。 All entities are created for the primary datasource, so both crawlerdb and userdb entities are created in the userdb.所有实体都是为主数据源创建的,因此 crawlerdb 和 userdb 实体都是在 userdb 中创建的。

My userdb primary config:我的 userdb 主要配置:

/**
 * Data source for the MySQL User Database schema
 */
@Configuration
@EntityScan(basePackages = "cs.crawler.server.projectcs.domain.userdb")
@EnableJpaRepositories(basePackages = "cs.crawler.server.projectcs.repos.userdb")
@EnableTransactionManagement
public class UserDomainConfig {

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.users")
    public DataSourceProperties userDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.users.configuration")
    public HikariDataSource firstDataSource(DataSourceProperties firstDataSourceProperties) {
        return firstDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

}

My secondary crawlerdb config:我的辅助爬虫数据库配置:

/**
 * Data source for the MySQL crawler database schema
 */
@Configuration
@EntityScan(basePackages = "cs.crawler.server.projectcs.domain.crawlerdb")
@EnableJpaRepositories(basePackages = "cs.crawler.server.projectcs.repos.crawlerdb")
@EnableTransactionManagement
public class CrawlerDomainConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.crawler")
    public DataSourceProperties crawlerDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.crawler.configuration")
    public HikariDataSource secondDataSource(DataSourceProperties secondDataSourceProperties) {
        return secondDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

}

The entity classes for both schema's are in different packages as shown in @EntityScan above the class name.两种模式的实体类位于不同的包中,如@EntityScan名称上方的 @EntityScan 所示。 But when I check MySQL workbench for the created schema's I see that all entities are created in the userdb.但是当我检查 MySQL 工作台以获取创建的模式时,我看到所有实体都是在 userdb 中创建的。

I fixed my issues by changing the config files like this aticle:我通过更改像这篇文章这样的配置文件来解决我的问题:

https://springframework.guru/how-to-configure-multiple-data-sources-in-a-spring-boot-application/ https://springframework.guru/how-to-configure-multiple-data-sources-in-a-spring-boot-application/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM