Spring Boot 2 Cassandra多个键空间或集群

时间:2018-08-31 22:29:14

标签: spring-boot spring-data-cassandra

我正在使用Spring Boot版本2.0.3.RELEASEspring-data-cassandra-2.0.8。 我必须连接到2个不同的Cassandra群集。 Spring Boot的CassandraAutoConfiguration似乎仅支持1个Cassandra集群。

如何使其与@Repository自动创建的spring-data-cassandra实现一起使用?

1 个答案:

答案 0 :(得分:0)

您可以具有多个Cassandra集群配置。

/**
 * Configuration class for keyspace: big_data (Cluster 1)
 *
 */
@Configuration
@EnableCassandraRepositories(basePackages = "com.bigdata.cassandra.repository.data",
    cassandraTemplateRef = "keyspaceBigDataServiceTemplate")
public class CassandraBigDataServiceConfiguration {

  @Autowired
  private Properties cassandraProperties;

  @Bean
  public CassandraClusterFactoryBean cluster() {

    CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
    cluster.setContactPoints(cassandraProperties.getCassandraBigDataContactPoint());
    cluster.setPort(cassandraProperties.getCassandraBigDataPort());
    cluster.setSslEnabled(cassandraProperties.isCassandraBigDataSslEnabled());
    return cluster;
  }

  @Bean
  public CassandraMappingContext mappingContext() {
    return new CassandraMappingContext();
  }

  @Bean
  public CassandraConverter converter() {
    return new MappingCassandraConverter(mappingContext());
  }

  @Bean("keyspaceBigDataServiceSession")
  public CassandraSessionFactoryBean session() {

    CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
    session.setCluster(cluster().getObject());
    session.setKeyspaceName(cassandraProperties.getCassandraBigDataKeyspaceReporting());
    session.setConverter(converter());
    session.setSchemaAction(SchemaAction.NONE);

    return session;
  }

  @Bean("keyspaceBigDataServiceTemplate")
  public CassandraOperations cassandraTemplate() {
    return new CassandraTemplate(session().getObject());
  }

}

另一个具有不同键空间的集群:报告

/**
 * Configuration class for keyspace: reporting (Cluster 2)
 *
 */
@Configuration
@EnableCassandraRepositories(basePackages = "com.bigdata.cassandra.repository.reports",
    cassandraTemplateRef = "keyspaceReportingServiceTemplate")
public class CassandraReportingServiceConfiguration {

  @Autowired
  private Properties cassandraProperties;

  @Bean
  public CassandraClusterFactoryBean cluster() {

    CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
    cluster.setContactPoints(cassandraProperties.getCassandraReportingContactPoint());
    cluster.setPort(cassandraProperties.getCassandraReportingPort());
    cluster.setSslEnabled(cassandraProperties.isCassandraReportingSslEnabled());
    return cluster;
  }

  @Bean
  public CassandraMappingContext mappingContext() {
    return new CassandraMappingContext();
  }

  @Bean
  public CassandraConverter converter() {
    return new MappingCassandraConverter(mappingContext());
  }

  @Bean("keyspaceReportingServiceSession")
  public CassandraSessionFactoryBean session() {

    CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
    session.setCluster(cluster().getObject());
    session.setKeyspaceName(cassandraProperties.getCassandraReportingKeyspaceReporting());
    session.setConverter(converter());
    session.setSchemaAction(SchemaAction.NONE);

    return session;
  }

  @Bean("keyspaceReportingServiceTemplate")
  public CassandraOperations cassandraTemplate() {
    return new CassandraTemplate(session().getObject());
  }

}

您必须为要连接的每个集群指定属性:

# big_data keyspaces are in Cluster 1
cassandra-cluster1.contact-points=1.set.cluster1.ip
cassandra-cluster1.port=9042
cassandra-cluster1.keyspace-reporting.name=big_data
cassandra-cluster1.ssl.enabled=true

# reporting keyspace are in Cluster 2
cassandra-cluster2.contact-points=2.set.cluster2.ip
cassandra-cluster2.port=9042
cassandra-cluster2.keyspace-2.name=keyspace_2 // can have multiple keyspaces of course 
cassandra-cluster2.keyspace-reporting.name=reporting
cassandra-cluster2.ssl.enabled=true