java中的Zookeeper连接
下面的代码(不是我的)应该使用znode_exists()方法检查zookeeper的连接状态。我想在发布消息之前在producerAPI使用它。但是我在定义ZooKeeperConnection对象时出错了。我已经添加了可能的类和库java中的Zookeeper连接,java,apache-zookeeper,Java,Apache Zookeeper,下面的代码(不是我的)应该使用znode_exists()方法检查zookeeper的连接状态。我想在发布消息之前在producerAPI使用它。但是我在定义ZooKeeperConnection对象时出错了。我已经添加了可能的类和库 import java.io.IOException; //import org.apache.zookeeper.*; adding this doesn't help import org.apache.zookeeper.ZooKeeperMain; imp
import java.io.IOException;
//import org.apache.zookeeper.*; adding this doesn't help
import org.apache.zookeeper.ZooKeeperMain;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.data.Stat;
public class ZKExists {
private static ZooKeeper zk;
private static ZooKeeperConnection conn; // Object cannot be resolve to a type
// Method to check existence of znode and its status, if znode is available.
public static Stat znode_exists(String path) throws
KeeperException,InterruptedException {
return zk.exists(path, true);
}
public static void main(String[] args) throws InterruptedException,KeeperException {
String path = "/Znode_path"; // Assign znode to the specified path
try {
ZooKeeperConnection conn = new ZooKeeperConnection();
zk = conn.connect("localhost");
Stat stat = znode_exists(path); // Stat checks the path of the znode
if(stat != null) {
System.out.println("Node exists and the node version is " +
stat.getVersion());
} else {
System.out.println("Node does not exists");
}
} catch(Exception e) {
System.out.println(e.getMessage()); // Catches error messages
}
}
}
要使对象有效,我缺少哪个库?是否要将消息发布到Apache Kafka主题?假设您这样做:发布前不需要检查Zookeeper,只需发布到Kafka并处理任何错误即可。卡夫卡要求Zookeeper无论如何都要运行,否则它将无法工作。在生产中,你会监控应用程序。@JensHoffmann是的,我会。我只是觉得最好看看是否有经纪人。我知道经纪人支票比zk支票更明智。但我无法使用“zkUtils.getAllBrokerIncluster()”作为列表。是否要将消息发布到Apache Kafka主题?假设这样做:发布前不需要检查Zookeeper,只需发布到Kafka并处理任何错误即可。卡夫卡要求Zookeeper无论如何都要运行,否则它将无法工作。在生产中,你会监控应用程序。@JensHoffmann是的,我会。我只是觉得最好看看是否有经纪人。我知道经纪人支票比zk支票更明智。但是我不能使用“zkUtils.getAllBrokerIncluster()”作为列表。