NoHostAvailableException:尝试查询的所有主机均失败
NoHostAvailableException: All host(s) tried for query failed
当我连接单个节点 cassandra 时,它工作正常。当我连接一个三节点集群(10.20.12.20、10.20.12.21、10.20.12.22)时,它抛出了以下错误。为什么要尝试连接本地主机?
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/0:0:0:0:0:0:0:1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/0:0:0:0:0:0:0:1:9042] Cannot connect), localhost/127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9042] Cannot connect))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:232) ~[cassandra-driver-core-3.4.0.jar:na]
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79) ~[cassandra-driver-core-3.4.0.jar:na]
at com.datastax.driver.core.Cluster$Manager.negotiateProtocolVersionAndConnect(Cluster.java:1619) ~[cassandra-driver-core-3.4.0.jar:na]
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1537) ~[cassandra-driver-core-3.4.0.jar:na]
at com.datastax.driver.core.Cluster.init(Cluster.java:159) ~[cassandra-driver-core-3.4.0.jar:na]
通过default(如果出现任何错误或丢失属性)它将尝试连接到本地主机,因为它被设置为DEFAULT_CONTACT_POINTS
您的语法是正确的,我的猜测是 属性 根本没有被您的应用程序读取。你可以尝试调试验证一下。
至于调试:触点已设置here, if you check it will always set to default=localhost。
在您的 CassandraConfig 中,除了重写 getKeyspaceName()
方法之外,您还需要重写 cluster()
和 cassandraMapping()
方法以使其与您想要的设置一起工作。例如:
@Configuration
public class CassandraConfig extends AbstractCassandraConfiguration {
public static final String KEYSPACE = "my_keyspace";
// read contact points from config
@Value("${spring.data.cassandra.contact-points}")
private String contactPoints;
@Override
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean bean = super.cluster();
bean.setContactPoints(contactPoints);
return bean;
}
另请注意:如果您只添加一个接触点,您将获得预期的行为,因为驱动程序将查找所有可用节点。实际上,如果您想限制与特定节点的通信,则需要实施 loadBalancingPolicy
进行排除。
当我连接单个节点 cassandra 时,它工作正常。当我连接一个三节点集群(10.20.12.20、10.20.12.21、10.20.12.22)时,它抛出了以下错误。为什么要尝试连接本地主机?
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/0:0:0:0:0:0:0:1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/0:0:0:0:0:0:0:1:9042] Cannot connect), localhost/127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9042] Cannot connect))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:232) ~[cassandra-driver-core-3.4.0.jar:na]
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79) ~[cassandra-driver-core-3.4.0.jar:na]
at com.datastax.driver.core.Cluster$Manager.negotiateProtocolVersionAndConnect(Cluster.java:1619) ~[cassandra-driver-core-3.4.0.jar:na]
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1537) ~[cassandra-driver-core-3.4.0.jar:na]
at com.datastax.driver.core.Cluster.init(Cluster.java:159) ~[cassandra-driver-core-3.4.0.jar:na]
通过default(如果出现任何错误或丢失属性)它将尝试连接到本地主机,因为它被设置为DEFAULT_CONTACT_POINTS
您的语法是正确的,我的猜测是 属性 根本没有被您的应用程序读取。你可以尝试调试验证一下。
至于调试:触点已设置here, if you check it will always set to default=localhost。
在您的 CassandraConfig 中,除了重写 getKeyspaceName()
方法之外,您还需要重写 cluster()
和 cassandraMapping()
方法以使其与您想要的设置一起工作。例如:
@Configuration
public class CassandraConfig extends AbstractCassandraConfiguration {
public static final String KEYSPACE = "my_keyspace";
// read contact points from config
@Value("${spring.data.cassandra.contact-points}")
private String contactPoints;
@Override
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean bean = super.cluster();
bean.setContactPoints(contactPoints);
return bean;
}
另请注意:如果您只添加一个接触点,您将获得预期的行为,因为驱动程序将查找所有可用节点。实际上,如果您想限制与特定节点的通信,则需要实施 loadBalancingPolicy
进行排除。