错误是
Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /172.17.1.XX:9160 (com.datastax.driver.core.TransportException: [/172.17.1.XX:9160] Cannot connect))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:196)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:80)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1145)
at com.datastax.driver.core.Cluster.getMetadata(Cluster.java:313)
at com.example.cassandra.Client.connect(Client.java:18)
at com.example.cassandra.Client.main(Client.java:74)
数据存储的版本为dse 4.5.2
发生错误的行是connect(node)
cluster = Cluster.builder().addContactPoint(node).withPort(9160).build();
Metadata metadata = cluster.getMetadata(); //Error at this line
调用该函数是
public static void main(String a[])
{
System.out.println("I am in");
Client client = new Client();
client.connect("172.17.1.XX");
client.close();
}
pom.xml是
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.cassandra</groupId>
<artifactId>simple-client</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<dependencies>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache-extras.cassandra-jdbc</groupId>
<artifactId>cassandra-jdbc</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<mainClass>com.example.cassandra.Client</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
我提到了this link
然后我试了
telnet 172.17.1.XX 9160
Trying 172.17.1.XX...
Connected to 172.17.1.XX.
Escape character is '^]'.
如果我输入内容,则会显示connection closed by foreign agent
netstat -plten
on 172.17.1.XX
shows
tcp 0 0 127.0.0.1:8984 0.0.0.0:* LISTEN 0 73148094 19145/java
tcp 0 0 127.0.0.1:7000 0.0.0.0:* LISTEN 0 73148635 19145/java
tcp 0 0 0.0.0.0:7199 0.0.0.0:* LISTEN 0 73148631 19145/java
tcp 0 0 0.0.0.0:50370 0.0.0.0:* LISTEN 0 73148632 19145/java
tcp 0 0 0.0.0.0:9160 0.0.0.0:* LISTEN 0 73148104 19145/java
tcp 0 0 0.0.0.0:33935 0.0.0.0:* LISTEN 0 73148630 19145/java
tcp 0 0 0.0.0.0:9042 0.0.0.0:* LISTEN 0 73148103 19145/java
tcp 0 0 0.0.0.0:8983 0.0.0.0:* LISTEN 0 73148083 19145/java
那么我该如何解决这个问题呢?任何解决方案?
答案 0 :(得分:2)
您正在连接到错误的端口:Java Driver连接到Native Port(通常是9042)而不是Thrift Port(通常是9160)。改变这一行
cluster = Cluster.builder().addContactPoint(node).withPort(9042).build();