java 如何在spring batch 5中配置两个数据源?

uqdfh47h  于 2023-05-12  发布在  Java
关注(0)|答案(2)|浏览(272)

我们如何利用@EnableBatchProcessing并扩展default Configuration来使用Spring batch 5中的两个数据源?
最佳实践与以前版本的spring batch有很大不同吗?

5tmbdcev

5tmbdcev1#

使用@Autowired注解将两个数据源注入配置类。
然后定义一个由两个步骤组成的作业,每个步骤使用不同的数据源。使用step()方法为每个数据源创建一个步骤,并将适当的数据源作为参数传递。在每个步骤中,定义使用相应数据源的读取器、处理器和写入器。例如:从数据源1读取并向数据源2写入。
您可以在application.properties文件或单独的配置类中单独配置数据源。

2hh7jdfx

2hh7jdfx2#

下面是示例实现。

应用.属性

spring.datasource.ds1.url=jdbc:oracle:thin:@{host}:{port}:{database}
spring.datasource.ds1.username={username}
spring.datasource.ds1.password={password}
spring.datasource.ds1.hikari.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.ds1.hikari.minimum-idle=5
spring.datasource.ds1.hikari.maximum-pool-size=20
spring.datasource.ds1.hikari.connection-timeout=120000
spring.datasource.ds1.hikari.idle-timeout=120000
spring.datasource.ds1.hikari.max-lifetime=300000
spring.datasource.ds1.hikari.pool-name=HikariPool-1
#
spring.datasource.ds2.url=jdbc:oracle:thin:@{host}:{port}:{database}
spring.datasource.ds2.username={username}
spring.datasource.ds2.password={password}
spring.datasource.ds2.hikari.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.ds2.hikari.minimum-idle=5
spring.datasource.ds2.hikari.maximum-pool-size=20
spring.datasource.ds2.hikari.connection-timeout=120000
spring.datasource.ds2.hikari.idle-timeout=120000
spring.datasource.ds2.hikari.max-lifetime=300000
spring.datasource.ds2.hikari.pool-name=HikariPool-2

configuration.java

@EnableBatchProcessing
@EnableTransactionManagement
@EnableConfigurationProperties
@Configuration(proxyBeanMethods = false)
public class AppConfiguration {

    // == Datasource Configuration ==
    @Bean(name = "ds1DataSourceProperties")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.ds1")
    public DataSourceProperties ds1DataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "ds1DataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.ds1.hikari")
    public HikariDataSource ds1DataSource(@Qualifier("ds1DataSourceProperties") DataSourceProperties ds1DataSourceProperties) {
        return ds1DataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    @Bean(name = "ds1JdbcTemplate")
    public JdbcTemplate ds1JdbcTemplate(@Qualifier("ds1DataSource") HikariDataSource ds1DataSource) {
        return new JdbcTemplate(ds1DataSource);
    }

    @Bean(name = "ds2DataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.w")
    public DataSourceProperties ds2DataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "ds2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.w.hikari")
    public HikariDataSource ds2DataSource(@Qualifier("ds2DataSourceProperties") DataSourceProperties ds2DataSourceProperties) {
        return ds2DataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    @Bean(name = "ds2JdbcTemplate")
    public JdbcTemplate ds2JdbcTemplate(@Qualifier("ds2DataSource") HikariDataSource ds2DataSource) {
        return new JdbcTemplate(ds2DataSource);
    }

    @Bean(name = "ccbsWriteTransactionManager")
    public PlatformTransactionManager ccbsWriteTransactionManager(@Qualifier("ds2DataSource") HikariDataSource ds2DataSource) {
        return new DataSourceTransactionManager(ds2DataSource);
    }

你可以在你的读取器、处理器和写入器中定义相关的数据源。

相关问题