02 ZooKeeper Getting Started Guide

ZooKeeper Getting Started Guide1. Download

下载地址:http://zookeeper.apache.org/releases.html

2. Standalone Operation(设置)

下载zookeeper的压缩包,解压进入到root目录。

2.1 配置 zoo.cfg

在config目录下创建文件 zoo.cfg,添加如下内容:

tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181

**tickTime: **
是zookeeper的最小时间单位,被使用作为zookeeper的心跳检查,默认session的最小超时时间是 2tickTIme,最大是 20 tickTime
**dataDir : **
Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也保存在这个目录里。这个目录必须存在
**clientPort: **
客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。

2.2 start ZooKeeper
bin/zkServer.sh start

bin/zkServer.sh start
ZooKeeper 日志使用的是log4j
因为这里运行Zookeeper在单机模式下,如果zookeeper进程出现错误,那么服务就会down掉。大多数都是在开发情况下使用这种模式。
在Window下,启动的时候不使用 start

3. Connecting to ZooKeeper

Java方式连接到Zookeeper:
bin/zkCli.sh -server 127.0.0.1:2181

链接成功后:

image

4. 命令
4.1 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
        createpath data acl
        stat path [watch]
        listquota path
        history
        setAcl path acl
        getAcl path
        sync path
        redo cmdno
        addauth scheme auth
        delete path [version]
        setquota -n|-b val path
4.2 ls 列出数据节点下的子节点
[zkshell: 8] ls /
[zookeeper]
4.3 create 创建新的数据节点
[zkshell: 9] create /zk_test my_data
Created /zk_test

创建一个 zk_test节点,指定数据 my_data

4.4 get 获取节点数据
[zkshell: 12] get /zk_test
my_data
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 5
mtime = Fri Jun 05 13:57:06 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0
dataLength = 7
numChildren = 0
4.5 set 修改数据节点
[zkshell: 14] set /zk_test junk
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 6
mtime = Fri Jun 05 14:01:52 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0
dataLength = 4
numChildren = 0
[zkshell: 15] get /zk_test
junk
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 6
mtime = Fri Jun 05 14:01:52 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0
dataLength = 4
numChildren = 0
4.6 delete 删除数据节点
[zkshell: 16] delete /zk_test
[zkshell: 17] ls /
[zookeeper]
[zkshell: 18]
5. Running Replicated ZooKeeper (zookeeper集群)The End

你可能感兴趣的:(02 ZooKeeper Getting Started Guide)