Configuring a Spring Cloud Data Flow Task with its own Database

折月煮酒 提交于 2019-12-11 15:37:23

问题


I have a task application with its own database that I'd like to run in Spring Cloud Data Flow.

My problem is that SCDF overwrites the datasource configuration in the task with the datasource configuration for SCDF. (Both databases are Oracle DBs.)

My task should write to a different database (but I also want to know its status in SCDF database).

How is it possible to configure my task to connect to its own database as well as to SCDF's database?


回答1:


I found the solution.

I defined both data sources in a configuration class (one for JPA and one for SCDF) following this as an example: https://www.baeldung.com/spring-data-jpa-multiple-databases

However this wasn't enough because the Data Flow Server accepts only one data source by default. To overcome this, one needs to extend the DefaultTaskConfigurer and set the Data Flow Server's data source in the constructor.

@Component
public class GeneratorTaskConfigurer extends DefaultTaskConfigurer {

    public GeneratorTaskConfigurer(@Qualifier("dataflowDataSource") DataSource dataSource) {
        super(dataSource);
    }
}



回答2:


You can have one config class with SCDF datasource code like this

@Configuration
@Profile("cloud")
public class MySqlConfiguration {

@Bean
public Cloud cloud() {
    return new CloudFactory().getCloud();
}

@Bean
@Primary
public DataSource dataSource() {
    return cloud().getSingletonServiceConnector(DataSource.class, null);
}

@Bean
@Primary
public PlatformTransactionManager getTransactionManager() {
    return new DataSourceTransactionManager(dataSource());
}

@Bean
public JobRepository jobRepositoryFactoryBean() throws Exception{
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(dataSource());
    factory.setTransactionManager(getTransactionManager());
    factory.afterPropertiesSet();
    return factory.getObject();
}

@Bean
@Primary
public DefaultTaskConfigurer defaultTaskConfigurer() {
    return new DefaultTaskConfigurer(dataSource());
}
}

And then have your other datasource configuration in a separate class for the database you want to write to. Make sure you mark the SCDF one @Primary, otherwise you get multiple datasource error.

Hope this helps.



来源:https://stackoverflow.com/questions/55047310/configuring-a-spring-cloud-data-flow-task-with-its-own-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!