简体   繁体   中英

Multiple R2DBC datasource with Spring boot

I want to migrate my app to WebFlux, but the tricky part that I have bean which connects to 6 data sources by such mechanism

public class MultiRoutingDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return //code which sets context for chosen db;
    }
}

Then I'm creating 6 data sources which is then managed by multiRoutingDataSource

@Bean(name = "multiRoutingDataSource")
    public DataSource multiRoutingDataSource() {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(ident, MyDataSourceBean());
        MultiRoutingDataSource multiRoutingDataSource = new MultiRoutingDataSource();
        multiRoutingDataSource.setTargetDataSources(targetDataSources);
        return multiRoutingDataSource;
    }

and this data sources could be changed in runtime. This multiRouting then set into entity manager.

Is there something similar with WebFlux?

I found

public class MultiRoutingDataSource extends AbstractRoutingConnectionFactory {

    @Override
    protected Mono<Object> determineCurrentLookupKey() {
        return null;
    }

But how to create beans with connections and switch them in runtime like I'm doing in Spring MVC?

If you want multi R2dbc connectionfactories at the same application, check my example multi-r2dbc-connectionfactories .

For multi-tenancy support, check multi-tenancy-r2dbc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM