内容简介:在第七章节搭建单机Kafka中,我们已经发现了,Kafka是自带Zookeeper的,而且在启动Kafka之前,要先启动Zookeeper,相当于启动了单机Zookeeper,所以我们先说Zookeeper CLI,后面说Zookeeper集群时再具体说配置参数。首先打开终端,连接至我们的服务器,进入这是Zookeeper CLI Client连接Zookeeper的命令,当看到如下信息时,说明连接成功:
在第七章节搭建单机Kafka中,我们已经发现了,Kafka是自带Zookeeper的,而且在启动Kafka之前,要先启动Zookeeper,相当于启动了单机Zookeeper,所以我们先说Zookeeper CLI,后面说Zookeeper集群时再具体说配置参数。
展示zNode
首先打开终端,连接至我们的服务器,进入 /root/kafka_2.12-2.0.0/bin
目录,执行如下命令:
sh zookeeper-shell.sh 127.0.0.1:2181
这是Zookeeper CLI Client连接Zookeeper的命令,当看到如下信息时,说明连接成功:
Connecting to 127.0.0.1:2181 Welcome to ZooKeeper! JLine support is disabled
先来来看看目前Zookeeper里都有哪些zNode:
ls / [cluster, controller_epoch, controller, brokers, zookeeper, kafka-manager, admin, isr_change_notification, consumers, log_dir_event_notification, latest_producer_id_block, config]
ls
命令和 Linux 中的作用一样,在Zookeeper中是展示某个zNode下的所有zNode。这里的 /
表示根zNode。可以看到已经有很多zNode注册在了Zookeeper。再来看看 brokers
下还有哪些zNode:
ls /brokers [ids, topics, seqid]
再来看看有哪些Topic:
ls /brokers/topics [with_keys_topic, first_topic, __consumer_offsets, configured-topic]
查看zNode存储的数据
在上一章节中说过,Zookeeper中的zNode是可以存储数据的,那么我们来看看如何查看zNode中存储的数据,比如我们来看看 /brokers/ids
里保存了什么数据:
get /brokers/ids null cZxid = 0x5 ctime = Wed Dec 19 23:46:53 CST 2018 mZxid = 0x5 mtime = Wed Dec 19 23:46:53 CST 2018 pZxid = 0x43d cversion = 51 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1
get
命令用于查看zNode中存储的数据,从上面的结果看到, /brokers/ids
这个zNode里的数据是 null
,那么看看是否 /brokers/ids
下还有zNode:
ls /brokers/ids [0]
果然, /brokers/ids
下还有zNode,这个zNode很明显是以Broker ID命名的。那再来看看 /brokers/ids/0
里存储了什么样的数据:
get /brokers/ids/0 {"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://ECS外网IP:9092"],"jmx_port":-1,"host":"ECS外网IP","timestamp":"1546570570448","port":9092,"version":4} cZxid = 0x43d ctime = Fri Jan 04 10:56:10 CST 2019 mZxid = 0x43d mtime = Fri Jan 04 10:56:10 CST 2019 pZxid = 0x43d cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x100363316d3004a dataLength = 192 numChildren = 0
从上面的结果可以看到,第一行显示的就是zNode存储的数据, /brokers/ids/0
这个zNode存储了Broker的IP、端口、注册时间戳、JMX端口等信息。这一行之后的信息都是zNode的标准属性了,有各种时间戳、版本号、数据长度、子节点数等。
创建zNode
我们可以使用Zookeeper CLI自行创建zNode:
create /my_node "some data" Created /my_node ls / [cluster, controller, brokers, zookeeper, my_node, admin, isr_change_notification, log_dir_event_notification, controller_epoch, kafka-manager, consumers, latest_producer_id_block, config]
使用 create
命令创建zNode。这里要注意的是,在创建zNode时必须要带着存储数据,哪怕是空也可以:
create /my_node ""
否则是无法创建zNode的。
在创建zNode时不可以一次性创建多级zNode,如果还没有创建 my_node
,直接创建 deeper_node
是不可以的:
create /my_node/deeper_node "some data" Node does not exist: /my_node/deeper_node
所以Zookeeper要一层一层创建zNode:
create /my_node "some data" Created /my_node create /my_node/deeper_node "some data" Created /my_node/deeper_node get /my_node/deeper_node some data cZxid = 0x454 ctime = Mon Jan 07 19:12:20 CST 2019 mZxid = 0x454 mtime = Mon Jan 07 19:12:20 CST 2019 pZxid = 0x454 cversion = 0
更新zNode的数据
我们可以通过 set
命令更新zNode中存储的数据:
set /my_node "new data" cZxid = 0x453 ctime = Mon Jan 07 19:12:07 CST 2019 mZxid = 0x455 mtime = Mon Jan 07 19:14:04 CST 2019 pZxid = 0x454 cversion = 1 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 8 numChildren = 1 get /my_node new data cZxid = 0x453 ctime = Mon Jan 07 19:12:07 CST 2019 mZxid = 0x455 mtime = Mon Jan 07 19:14:04 CST 2019 pZxid = 0x454 cversion = 1 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 8 numChildren = 1
可以看到 dataVersion
和 cversion
从0变成了1。这里注意一下,每当更新zNode存储的数据时, dataVersion
会递增,之所以 cversion
也递增了是因为更新数据本身也是对zNode的修改,如果我们再更新一次数据,就只有 dataVersion
会递增了,因为第一次和第二次都是对zNode存储的数据的修改,只算作一次zNode的改变,所以 cversion
不会再更新:
set /my_node "again new data" cZxid = 0x453 ctime = Mon Jan 07 19:12:07 CST 2019 mZxid = 0x456 mtime = Mon Jan 07 19:16:24 CST 2019 pZxid = 0x454 cversion = 1 dataVersion = 2 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 14 numChildren = 1
如果想让 cversion
变化,那么给 my_node
再增加一个zNode:
create /my_node/another_node "some data" Created /my_node/another_node get /my_node again new data cZxid = 0x453 ctime = Mon Jan 07 19:12:07 CST 2019 mZxid = 0x456 mtime = Mon Jan 07 19:16:24 CST 2019 pZxid = 0x457 cversion = 2 dataVersion = 2 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 14 numChildren = 2
可以看到 cversion
和 numChildren
都变了。
添加zNode Watcher
上一章节同样说过,Zookeeper中的zNode的所有变更都可以被监控到。来看看如何通过CLI给zNode添加Watcher。我们给 /my_node/deeper_node
添加Watcher:
get /my_node/deeper_node true some data cZxid = 0x454 ctime = Mon Jan 07 19:12:20 CST 2019 mZxid = 0x454 mtime = Mon Jan 07 19:12:20 CST 2019 pZxid = 0x454 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 0
通过 get /zNode true
给zNode添加Watcher。当 /my_node/deeper_node
修改数据时,就会收到监听事件了:
set /my_node/deeper_node "new data" WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/my_node/deeper_node cZxid = 0x454 ctime = Mon Jan 07 19:12:20 CST 2019 mZxid = 0x458 mtime = Mon Jan 07 19:24:05 CST 2019 pZxid = 0x454 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 8 numChildren = 0
删除zNode
可以通过 rmr
命令删除zNode,该命令是递归删除,既可以删除指定zNode以及该zNode下的所有zNode:
rmr /my_node ls / [cluster, controller, brokers, zookeeper, admin, isr_change_notification, log_dir_event_notification, controller_epoch, kafka-manager, consumers, latest_producer_id_block, config]
以上所述就是小编给大家介绍的《Kafka从上手到实践-Zookeeper CLI:CRUD zNode》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Kafka从上手到实践-实践真知:搭建Zookeeper集群
- Kafka从上手到实践-实践真知:搭建单机Kafka
- Kafka从上手到实践-实践真知:Kafka Java Consumer
- Kafka从上手到实践-实践真知:Kafka Java Producer
- Nacos上手实践(基于Spring Cloud)
- Kafka从上手到实践-初步认知:Zookeeper
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。