一、从zookeeper的官网https://zookeeper.apache.org/ 下载到名字类似于zookeeper-3.4.5.tar的压缩文件,解压。
二、进入到解压后目录,bin目录下为相关启动脚本,conf下为配置文件,打开conf下面的zoo.cfg文件,修改zookeeper的数据目录和端口号,dataDir=/home/Lyndon/zookeeper clientPort=12181(避免端口冲突)。
三、添加zookeeper路径到/etc/profile
export ZOOKEEPER_HOME=/home/Lyndon/zookeeper-3.4.5
export PATH=$ZOOKEEPER_HOME/bin:$ZOOKEEPER_HOME/conf:$PATH
四、启动zookeeper服务器,./bin/zkServer.sh start这个命令会使得zookeeper服务器在后台运行,如果想在前台运行以便查看服务器的输出,可以通过以下命令: ./bin/zkServer.sh start-foreground,启动后的情况如下:
Lyndon@test_slave:~/zookeeper-3.4.5/bin$ ./zkServer.sh start
JMX enabled by default
Using config: /home/Lyndon/zookeeper-3.4.5/bin/../conf/zoo.cfg
Starting zookeeper ... already running as process 3495.
五、启动客户端,运行以下命令
Lyndon@test_slave:~/zookeeper-3.4.5/bin$ ./zkCli.sh -server localhost:12181 Connecting to localhost:12181 2017-12-11 20:46:56,279 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT 2017-12-11 20:46:56,288 [myid:] - INFO [main:Environment@100] - Client environment:host.name=test_slave 2017-12-11 20:46:56,289 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_79 2017-12-11 20:46:56,293 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2017-12-11 20:46:56,294 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/home/Lyndon/jdk1.7/jdk1.7.0_79/jre 2017-12-11 20:46:56,294 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/Lyndon/zookeeper-3.4.5/bin/../build/classes:/home/Lyndon/zookeeper-3.4.5/bin/../build/lib/*.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/home/Lyndon/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/home/Lyndon/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/home/Lyndon/zookeeper-3.4.5/bin/../conf:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib: 2017-12-11 20:46:56,295 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2017-12-11 20:46:56,295 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 2017-12-11 20:46:56,296 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA> 2017-12-11 20:46:56,296 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 2017-12-11 20:46:56,297 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64 2017-12-11 20:46:56,297 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.13.0-24-generic 2017-12-11 20:46:56,297 [myid:] - INFO [main:Environment@100] - Client environment:user.name=Lyndon 2017-12-11 20:46:56,298 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/Lyndon 2017-12-11 20:46:56,298 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/Lyndon/zookeeper-3.4.5/bin 2017-12-11 20:46:56,300 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:12181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5409396f 2017-12-11 20:46:56,333 [myid:] - INFO [main-SendThread(localhost:12181):ClientCnxn$SendThread@966] - Opening socket connection to server localhost/127.0.0.1:12181. Will not attempt to authenticate using SASL (unknown error) Welcome to ZooKeeper! 2017-12-11 20:46:56,360 [myid:] - INFO [main-SendThread(localhost:12181):ClientCnxn$SendThread@849] - Socket connection established to localhost/127.0.0.1:12181, initiating session JLine support is enabled 2017-12-11 20:46:56,385 [myid:] - INFO [main-SendThread(localhost:12181):ClientCnxn$SendThread@1207] - Session establishment complete on server localhost/127.0.0.1:12181, sessionid = 0x160458e873d0002, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: localhost:12181(CONNECTED) 0]
六、让我们列出此时root下的所有znode,然后使用命令创建一个znode
[zk: localhost:12181(CONNECTED) 0] ls /
[zookeeper]
此时的znode数为空,除了节点/zookeeper之外,该节点内标记了zookeeper服务所需的元数据树。
七、创建一个名为workers的znode:
[zk: localhost:12181(CONNECTED) 1] create /workers ""
Created /workers
[zk: localhost:12181(CONNECTED) 2] ls /
[workers, zookeeper]
[zk: localhost:12181(CONNECTED) 3]
创建/workers节点指定了一个空字符串,是说此刻不希望这个znode中保存数据。
八、删除这个znode:
[zk: localhost:12181(CONNECTED) 3] delete /workers
[zk: localhost:12181(CONNECTED) 4] ls /
[zookeeper]
[zk: localhost:12181(CONNECTED) 5]
这时候我们已经完成了创建和删除znode,使用quit命令退出客户端,使用./zkServer.sh stop停止服务端