在Spring数据中连接到2个Cassandra集群

时间:2019-06-17 16:49:20

标签: cassandra spring-data spring-data-cassandra

我正在尝试使用spring data cassandra连接到2个不同的cassandra集群。但它始终仅使用第一个cassandra集群配置。第二个没有影响。知道我在做什么错吗?这是我正在使用的配置:

第一个cassandra群集配置:

@Configuration
@EnableCassandraRepositories(
        basePackageClasses = SourceRepository.class
)
public class SourceCassandraConfig extends AbstractCassandraConfiguration {

    @Override
    public String getContactPoints() {
        return "localhost";
    }

    @Override
    public int getPort() {
        return "9051";
    }

    @Override
    protected String getKeyspaceName() {
        return "source_keyspace";
    }
}

第二个cassandra集群配置:

@Configuration
@EnableCassandraRepositories(
        basePackageClasses = TargetRepository.class,
        cassandraTemplateRef = "targetCassandraTemplate"
)
public class TargetCassandraConfig extends AbstractCassandraConfiguration {

    @Override
    public String getContactPoints() {
        return "localhost";
    }

    @Override
    public int getPort() {
        return "9052";
    }

    @Override
    protected String getKeyspaceName() {
        return "target_keyspace";
    }

    @Override
    @Bean("targetSession")
    public CassandraSessionFactoryBean session() throws ClassNotFoundException {
        final CassandraSessionFactoryBean session = super.session();
        session.setKeyspaceName(getKeyspaceName());
        session.setCluster(cluster().getObject());
        return session;
    }

    @Override
    public CassandraCqlClusterFactoryBean cluster() {
        CassandraCqlClusterFactoryBean cluster = super.cluster();
        cluster.setContactPoints(contactPoints);
        cluster.setPort(port);
        return cluster;
    }

    @Bean("targetCassandraTemplate")
    public CassandraAdminOperations cassandraTemplate(
            @Qualifier("targetSession") final CassandraSessionFactoryBean session) throws Exception {
        return new CassandraAdminTemplate(session.getObject(), cassandraConverter());
    }
}

我总是看到只有第一个集群节点被添加

com.datastax.driver.core.Cluster         : New Cassandra host localhost/127.0.0.1:9051 added

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我花了2天的时间对此进行调试,并在发布此问题10分钟后,找到了解决方法:)

我没有使用在会话bean中正确创建的集群bean。因此,我执行了以下操作,并成功了:

    @Override
    @Bean("targetCassandraCluster")
    public CassandraCqlClusterFactoryBean cluster() {
        CassandraCqlClusterFactoryBean cluster = super.cluster();
        cluster.setContactPoints(contactPoints);
        cluster.setPort(port);
        return cluster;
    }

    @Bean("targetCassandraSession")
    public CassandraSessionFactoryBean session(
            @Qualifier("targetCassandraCluster") final CassandraCqlClusterFactoryBean cluster
    ) throws ClassNotFoundException {
        final CassandraSessionFactoryBean session = super.session();
        session.setKeyspaceName(getKeyspaceName());
        session.setCluster(cluster.getObject());
        return session;
    }

    @Bean("targetCassandraTemplate")
    public CassandraAdminOperations cassandraTemplate(
            @Qualifier("targetCassandraSession") final CassandraSessionFactoryBean session) throws Exception {
        return new CassandraAdminTemplate(session.getObject(), cassandraConverter());
    }