gpt4 book ai didi

java - cassandra单节点连接错误

转载 作者:行者123 更新时间:2023-12-01 12:01:10 25 4
gpt4 key购买 nike

我正在尝试使用 cassandra 作为我正在开发的应用程序的数据库。该应用程序是 Netbeans 平台应用程序。为了在我的本地主机上启动 cassandra 服务器,我发出 Runtime.getRuntime().exec(command)其中 command 是启动 cassandra 服务器的字符串,然后我使用 datastax 驱动程序连接到 cassandra 服务器。但是我收到错误:

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query   failed (tried: /127.0.0.1:9042 (com.datastax.driver.core.TransportException: [/127.0.0.1:9042]  Cannot connect))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:199)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:80)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1154)
at com.datastax.driver.core.Cluster.getMetadata(Cluster.java:318)
at org.dhviz.boot.DatabaseClient.connect(DatabaseClient.java:43)
at org.dhviz.boot.Installer.restored(Installer.java:67)
....

我发现服务器需要一些时间才能启动,因此我添加了 Thread.sleep(MAX_DELAY_SERVER) 行,这似乎解决了问题。

有没有更优雅的方法来解决这个问题?谢谢。

代码如下。

public class Installer extends ModuleInstall {

private final int MAX_DELAY_SERVER = 12000;

//private static final String pathSrc = "/org/dhviz/resources";
@Override
public void restored() {

/*
-*-*-*-*-*DESCRIPTION*-*-*-*-*-*
IMPLEMENT THE CASSANDRA DATABASE
*********************************
*/
DatabaseClient d = new DatabaseClient();
// launch an instance of the cassandra server
d.loadDatabaseServer();


/*wait for MAX_DELAY_SERVER milliseconds before launching the other instructions.
*/
try {
Thread.sleep(MAX_DELAY_SERVER);
Logger.getLogger(Installer.class.getName()).log(Level.INFO, "wait for MAX_DELAY_SERVER milliseconds before the connect database");
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
Logger.getLogger(Installer.class.getName()).log(Level.INFO, "exeption in thread sleep");
}

d.connect("127.0.0.1");

}
}



public class DatabaseClient {

private Cluster cluster;
private Session session;
private ShellCommand shellCommand;
private final String defaultKeyspace = "dhviz";

final private String LOAD_CASSANDRA = "launchctl load /usr/local/Cellar/cassandra/2.1.2/homebrew.mxcl.cassandra.plist";

final private String UNLOAD_CASSANDRA = "launchctl unload /usr/local/Cellar/cassandra/2.1.2/homebrew.mxcl.cassandra.plist";

public DatabaseClient() {
shellCommand = new ShellCommand();

}

public void connect(String node) {
//this connect to the cassandra database

cluster = Cluster.builder()
.addContactPoint(node).build();
// cluster.getConfiguration().getSocketOptions().setConnectTimeoutMillis(12000);
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n",
metadata.getClusterName());
for (Host host
: metadata.getAllHosts()) {
System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack());

}

session = cluster.connect();


Logger.getLogger(DatabaseClient.class.getName()).log(Level.INFO, "connected to server");
}

public void loadDatabaseServer() {
if (shellCommand == null) {

shellCommand = new ShellCommand();

}
shellCommand.executeCommand(LOAD_CASSANDRA);
Logger.getLogger(DatabaseClient.class.getName()).log(Level.INFO, "database cassandra loaded");
}

public void unloadDatabaseServer() {
if (shellCommand == null) {

shellCommand = new ShellCommand();

}

shellCommand.executeCommand(UNLOAD_CASSANDRA);

Logger.getLogger(DatabaseClient.class.getName()).log(Level.INFO, "database cassandra unloaded");
}

}

最佳答案

如果您在 Runtime.getRuntime().exec(command) 中不带任何参数调用 cassandra,则很可能会生成 cassandra 作为后台进程,并在 cassandra 节点完全启动之前返回,并且正在听。

我不确定您为什么尝试将 cassandra 嵌入您的应用程序中,但您可能会发现使用 cassandra-unit对于提供将 cassandra 嵌入到您的应用程序中的机制很有用。它主要用于运行需要 cassandra 实例的测试,但它也可能满足您的用例。

The wiki提供了有关如何使用 cassandra-unit 启动嵌入式 cassandra 实例的有用示例:

EmbeddedCassandraServerHelper.startEmbeddedCassandra();

根据我的经验,cassandra-unit 将等到服务器启动并监听后再返回。您还可以编写一个等待套接字使用的方法,使用与 this answer 相反的逻辑。 .

关于java - cassandra单节点连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973103/

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