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



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

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

您可以有多个 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

相关内容

  • 没有找到相关文章

最新更新