Spring Batch-创建两个数据源以及如何自定义使用其他属性 [英] Spring Batch - Create Two Datasources and how to customized to use other properties

查看:218
本文介绍了Spring Batch-创建两个数据源以及如何自定义使用其他属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要快速指导来在Spring Boot Batch项目中创建两个关系数据源.一个是Oracle作为源数据库,另一个是Postgres目标数据库. Spring Boot V2.2.5.RELEADE

I need quick guidance to create two relational datasources in Spring Boot Batch project. One is Oracle as a Source DB and Other is Postgres Target DB. Spring Boot V2.2.5.RELEADE

Spring Boot版本2.2.5.RELEASE

在这里我想自定义两个数据源以使用此处提到的所有属性( http://shekup.blogspot.com/2018/05/针对两个数据源的multi-data-sources-in-spring-batch.html#:〜:text = Multiple%20Data%20sources%20in%20Spring%20batch,例如%20as%20ETL%20batch%20job.)

Here I want to customized both datasources to use all properties mentioned here (http://shekup.blogspot.com/2018/05/multiple-data-sources-in-spring-batch.html#:~:text=Multiple%20Data%20sources%20in%20Spring%20batch,such%20as%20ETL%20batch%20job.) for both datasources

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres?currentSchema=XXXX?useSSL=false
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driver-class-name=org.postgresql.Driver

# max no. of connections in the pool
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=20
spring.datasource.hikari.connection-test-query=SELECT 1


spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.default_schema=YYYY


spring.batch.initialize-schema=always
spring.batch.table-prefix=YYYY.BATCH_

### Source oracle DS ###
oracle.datasource.url=jdbc:oracle:thin:@//XXXX:1527/XXX
oracle.datasource.username=XXX
oracle.datasource.password=XXX
oracle.datasource.driverClassName=oracle.jdbc.Oracloracleiver

# max no. of connections in the pool
oracle.spring.datasource.hikari.maximum-pool-size=30
oracle.spring.datasource.hikari.minimum-idle=20
oracle.spring.datasource.hikari.connection-test-query=SELECT 1

DBConfig

@Configuration
public class DataSourceConfig {
    @Autowired
    private Environment env;

    @Primary
    @Bean(name = "postgresDataSource")
    @ConfigurationProperties("spring.datasource")
    public DataSource batchDataSource() {
        return DataSourceBuilder.create().url(env.getProperty("spring.datasource.url"))
                .driverClassName(env.getProperty("spring.datasource.driver-class-name"))
                .username(env.getProperty("spring.datasource.username"))
                .password(env.getProperty("spring.datasource.password")).build();
    }
       
    @Bean(name = "oracleDataSource")
    @ConfigurationProperties("oracle.datasource")
    public DataSource mysqlBatchDataSource() {
        return DataSourceBuilder.create().url(env.getProperty("oracle.datasource.url"))
                .driverClassName(env.getProperty("oracle.datasource.driver-class-name"))
                .username(env.getProperty("oracle.datasource.username"))
                .password(env.getProperty("oracle.datasource.password")).build();
    }   
}

推荐答案

您必须通过自己的代码提供其他信息. 不要使用DataSourceBuilder.这是Tomcat-Jdbc和Hikari的示例

You have to provide the additional information by your own code. Do not use DataSourceBuilder. Here is examples of Tomcat-Jdbc and Hikari

@Bean(name = "sourceBatchDataSource")
public DataSource sourceBatchDataSource() {
    HikariDataSource hikariDataSource = new HikariDataSource();
    hikariDataSource.setJdbcUrl(sourceDataSourceProperties.getUrl());
    hikariDataSource.setUsername(sourceDataSourceProperties.getUsername());
    hikariDataSource.setPassword(sourceDataSourceProperties.getPassword());
    hikariDataSource.setDriverClassName(sourceDataSourceProperties.getDriverClassName());
    hikariDataSource.setAutoCommit(from(environment.getProperty("spring.datasource.hikari.auto-commit")));
    hikariDataSource.setConnectionTimeout(environment.getProperty("spring.datasource.hikari.connection-timeout", Integer.class));
    hikariDataSource.setMaximumPoolSize(environment.getProperty("spring.datasource.hikari.maximum-pool-size", Integer.class));
    hikariDataSource.setMaxLifetime(environment.getProperty("spring.datasource.hikari.max-lifetime", Integer.class));
    hikariDataSource.setMinimumIdle(environment.getProperty("spring.datasource.hikari.minimum-idle", Integer.class));
    hikariDataSource.setPoolName("SourceBatchHikariCP");
    return hikariDataSource;
}

@Primary
@Bean(destroyMethod = "close", name = "sourceDataSource")
public DataSource sourceDataSource() {
    DataSourceProperties dataSourceProperties = dataSourceProperties();
    PoolProperties properties = new PoolProperties();
    properties.setUrl(dataSourceProperties.getUrl());
    properties.setDriverClassName(dataSourceProperties.getDriverClassName());
    properties.setUsername(dataSourceProperties.getUsername());
    properties.setPassword(dataSourceProperties.getPassword());
    properties.setInitialSize(environment.getProperty("spring.datasource.tomcat.initial-size", Integer.class));
    properties.setMaxWait(environment.getProperty("spring.datasource.tomcat.max-wait", Integer.class));
    properties.setMaxActive(environment.getProperty("spring.datasource.tomcat.max-active", Integer.class));
    properties.setMaxIdle(environment.getProperty("spring.datasource.tomcat.max-idle", Integer.class));
    properties.setMinIdle(environment.getProperty("spring.datasource.tomcat.min-idle", Integer.class));
    properties.setDefaultAutoCommit(from(environment.getProperty("spring.datasource.tomcat.default-auto-commit")));
    properties.setValidationQuery(environment.getProperty("spring.datasource.tomcat.validation-query"));
    properties.setTestOnBorrow(from(environment.getProperty("spring.datasource.tomcat.test-on-borrow")));
    properties.setTestWhileIdle(from(environment.getProperty("spring.datasource.tomcat.test-while-idle")));
    properties.setTestOnReturn(from(environment.getProperty("spring.datasource.tomcat.test-on-return")));
    properties.setTimeBetweenEvictionRunsMillis(environment.getProperty("spring.datasource.tomcat.time-between-eviction-runs-millis", Integer.class));
    properties.setMinEvictableIdleTimeMillis(environment.getProperty("spring.datasource.tomcat.min-evictable-idle-time-millis", Integer.class));
    properties.setRemoveAbandoned(from(environment.getProperty("spring.datasource.tomcat.remove-abandoned")));
    properties.setRemoveAbandonedTimeout(environment.getProperty("spring.datasource.tomcat.remove-abandoned-timeout", Integer.class));
    properties.setLogAbandoned(from(environment.getProperty("spring.datasource.tomcat.log-abandoned")));
    properties.setLogValidationErrors(from(environment.getProperty("spring.datasource.tomcat.log-validation-errors")));
    properties.setJdbcInterceptors(environment.getProperty("spring.datasource.tomcat.jdbc-interceptors"));
    return new org.apache.tomcat.jdbc.pool.DataSource(properties);
}

这篇关于Spring Batch-创建两个数据源以及如何自定义使用其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆