1.1 2 ZooKeeper安装和基本使用
安装要求:JDK版本>=1.6
下载:http://mirrors.cnnic.cn/apache/zookeeper/
1、本地模式
conf/zoo.cfg
tickTime=2000 dataDir=/var/lib/zookeeper clientPort=2181
tickTime
the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.
dataDir
the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database.
clientPort
the port to listen for client connections
启动
bin/zkServer.sh start
客户端连接
bin/zkCli.sh -server 127.0.0.1:2181
登录后界面
输入help
[zkshell: 0] help ZooKeeper host:port cmd args get path [watch] ls path [watch] set path data [version] delquota [-n|-b] path quit printwatches on|off create path data acl stat path [watch] listquota path history setAcl path acl getAcl path sync path redo cmdno addauth scheme auth delete path [version] deleteall path setquota -n|-b val path
查看节点
[zk: 127.0.0.1:2181(CONNECTED) 0] ls / [zookeeper]
create a new znode by running create /zk_test my_data. This creates a new znode and associates the string "my_data" with the node.
[zk: 127.0.0.1:2181(CONNECTED) 1] create /zk_test my_data Created /zk_test
Issue another ls / command to see what the directory looks like:
[zk: 127.0.0.1:2181(CONNECTED) 0] ls / [zookeeper, zk_test]
查看节点数据
[zk: 127.0.0.1:2181(CONNECTED) 1] get /zk_test my_data cZxid = 0x4 ctime = Wed Oct 07 21:14:51 CST 2015 mZxid = 0x4 mtime = Wed Oct 07 21:14:51 CST 2015 pZxid = 0x4 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0
Zxid: Every change to the ZooKeeper state receives a stamp in the form of a zxid (ZooKeeper Transaction Id). This exposes the total ordering of all changes to ZooKeeper. Each change will have a unique zxid and if zxid1 is smaller than zxid2 then zxid1 happened before zxid2.
更新数据
We can change the data associated with zk_test by issuing the set command, as in:
[zk: 127.0.0.1:2181(CONNECTED) 2] set /zk_test junk cZxid = 0x4 ctime = Wed Oct 07 21:14:51 CST 2015 mZxid = 0x7 mtime = Wed Oct 07 21:17:42 CST 2015 pZxid = 0x4 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 4 numChildren = 0 [zk: 127.0.0.1:2181(CONNECTED) 3] get /zk_test junk cZxid = 0x4 ctime = Wed Oct 07 21:14:51 CST 2015 mZxid = 0x7 mtime = Wed Oct 07 21:17:42 CST 2015 pZxid = 0x4 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 4 numChildren = 0
删除节点
[zk: 127.0.0.1:2181(CONNECTED) 4] delete /zk_test [zk: 127.0.0.1:2181(CONNECTED) 5] ls / [zookeeper] [zk: 127.0.0.1:2181(CONNECTED) 6]
参数说明:
* czxid
The zxid of the change that caused this znode to be created.
* mzxid
The zxid of the change that last modified this znode.
* ctime
The time in milliseconds from epoch when this znode was created.
* mtime
The time in milliseconds from epoch when this znode was last modified.
* version
The number of changes to the data of this znode.
* cversion
The number of changes to the children of this znode.
* aversion
The number of changes to the ACL of this znode.
* ephemeralOwner
The session id of the owner of this znode if the znode is an ephemeral node. If it is not an ephemeral node, it will be zero.
* dataLength
The length of the data field of this znode.
* numChildren
The number of children of this znode.