[英]How can we create two separate cassandra clusters from the same Spring Boot application?
I'm having a hard time connecting to two different clusters from a Spring Boot application.我很难从 Spring 引导应用程序连接到两个不同的集群。 The task is to get the data from Cassandra on ec2 and insert in batches to an AWS keyspace.
任务是从 ec2 上的 Cassandra 获取数据并批量插入到 AWS keyspace。 I am able to get the data from Cassandra on ec2.
我能够从 ec2 上的 Cassandra 获取数据。
Here I am overriding the customize method of在这里,我重写了自定义方法
org.springframework.boot.autoconfigure.cassandra.ClusterBuilderCustomizer
org.springframework.boot.autoconfigure.cassandra.ClusterBuilderCustomizer
When trying to connect to the Cassandra cluster running on ec2, things are working fine, but the requirement is to also have one more cluster to select data from Cassandra ec2 and insert into the other Cassandra cluster.当尝试连接到在 ec2 上运行的 Cassandra 集群时,一切正常,但要求还有一个集群到 Cassandra ec2 的 select 数据并插入另一个 Cassandra 集群。
The problem is when I am overriding the customize method for creating a cluster in the AWS Keyspace, instead of a new cluster being created, the contact point gets added to a previously created cluster.问题是当我覆盖在 AWS Keyspace 中创建集群的自定义方法时,联系点被添加到先前创建的集群,而不是创建新集群。
After that, I tried creating two different configurations for instantiating two different clusters.之后,我尝试创建两个不同的配置来实例化两个不同的集群。 See the configuration below for cluster 1. Cluster 2 is the same;
参见下面集群 1 的配置。集群 2 相同; the only difference is its for 3rd party Cassandra on cloud.
唯一的区别是它适用于云上的第 3 方 Cassandra。
@Configuration
@EnableCassandraRepositories(cassandraTemplateRef = "cassandraKeyspaceServiceTemplate")
public class CassandraClusterOneServiceConfiguration {
@Autowired
private CassandraOneProperties cassandraProperties;
@Bean(name = "cluster1")
@Primary
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(cassandraProperties.getContactPoints());
cluster.setPort(cassandraProperties.getPort());
cluster.setSslEnabled(cassandraProperties.isSsl());
cluster.setUsername(cassandraProperties.getUsername());
cluster.setPassword(cassandraProperties.getPassword());
final SSLContext sslContext = SSLContext.getInstance("SSL");
cluster.setSslOptions(
RemoteEndpointAwareJdkSSLOptions.builder().withSSLContext(sslContext).build());
cluster.setJmxReportingEnabled(false);
return cluster;
}
@Bean(name = "clusterContext1")
@Primary
public CassandraMappingContext mappingContext() {
return new CassandraMappingContext();
}
@Bean(name = "clusterConverter1")
@Primary
public CassandraConverter converter() {
return new MappingCassandraConverter(mappingContext());
}
@Bean("cassandraKeyspaceServiceSession")
@Primary
public CassandraSessionFactoryBean session() {
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster().getObject());
session.setKeyspaceName(cassandraProperties.getKeyspaceName());
session.setConverter(converter());
session.setSchemaAction(SchemaAction.NONE);
return session;
}
@Bean("cassandraKeyspaceServiceTemplate")
@Primary
public CassandraOperations cassandraTemplate() {
return new CassandraTemplate(session().getObject());
}
}
With this code the first cluster is also setting up correctly, but the second one is giving me a NoHostAvailable
exception.使用此代码,第一个集群也设置正确,但第二个集群给我一个
NoHostAvailable
异常。 This seems to be a connection timeout.这似乎是连接超时。
How can I create two different clusters in a Spring Boot application?如何在 Spring Boot 应用程序中创建两个不同的集群?
You will want to create two classes as show in the example Managing Multiple Cassandra Sessions .您需要创建两个类,如管理多个 Cassandra 会话示例中所示。
Additionally, you can find some best practices with Amazon Keyspaces and spring boot here.此外,您可以在此处找到有关 Amazon Keyspaces 和 spring 引导的一些最佳实践。 https://github.com/aws-samples/amazon-keyspaces-examples/tree/main/java/datastax-v4/spring
https://github.com/aws-samples/amazon-keyspaces-examples/tree/main/java/datastax-v4/spring
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.