gpt4 book ai didi

java - Cassandra NoHostAvailableException Java-CQLDriver

转载 作者:行者123 更新时间:2023-12-03 18:37:56 26 4
gpt4 key购买 nike

我正在使用 Cassandra 的 CQL 驱动程序在 Java 中构建应用程序。当我使用本地 cassandra 服务器时,我能够正常运行它。但是,当我尝试运行时,相同的代码不适用于远程 cassandra 服务器。它在初始化期间出现以下错误:-

以下是我用于 CQL 驱动程序的 Maven 依赖项:-

<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>2.1.3</version>
</dependency>

<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>2.1.2</version>
</dependency>

错误如下:-

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: yyyy.yyy.yyy.yyy.yyy-yyy.net/yy.yy.yy.yy:9160 (com.datastax.driver.core.ConnectionException: [yyyy.yyy.yyy.yyy.yyy-yyy.net/yy.yy.yy.yy:9160] Unexpected error during transport initialization (com.datastax.driver.core.ConnectionException: [yyyy.yyy.yyy.yyy.yyy-yyy.net/yy.yy.yy.yy:9160] Operation timed out)), 
Unexpected error during transport initialization (com.datastax.driver.core.ConnectionException: [xxxx.xxx.xxx.xxx.xxx-xxx.net/xx.xx.xx.xx:9160] Operation timed out)))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:220)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:78)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1231)
at com.datastax.driver.core.Cluster.init(Cluster.java:158)
at com.datastax.driver.core.Cluster.connect(Cluster.java:246)

一切正常的本地 cassandra 服务器是:-

cqlsh 4.1.1, Cassandra 2.0.7 , CQL spec: 3.1.1, Thrift Protocol 19.39.0

连接超时的远程 cassandra 服务器是:-

cqlsh 3.1.8 | Cassandra 1.2.12 | CQL spec 3.0.0 | Thrift protocol 19.36.1

我正在使用以下代码进行连接。有人可以评论远程连接案例中出了什么问题吗?

clusterOBJECT = Cluster.builder().addContactPointsWithPorts(addrs)
.withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE))
.withProtocolVersion(1).withoutJMXReporting()
.withPort(port)
.withSocketOptions(options)
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
.withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy())).build();
session = clusterOBJECT.connect();

我在 addContactPointsWithPorts 中提供了有效的主机名和端口 (9160)。主机名也是可 ping 通的。

PS:- 以下属性已在 cassandra.yaml 中设置。

start_native_transport: true
native_transport_port: 9042

[编辑]将端口更改为 9042 后,我能够连接到 Cassandra。但是我看到连接很快就被这个异常丢弃了。

com.datastax.driver.core.TransportException: [/xx.xx.xxx.xx:9042] Cannot connect
at com.datastax.driver.core.Connection.<init>(Connection.java:106)
at com.datastax.driver.core.PooledConnection.<init>(PooledConnection.java:35)
at com.datastax.driver.core.Connection$Factory.open(Connection.java:528)
at com.datastax.driver.core.DynamicConnectionPool.<init>(DynamicConnectionPool.java:74)
at com.datastax.driver.core.HostConnectionPool.newInstance(HostConnectionPool.java:33)
at com.datastax.driver.core.SessionManager.replacePool(SessionManager.java:270)
at com.datastax.driver.core.SessionManager.access$400(SessionManager.java:39)
at com.datastax.driver.core.SessionManager$3.call(SessionManager.java:303)
at com.datastax.driver.core.SessionManager$3.call(SessionManager.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: /xx.xx.xxx.xx:9042
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:739)
at org.jboss.netty.channel.socket.nio.NioClientBoss.connect(NioClientBoss.java:150)
at org.jboss.netty.channel.socket.nio.NioClientBoss.processSelectedKeys(NioClientBoss.java:105)
at org.jboss.netty.channel.socket.nio.NioClientBoss.process(NioClientBoss.java:79)
at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318)
at org.jboss.netty.channel.socket.nio.NioClientBoss.run(NioClientBoss.java:42)
... 3 more

谢谢。

最佳答案

“我在 addContactPointsWithPorts 中提供了有效的主机名和端口 (9160)。而且主机名是可 ping 通的。”

在阅读您原始帖子中的这一行并仔细检查一些文档后,我想我知道这里发生了什么。 About the Java Driver document明确指出:

The Java Driver 2.0 for Apache Cassandra works exclusively with the Cassandra Query Language version 3 (CQL3) and Cassandra's new binary protocol

connection requirements部分还指出:

you have configured the following in the cassandra.yaml:

start_native_transport : true
rpc_address : IP address or hostname

reachable from the client machines in the cluster can accept connections on port 9042. Note: The client port can be configured using the native_transport_port in cassandra.yaml.

换句话说,您不应该使用 Thrift (9160) 通过 Java-CQL 驱动程序连接到 Cassandra。尝试不设置端口(在 withPort 或主机名中)。它应该尝试自己使用 9042。

编辑:好的,我只是使用 .addContactPoints 自己尝试过(没有用主机名指定的端口)和 .withPort(9160) ,它奏效了。这似乎与我之前在帖子中引用的文档相矛盾。无论如何,我仍然建议使用 native 协议(protocol) (9042),因为 Thrift 协议(protocol)已被弃用。此外,较新版本的 Cassandra (2.1) 和 DataStax CQL 驱动程序 have been shown to out-perform the Thrift-based drivers .

关于java - Cassandra NoHostAvailableException Java-CQLDriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28538005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com