当前位置: 首页 > 知识库问答 >
问题:

如何在Spring中为多个数据源设置liquibase?

诸葛苏燕
2023-03-14

我需要在Spring中为两个DataSources设置LiquiBase,目前看来只有一个LiquiBase设置是可能的,您可以选择哪个数据源。

共有1个答案

狄晟睿
2023-03-14

如果您正在使用spring boot,以下是可以帮助您的设置:

配置类:

@Configuration
public class DatasourceConfig {

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

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

    @Bean
    public SpringLiquibase primaryLiquibase() {
        return springLiquibase(primaryDataSource(), primaryLiquibaseProperties());
    }

    @Bean
    @ConfigurationProperties(prefix = "datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "datasource.secondary.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;
    }


...

}

属性.yml

datasource:
  primary:
    url: jdbc:mysql://localhost/primary
    username: username
    password: password
    liquibase:
      change-log: classpath:/db/changelog/db.primary.changelog-master.xml
  secondary:
    url: jdbc:mysql://localhost/secondary
    username: username
    password: password
    liquibase:
      change-log: classpath:/db/changelog/db.secondary.changelog-master.xml
 类似资料:
  • 我还向b_spring.xml声明了另一个entityManagetFactory、事务管理器和dataSource。 误差 bean初始化失败;嵌套异常是org.springframework.beans.factory.nosuchbeanDefinitionException:没有定义[javax.persistence.entityManagerFactory]类型的唯一bean:预期的单

  • 问题内容: 我正在尝试使用此网络研讨会中概述的单独的架构方法向Java应用程序添加多租户 我想知道如何通过spring来配置多个数据源,也许是通过使用属性文件并基于租户id从spring上下文中获取数据源。 更重要的是,尽管我希望能够配置支持此多租户功能的自定义连接提供程序实现,以供Hibernate使用,而不是默认使用的注入功能。 我怎样才能做到这一点。 问题答案: 使用。

  • 问题内容: 我正在尝试将HikariCP与Spring一起用于连接池。我正在使用jdbcTempLate和JdbcdaoSupport。 这是我用于数据源的spring配置文件: 但不幸的是,正在生成以下错误消息: 谁能告诉我如何解决这个问题? 问题答案: 你需要在bean配置上编写此结构(这是你的数据源): 这是我的示例,正​​在运行。你只需要将属性放在hibernate.properties上

  • 我有一个Spring Boot应用程序,它连接到两个独立的数据库。虽然为了自定义Tomcat JDBC连接池设置,我必须手动配置它(因为通过定义多个数据源,引导自动配置将被忽略,并且Spring Boot不再从application.properties读取特定于Tomcat的属性),但所有操作都很正常(我遵循了文档和教程中的步骤)。 由于Spring内部的多层抽象,我很难对此进行调试。我有Ecl

  • 如何配置执行器/health以显示两个数据源的health状态?

  • 我有不同的数据库连接,但在他们相同的表。因此,为了重用这些实体类,我必须动态地获取模式名称。试图在属性文件和数据源中设置架构,但不起作用。 应用属性文件: 尝试在属性文件中设置模式如下,并获得错误"不支持DDM参数值。DDM参数代码点具有不支持的值:0x2110."错误。 在配置类中也尝试了如下操作,但不起作用。