Вы можете иметь несколько конфигураций кластера 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