如何在JavaSpringBootWebFlux中与多个键空间进行交互?

8aqjt8rx  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(254)

我正在尝试连接到两个不同的React键空间,具体取决于传入的http请求。请求将包含一些信息,供业务逻辑/服务层从键空间a或键空间b检索数据(通过调用reactivearepository或reactivebrepository)以获取相应的数据。
简单的if语句不是解决方案,因为它需要使用正确的React式存储库以及相应的React式配置、React式模板等…
在存储库层,非常简单,甚至不使用任何自定义查询。我甚至将存储库放在各自的包中。

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

我已经试过了:

@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);
    }

但是,它只指向键空间a。请问在cassandra中使用两个键空间的React式应用程序的正确方法是什么?

sxpgvts3

sxpgvts31#

这可能不是您想要的答案,但是在SpringDataJPA中,您可以通过为每个数据源配置单独的entitymanagerfactory和platformtransactionmanager bean来实现连接到不同实体集的多个数据库。
示例
嗯!

相关问题