ZooKeeper是做什么的呢?总体上来说:
针对分布式系统的:
(1)结点管理,特别是集群很大时。
(2)配置文件,特别是需要反复改动的配置。
(3)协同、同步。
自己做实验的话,StandAlone模式就行。
1、下载
wget http://apache.spinellicreations.com/zookeeper/zookeeper-3.4.3/zookeeper-3.4.3.tar.gz tar -xzvf zookeeper-3.4.3.tar.gz
2、配置文件
cd zookeeper-3.4.3 cd conf cp zoo_sample.cfg zoo.cfg
然后更改dataDir=为一个非tmp的目录。
3、启动
cd .. ./bin/zkServer.sh start
4、基本命令
cd bin ./zkCli.sh -server 127.0.0.1:2181
列出根目录树
[zk: 127.0.0.1:2181(CONNECTED) 1] ls / [zookeeper]
创建一个叫做/zk_test的结点,给结点追加字符串数据"test_data"
[zk: 127.0.0.1:2181(CONNECTED) 0] create /zk_test my_data Created /zk_test
获取/zk_test结点的数据
[zk: 127.0.0.1:2181(CONNECTED) 4] get /zk_test my_data cZxid = 0x9 ctime = Mon May 07 12:03:23 CST 2012 mZxid = 0x9 mtime = Mon May 07 12:03:23 CST 2012 pZxid = 0x9 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0
更改/zk_test的数据,再显示
[zk: 127.0.0.1:2181(CONNECTED) 5] set /zk_test abcdefg cZxid = 0x9 ctime = Mon May 07 12:03:23 CST 2012 mZxid = 0xb mtime = Mon May 07 12:05:55 CST 2012 pZxid = 0x9 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0 [zk: 127.0.0.1:2181(CONNECTED) 6] get /zk_test abcdefg cZxid = 0x9 ctime = Mon May 07 12:03:23 CST 2012 mZxid = 0xb mtime = Mon May 07 12:05:55 CST 2012 pZxid = 0x9 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0
每个结点既可以有孩子,也可以追加数据!
[zk: 127.0.0.1:2181(CONNECTED) 9] create /zk_test/123 456 Created /zk_test/123 [zk: 127.0.0.1:2181(CONNECTED) 10] ls /zk_test [123] [zk: 127.0.0.1:2181(CONNECTED) 11] get /zk_test abcdefg cZxid = 0x9 ctime = Mon May 07 12:03:23 CST 2012 mZxid = 0xb mtime = Mon May 07 12:05:55 CST 2012 pZxid = 0xc cversion = 1 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 1
删除结点:
#非空的(有孩子),不能删 [zk: 127.0.0.1:2181(CONNECTED) 12] delete /zk_test Node not empty: /zk_test #依次删除孩子,再删除父亲 [zk: 127.0.0.1:2181(CONNECTED) 13] delete /zk_test/123 [zk: 127.0.0.1:2181(CONNECTED) 14] delete /zk_test [zk: 127.0.0.1:2181(CONNECTED) 16] ls / [zookeeper]