简体   繁体   English

我们如何从同一个 Spring Boot 应用程序创建两个独立的 cassandra 集群?

[英]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.

相关问题 如何将 Spring 启动应用程序的数据输入 Amazon Kinesis? - How to enter data from Spring Boot Application into Amazon Kinesis? 我们可以使用 java 和 spring 通过 API 引导从 AWS EFS 上传和下载文件吗? - Can we upload and download files from AWS EFS using java and spring boot through an API? 我们可以在服务器端的 Spring Boot 中生成 fcm 令牌吗? - Can we generate fcm token in Spring Boot on server side? 将 spring 引导应用程序部署到 gcp 应用程序引擎时,如何在云 sql 中自动创建数据库表 - How to Auto create db table in cloud sql when deploy spring boot application to gcp app engine 如何使用 spring 引导 java 应用程序从服务总线队列中持续拉取消息 - How to continuously pull messages from service bus queue by using spring boot java application 我可以在 Ionic 应用程序中同时使用 firebase + spring 启动吗 - Can i use firebase + spring boot at same time in Ionic app 可以使用 gcp 凭据登录 spring 启动应用程序 - Can use gcp credentials for logging in spring boot application 我可以使用 AWS Elastic Beanstalk 托管 Spring Boot 应用程序吗 - Can I use AWS Elastic Beanstalk to host a Spring Boot application 我们如何在 Cloudformation 模板中将相同的标签用于两个 AWS::DynamoDB::Table - How we can use same Tags into two AWS::DynamoDB::Table with in Cloudformation Template 我怎样才能分开这两个值 - how can i separate the two values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM