首先下载我们的zookeeper,这里我使用的是zookeeper-3.4.12版本,大家也可以去官网下载新版本!

然后把下载下来的压缩包拷贝到Linux服务器下的目录中

然后运行命令   tar -zxvf zookeeper-3.4.12.tar.gz -C /usr/src/zookeeper  解压到对应的文件夹中(-C后面的路径必须得是存在的,不存在就自行创建!

然后去解压指定的目录下,在zookeeper目录下下建立log文件夹和data文件夹用于存放日志和数据

[root@localhost zookeeper-3.4.12]# cd /usr/local/zookeeper/
[root@localhost zookeeper]# ls
zookeeper-3.4.12.tar.gz
[root@localhost zookeeper]# cd /usr/src/zookeeper/
[root@localhost zookeeper]# ls
zookeeper-3.4.12
[root@localhost zookeeper]# cd zookeeper-3.4.12/
[root@localhost zookeeper-3.4.12]# ls
bin        dist-maven       lib          README_packaging.txt  zookeeper-3.4.12.jar.asc
build.xml  docs             LICENSE.txt  recipes               zookeeper-3.4.12.jar.md5
conf       ivysettings.xml  NOTICE.txt   src                   zookeeper-3.4.12.jar.sha1
contrib    ivy.xml          README.md    zookeeper-3.4.12.jar
[root@localhost zookeeper-3.4.12]# mkdir data
[root@localhost zookeeper-3.4.12]# mkdir log
[root@localhost zookeeper-3.4.12]# ls
bin        contrib     docs             lib          NOTICE.txt            recipes               zookeeper-3.4.12.jar.asc
build.xml  data        ivysettings.xml  LICENSE.txt  README.md             src                   zookeeper-3.4.12.jar.md5
conf       dist-maven  ivy.xml          log          README_packaging.txt  zookeeper-3.4.12.jar  zookeeper-3.4.12.jar.sha1
[root@localhost zookeeper-3.4.12]# 

由于Zookeeper运行需要一个zoo.cfg的配置文件,在conf目录下没有conf.xml文件,所以进入conf目录,复制一份zoo_sample.cfg取名为zoo.cfg

[root@localhost zookeeper-3.4.12]# cd conf/
[root@localhost conf]# ls
configuration.xsl  log4j.properties  zoo_sample.cfg
[root@localhost conf]# cp zoo_sample.cfg zoo.cfg
[root@localhost conf]# ls
configuration.xsl  log4j.properties  zoo.cfg  zoo_sample.cfg

然后vi zoo.cfg修改配置文件

保存数据的目录

dataDir 对应创建的data路径

保存日志的目录(配置文件中不存在,手动添加)

dataLogDir 对应创建的log路径

 

dataDir=/usr/src/zookeeper/zookeeper-3.4.12/data
dataLogDir=/usr/src/zookeeper/zookeeper-3.4.12/log

配置好了后进入bin目录输入 ./zkServer.sh start启动zookeeper

[root@localhost zookeeper-3.4.12]# cd bin/
[root@localhost bin]# ls
README.txt  zkCleanup.sh  zkCli.cmd  zkCli.sh  zkEnv.cmd  zkEnv.sh  zkServer.cmd  zkServer.sh  zookeeper.out
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/src/zookeeper/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost bin]# 


启动成功后连接zookeeper

[root@localhost bin]# ./zkCli.sh 
Connecting to localhost:2181
2018-11-05 13:08:32,133 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
2018-11-05 13:08:32,137 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=localhost
2018-11-05 13:08:32,138 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_172
2018-11-05 13:08:32,139 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-11-05 13:08:32,139 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/src/jdk8/jdk1.8.0_172/jre
2018-11-05 13:08:32,139 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/src/zookeeper/zookeeper-3.4.12/bin/../build/classes:/usr/src/zookeeper/zookeeper-3.4.12/bin/../build/lib/*.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/slf4j-api-1.7.25.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/netty-3.10.6.Final.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/log4j-1.2.17.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/jline-0.9.94.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/audience-annotations-0.5.0.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../zookeeper-3.4.12.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../src/java/lib/*.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../conf:.:/usr/src/jdk8/jdk1.8.0_172/lib:/usr/src/jdk8/jdk1.8.0_172/jre/lib:
2018-11-05 13:08:32,139 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-11-05 13:08:32,139 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-11-05 13:08:32,140 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2018-11-05 13:08:32,140 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2018-11-05 13:08:32,140 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2018-11-05 13:08:32,140 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-862.el7.x86_64
2018-11-05 13:08:32,140 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2018-11-05 13:08:32,140 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2018-11-05 13:08:32,140 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/usr/src/zookeeper/zookeeper-3.4.12/bin
2018-11-05 13:08:32,141 [myid:] - INFO  [main:ZooKeeper@441] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@799f7e29
Welcome to ZooKeeper!
2018-11-05 13:08:32,175 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1028] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2018-11-05 13:08:32,295 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@878] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
2018-11-05 13:08:32,309 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x10000c48c800002, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] 

基本上大功告成!

然后就是基本的操作命令:

1. ls /: 使用 ls 命令来查看当前 ZooKeeper 中所包含的内容

2.  ls2 /: 查看当前节点数据并能看到更新次数等数据

3. 创建文件,并设置初始内容: create /xf "xfxf" 创建一个新的znode节点“ xf ”以及与它关联的字符串"xfxf"

4. 获取文件内容: get /xf 确认新创建的znode节点是否包含我们所创建的"xfxf"字符串

5. 修改文件内容: set /zk "xfxfxf" 对 zk 所关联的字符串"xfxf"进行设置,修改为"xfxfxf"

6. 删除文件: delete /zk 将刚才创建的 znode 删除;rmr /xf 删除节点和他的子节点

7. 退出客户端: quit

8. 帮助命令: help 

 

[zk: localhost:2181(CONNECTED) 26] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 27] create /xf "xfxf"
Created /xf
[zk: localhost:2181(CONNECTED) 28] ls /
[zookeeper, xf]
[zk: localhost:2181(CONNECTED) 29] get /xf
xfxf
cZxid = 0xa
ctime = Mon Nov 05 13:24:59 CST 2018
mZxid = 0xa
mtime = Mon Nov 05 13:24:59 CST 2018
pZxid = 0xa
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: localhost:2181(CONNECTED) 30] set /xf "xfxfxf"
cZxid = 0xa
ctime = Mon Nov 05 13:24:59 CST 2018
mZxid = 0xb
mtime = Mon Nov 05 13:25:19 CST 2018
pZxid = 0xa
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: localhost:2181(CONNECTED) 32] get /xf
xfxfxf
cZxid = 0xa
ctime = Mon Nov 05 13:24:59 CST 2018
mZxid = 0xb
mtime = Mon Nov 05 13:25:19 CST 2018
pZxid = 0xa
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: localhost:2181(CONNECTED) 33] 


The end !!!

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐