有 命令行终端链接 zookeeper ,webj界面链接,还有 java代码或者 python的方式。
(1)web界面链接:
这个方法请参考: zkWEB (zookeeper 可视化插件)
(2) 使用客户端命令连接Zookeeper
我们安装完 zookeeper后
直接在终端输入:
zkCli.sh # 连接本地
zkCli.sh -server elk101:2181 # 连接指定节点
zkCli.sh -server elk101:2181,elk102:2181,elk103:2181 # 连接集群
quit 退出终端
链接成功后
2023-06-03 10:50:54,454 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1179] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2023-06-03 10:50:54,471 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1011] - Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:55912, server: localhost/0:0:0:0:0:0:0:1:2181
2023-06-03 10:50:54,488 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1452] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, session id = 0x1000506ed6c0004, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
然后我们输入:
ls /
列出所有的 zknode
其他的命令参考:
2.zookeeper的基本使用
- 查:
- ls /
查看/路径下有多少个znode。(可以理解为linux目录)
- get /baimei
查看"/baimei"是否有数据。
- stat /school
查看/school的znode状态信息。
- 增:
- create /baimei
在"/"路径下创建一个baimei的znode。
- create /school www.baimei.com
在创建"/school"同时写入数据。
- create /school/class baimei
在"/school"路径下,创建一个名为"class"的znode,其数据为baimei。
- create -s /school/class 123
创建"/school/class"为前缀,
- 改:
- set /baimei 123
修改"/baimei"的znode的数据。
- 删:
- delete /baimei
删除"/baimei"的znode。
- deleteall /school
递归删除"/school"下的所有znode数据。
(3)java代码方式链接:
POM添加引用
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.8.1</version>
</dependency>
版本要对应的, 使用代码模拟增删改查
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
public class zkClient {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
final CountDownLatch countDownLatch = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper("10.168.x.23:2181,10.168.x.23:2182,10.168.x.23:2183", 3000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.SyncConnected) {
countDownLatch.countDown();
}
System.out.println("Watch =>" + event.getType());
}
});
countDownLatch.await();
System.out.println(zk.getState());
String node = "/app1";
Stat state = zk.exists(node, false);
if (state == null) {
System.out.println("创建节点");
String createResult = zk.create(node, "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(createResult);
}
byte[] b = zk.getData(node, false, state);
System.out.println("获取data值 =》" + new String(b));
state = zk.setData(node, "1".getBytes(), state.getVersion());
System.out.println("after update, version changed to =>" + state.getVersion());
zk.delete(node,state.getVersion());
System.out.println("delete complete");
zk.close();
}
ok
欢迎来撩 : zookeeper汇总
欢迎来撩 : 汇总all