zookeeperAPI连接集群
ZooKeeper(String connectionString, int sessionTimeout, Watcher watcher)
connectionString - zooKeeper集合主机。
sessionTimeout - 会话超时(以毫秒为单位)。
watcher - 实现“监视器”界面的对象。ZooKeeper集合通过监视器对象返回连接状态。
案例:
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.util.concurrent.CountDownLatch;
public class ZookeeperConnection {
public static void main(String[] args) {
try {
// 计数器对象
CountDownLatch countDownLatch = new CountDownLatch(1);
// arg1:服务器的ip和端口
// arg2:客户端与服务器之间的会话超时时间 以毫秒为单位的
// arg3:监视器对象
ZooKeeper zooKeeper = new
ZooKeeper("192.168.60.130:2181,192.168.60.130:2182,192.168.60.130:2183",
5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.SyncConnected) {
System.out.println("连接创建成功!");
countDownLatch.countDown();
}
}
});
// 主线程阻塞等待连接对象的创建成功
countDownLatch.await();
// 会话编号
System.out.println(zooKeeper.getSessionId());
zooKeeper.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
上次更新: 2025/07/23, 07:31:13