简体   繁体   中英

How to interact with multiple keyspaces with reactive Cassandra repositories in java Springboot WebFlux?

I am trying to connect to two different reactive keyspaces depending on the incoming http request. The request will contains some information which is for the business logic/ service layer to retrieve data from either a keyspace A or a keyspace B (by invoking ReactiveARepository or ReactiveBRepository) in order to get the corresponding data.

A simple if statement is not the solution, since it requires to use the correct reactive repository with the corresponding reactive configuration, reactive template, etc…

On the repository layer, very straightforward, not even using any custom queries. I even put the repositories to their own respective packages.

package com.cassandra.repository.a;

@Repository
public interface ReactiveARepository extends ReactiveCassandraRepository<MyPojo, String> {

package com.cassandra.repository.b;

@Repository
public interface ReactiveBRepository extends ReactiveCassandraRepository<MyPojo, String> {
@Configuration
@EnableReactiveCassandraRepositories
public class AandBcommonCassandraConfiguration extends AbstractReactiveCassandraConfiguration {

//the @Value private String for contactPoints, keyspace, datacenter, port, username and password 

    @Bean
    @NonNull
    @Override
    public CqlSessionFactoryBean cassandraSession() {
        final CqlSessionFactoryBean cqlSessionFactoryBean = new CqlSessionFactoryBean();
        cqlSessionFactoryBean.setContactPoints(contactPoints);
        cqlSessionFactoryBean.setKeyspaceName(keyspace);
        cqlSessionFactoryBean.setLocalDatacenter(datacenter);
        cqlSessionFactoryBean.setPort(port);
        cqlSessionFactoryBean.setUsername(username);
        cqlSessionFactoryBean.setPassword(passPhrase);
        return cqlSessionFactoryBean;
    }

    @NonNull
    @Override
    protected String getKeyspaceName() {
        return keyspace;
    }

    @Override
    protected String getLocalDataCenter() {
        return datacenter;
    }

//other getters

What I already tried:

@Configuration
@EnableReactiveCassandraRepositories(basePackages = "com.cassandra.repository.a”, reactiveCassandraTemplateRef = “keyspaceCassandraTemplateA”)
public class AkeyspaceCassandraConfiguration extends BaseCassandraConfiguration {

    @Value("${spring.data.cassandra.keyspace-name.keyspaceA}”)
    private String keyspace;

    @NonNull
    @Override
    public String getKeyspaceName() {
        return keyspace;
    }

    @NonNull
    @Override
    public CqlSessionFactoryBean cassandraSession() {
        final CqlSessionFactoryBean cqlSessionFactoryBean = super.cassandraSession();
        cqlSessionFactoryBean.setKeyspaceName(keyspace);
        return cqlSessionFactoryBean;
    }

    @Bean(“keyspaceCassandraTemplateA”)
    public ReactiveCassandraTemplate reactiveCassandraTemplate() {
        final ReactiveSession reactiveSession = new DefaultBridgedReactiveSession(cassandraSession().getObject());
        return new ReactiveCassandraTemplate(reactiveSession);
    }
@Configuration
@EnableReactiveCassandraRepositories(basePackages = "com.cassandra.repository.B”, reactiveCassandraTemplateRef = “keyspaceCassandraTemplateB”)
public class BKeyspaceCassandraConfiguration extends BaseCassandraConfiguration {

    @Value("${spring.data.cassandra.keyspace-name.keyspaceB}”)
    private String keyspace;

    @NonNull
    @Override
    public String getKeyspaceName() {
        return keyspace;
    }

    @NonNull
    @Override
    public CqlSessionFactoryBean cassandraSession() {
        final CqlSessionFactoryBean cqlSessionFactoryBean = super.cassandraSession();
        cqlSessionFactoryBean.setKeyspaceName(keyspace);
        return cqlSessionFactoryBean;
    }

    @Bean("keyspaceCassandraTemplateB")
    public ReactiveCassandraTemplate reactiveCassandraTemplate() {
        final ReactiveSession reactiveSession = new DefaultBridgedReactiveSession(cassandraSession().getObject());
        return new ReactiveCassandraTemplate(reactiveSession);
    }

However, it is only pointing the the keyspace A. May I ask what is the proper way to have a reactive application with two keyspaces in Cassandra?

This may not be the answer you are looking for, but in Spring Data JPA, you can achieve connecting to multiple databases for different sets of entities by configuring separate EntityManagerFactory and PlatformTransactionManager beans for each data source.

Examples

HTH!

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