Spring Boot 2多个数据源初始化架构

时间:2018-07-03 03:28:11

标签: java database spring spring-boot initialization

我有一个Spring Boot 2应用程序,它使用两个数据源-一个Oracle和一个H2。 H2数据源设置为辅助数据源,我想在启动时为其创建架构,但是它永远不会触发schema.sql文件。这是我的数据源配置文件:

@Configuration
public class DataSourceConfig {
    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties primaryDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSource primaryDataSource() {
        return primaryDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    @ConfigurationProperties("spring.runlogdatasource")
    public DataSourceProperties runlogDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("spring.runlogdatasource")
    public DataSource runlogDataSource() {
        return runlogDataSourceProperties().initializeDataSourceBuilder().build();
    }
}

这是我的application.properties文件:

spring.datasource.url=jdbc:oracle:thin:@my.database.com:1521/mydb
spring.datasource.username=test
spring.datasource.password=ENC(3PXcnoBndLpWN1EcMtmIn+odOwhdWjSrqANijutxuekKEIOco64Jew==)
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.initialization-mode=never
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=10
jasypt.encryptor.bean=stringEncryptor
spring.runlogdatasource.url=jdbc:h2:~/runlogdb;CIPHER=AES;DB_CLOSE_ON_EXIT=FALSE;
spring.runlogdatasource.username=sa
spring.runlogdatasource.password=ENC(3PXcnoBndLpWN1EcMtmIn+odOwhdWjSrqANijutxuekKEIOco64Jew==)
spring.runlogdatasource.driverClassName=org.h2.Driver
spring.runlogdatasource.platform=h2
spring.runlogdatasource.schema=classpath:schema-h2.sql

对于主数据源,我可以看到它进入了DataSourceInitializerInvoker,它试图在其中加载架构,但是由于没有schema-all.sql文件,它会跳过该数据库。但是,我确实有一个schema-h2.sql文件,但是对于辅助数据源,它从不输入DataSourceInitializerInvoker,因此从不尝试初始化该模式。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

这是预期的行为。指定主要数据源后,仅执行该数据源的架构和数据。要具有此行为,您需要做的是为两个数据源中的每一个定义DataSourceInitializer

        @Bean
        public DataSourceInitializer dataSourceInitializer1(@Qualifier("datasource1") DataSource datasource) {
            ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
            resourceDatabasePopulator.addScript(new ClassPathResource("schema-h22.sql"));
            resourceDatabasePopulator.addScript(new ClassPathResource("data-h22.sql"));

                DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
                dataSourceInitializer.setDataSource(datasource);
                dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
                return dataSourceInitializer;
        }

    @Bean
    public DataSourceInitializer dataSourceInitializer2(@Qualifier("datasource2") DataSource datasource) {
        ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(new ClassPathResource("schema-h21.sql"));
        resourceDatabasePopulator.addScript(new ClassPathResource("data-h21.sql"));

            DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
            dataSourceInitializer.setDataSource(datasource);
            dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
            return dataSourceInitializer;
    }

您还需要禁用默认的spring数据初始化。您可以通过spring.datasource.initialization-mode=never

答案 1 :(得分:0)

您可以使用多个与数据源相对应的 Liquibase 配置。这可以帮助您初始化两个数据源的模式

示例:-

...

@Bean
@ConfigurationProperties(prefix = "spring.datasource.liquibase")
public LiquibaseProperties primaryLiquibaseProperties() {
    return new LiquibaseProperties();
}

@Bean
public SpringLiquibase primaryLiquibase() {
    return springLiquibase(primaryDataSource(), primaryLiquibaseProperties());
}
@Bean
@ConfigurationProperties(prefix = "spring.runlogdatasource.liquibase")
public LiquibaseProperties secondaryLiquibaseProperties() {
    return new LiquibaseProperties();
}

@Bean
public SpringLiquibase secondaryLiquibase() {
    return springLiquibase(secondaryDataSource(), secondaryLiquibaseProperties());
}

private static SpringLiquibase springLiquibase(DataSource dataSource, LiquibaseProperties properties) {
    SpringLiquibase liquibase = new SpringLiquibase();
    liquibase.setDataSource(dataSource);
    liquibase.setChangeLog(properties.getChangeLog());
    liquibase.setContexts(properties.getContexts());
    liquibase.setDefaultSchema(properties.getDefaultSchema());
    liquibase.setDropFirst(properties.isDropFirst());
    liquibase.setShouldRun(properties.isEnabled());
    liquibase.setLabels(properties.getLabels());
    liquibase.setChangeLogParameters(properties.getParameters());
    liquibase.setRollbackFile(properties.getRollbackFile());
    return liquibase;
}
...

U可以按以下方式编辑您的属性:

**spring.datasource.liquibase.change-log=** classpath:/db/changelog/db.primary.changelog-master.xml
**spring.runlogdatasource.liquibase.change-log=** classpath:/db/changelog/db.secondary.changelog-master.xml