cassandra ec2所有主机尝试查询失败

70gysomp  于 2021-06-13  发布在  Cassandra
关注(0)|答案(1)|浏览(357)

我的cassandra示例在aws ec2上运行。我在本地系统上创建了一个springboot应用程序,尝试连接到这个db并查询数据。每次我遇到错误时:
com.datastax.driver.core.exceptions.nohostavailableexception:所有尝试查询的主机都失败(尝试次数:/aaa.zz.yy.xx:9042)
在我的spring boot应用程序中,以下是依赖项:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-cassandra</artifactId>
        </dependency>
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-core</artifactId>
        </dependency>

我正在创建这样的连接:

@Bean 
    public Session session() {
        Cluster cluster = Cluster.builder().withPort(9042).addContactPoint("aaa.zz.yy.xx").withoutJMXReporting().build();
        cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(60000);
        return cluster.connect(keyspace_name);
      }

在我的cassandra.yaml文件中:

rpc_address: aaa.zz.yy.xx
rpc_port: 9160

我甚至在ec2中添加了一个入站规则,在这里我提供了本地系统ip(spring-boot应用程序正在运行)。
我做错什么了?
编辑:tldr:我做错的是我在代码中使用了私有ip。当我切换到公共ip时,它起了作用。

ecfsfe2w

ecfsfe2w1#

rpc\u端口是旧端口。我猜你在用cql。
尝试 .withPort(9042) 这是本地\u传输\u端口(默认值:9042)cql本机传输侦听客户端的端口。
当试图从本地m/c连接时,也要使用公共联系地址而不是私人联系地址。
我在这里添加编辑:
在方法中使用ec2的公共ip地址:addcontactpoint(ec2示例的公共ip是我们在putty中用于连接ec2的,例如:ec2-203-0-113-25.compute-1.amazonaws.com)
在代码中使用端口9042:withport
在ec2的yaml文件中,rpc:address should 是专用ip地址
如果使用spring boot,在pom.xml中,cassandra连接只有1个依赖关系:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-cassandra</artifactId>
        </dependency>

相关问题