如果我没记错的话,可以连接到 Cassandra 集群,至少知道集群中的一个节点,然后可以发现其他节点。
假设我有三个节点(1、2 和 3),并且我像这样连接到这些节点:
Cluster.builder().addContactPoints("1,2,3".split(",")).build();
然后,如果节点 3 发生故障,并且无法解析 IP,则这行代码将抛出IllegalArgumentException
如文档中所述:
@throws IllegalArgumentException if no IP address for at least one of {@code addresses} could be found
为什么有人想要这种行为?我的意思是,如果其中一个节点发生故障,我希望应用程序能够运行,因为 Cassandra 仍然工作正常。
我已经检查过这个Cassandra Java驱动程序:多少个接触点是合理的? https://stackoverflow.com/questions/26852413/cassandra-java-driver-how-many-contact-points-is-reasonable但这并不能回答我的问题,因为它没有说明任何关于无法访问的主机的信息。
我应该如何处理这个问题?也许这在另一个版本的java驱动程序中被改变了?我目前正在使用cassandra-driver-core-3.0.3
此验证只是为了确保可以解析所有提供的主机,它甚至不检查 Cassandra 服务器是否在每个主机上运行。因此,基本上是为了确保您在提供主机时没有犯任何拼写错误,因为实际上它并不认为提供的主机无法解析可能是正常用例。
作为您的情况的解决方法(主机已从 DNS 条目中删除),您可以简单地调用该方法addContactPoint(String address) http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Cluster.Builder.html#addContactPoint-java.lang.String-明确地而不是使用addContactPoints(String... addresses)
(在幕后简单地调用addContactPoint(String address)
对于每个提供的地址)并自行管理异常。
代码可能是这样的:
Cluster.Builder builder = Cluster.builder();
// Boolean used to check if at least one host could be resolved
boolean found = false;
for (String address : "1,2,3".split(",")) {
try {
builder.addContactPoint(address);
// One host could be resolved
found = true;
} catch (IllegalArgumentException e) {
// This host could not be resolved so we log a message and keep going
Log.log(
Level.WARNING,
String.format("The host '%s' is unknown so it will be ignored", address)
);
}
}
if (!found) {
// No host could be resolved so we throw an exception
throw new IllegalStateException("All provided hosts are unknown");
}
Cluster cluster = builder.build();
FYI:我刚刚创建了一个票证来提议改进 Java 驱动程序https://datastax-oss.atlassian.net/browse/JAVA-1334 https://datastax-oss.atlassian.net/browse/JAVA-1334.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)