ZooKeeper是一种分布式协调服务,用于管理大型主机。在分布式环境中协调和管理服务是一个复杂的过程。ZooKeeper通过其简单的架构和API解决了这个问题。ZooKeeper允许开发人员专注于核心应用程序逻辑,而不必担心应用程序的分布式特性。
- 下载资源并解压缩
使用wget下载zookeeper到指定的目录下。
wget -P /usr/local https://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
其中的-P 后面携带的是路径。
查看zookeeper的镜像
接着进行解压
tar -zxvf zookeeper-3.4.14.tar.gz
- 创建data目录
在zookeeper目录下创建一个data文件夹,用来存放输出日志
mkdir data
[root@VM_0_15_centos zookeeper-3.4.14]# ll
total 1720
drwxr-xr-x 2 2002 2002 4096 Mar 7 2019 bin
-rw-rw-r-- 1 2002 2002 97426 Mar 7 2019 build.xml
drwxr-xr-x 2 2002 2002 4096 Mar 7 2019 conf
drwxrwxr-x 2 root root 4096 Mar 18 22:42 data
drwxr-xr-x 2 2002 2002 4096 Mar 7 2019 dist-maven
-rw-rw-r-- 1 2002 2002 1709 Mar 7 2019 ivysettings.xml
-rw-rw-r-- 1 2002 2002 10742 Mar 7 2019 ivy.xml
drwxr-xr-x 4 2002 2002 4096 Mar 7 2019 lib
-rw-rw-r-- 1 2002 2002 11970 Mar 7 2019 LICENSE.txt
-rw-rw-r-- 1 2002 2002 3132 Mar 7 2019 NOTICE.txt
-rw-rw-r-- 1 2002 2002 31622 Mar 7 2019 pom.xml
-rw-rw-r-- 1 2002 2002 1765 Mar 7 2019 README.md
-rw-rw-r-- 1 2002 2002 1770 Mar 7 2019 README_packaging.txt
drwxr-xr-x 3 2002 2002 4096 Mar 7 2019 src
-rw-rw-r-- 1 2002 2002 1515359 Mar 7 2019 zookeeper-3.4.14.jar
-rw-rw-r-- 1 2002 2002 836 Mar 7 2019 zookeeper-3.4.14.jar.asc
-rw-rw-r-- 1 2002 2002 33 Mar 7 2019 zookeeper-3.4.14.jar.md5
-rw-rw-r-- 1 2002 2002 41 Mar 7 2019 zookeeper-3.4.14.jar.sha1
drwxr-xr-x 3 2002 2002 4096 Mar 7 2019 zookeeper-client
drwxr-xr-x 12 2002 2002 4096 Mar 7 2019 zookeeper-contrib
drwxr-xr-x 7 2002 2002 4096 Mar 7 2019 zookeeper-docs
drwxr-xr-x 3 2002 2002 4096 Mar 7 2019 zookeeper-it
drwxr-xr-x 4 2002 2002 4096 Mar 7 2019 zookeeper-jute
drwxr-xr-x 5 2002 2002 4096 Mar 7 2019 zookeeper-recipes
drwxr-xr-x 3 2002 2002 4096 Mar 7 2019 zookeeper-server
- 修改zoo.cfg配置文件
进入到conf目录下,拷贝zoo_sample.cfg重命名未zoo.cfg
cp zoo_sample.cfg zoo.cfg
接着用vim命令修改其中的dataDir参数,对应上述新建的data目录。
如果想修改对应的端口号,也可以通过修改clientPort。
dataDir=/usr/local/zookeeper-3.4.14/data
clientPort=2181
- 常用的命令
进入到zookeeper的目录下,操作bin/zkServer.sh。
(1)启动
[root@VM_0_15_centos zookeeper-3.4.14]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
(2)重启
[root@VM_0_15_centos zookeeper-3.4.14]# bin/zkServer.sh restart
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
(3)查看状态
[root@VM_0_15_centos zookeeper-3.4.14]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: standalone
(4)停止
[root@VM_0_15_centos zookeeper-3.4.14]# bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
(5)客户端登录
[root@VM_0_15_centos zookeeper-3.4.14]# bin/zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2020-03-18 23:09:00,106 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on 03/06/2019 16:18 GMT
2020-03-18 23:09:00,110 [myid:] - INFO [main:Environment@100] - Client environment:host.name=VM_0_15_centos
2020-03-18 23:09:00,111 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_201
2020-03-18 23:09:00,113 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2020-03-18 23:09:00,113 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/java/jdk1.8.0_201/jre
2020-03-18 23:09:00,113 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper-3.4.14/bin/../zookeeper-server/target/classes:/usr/local/zookeeper-3.4.14/bin/../build/classes:/usr/local/zookeeper-3.4.14/bin/../zookeeper-server/target/lib/*.jar:/usr/local/zookeeper-3.4.14/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.14/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper-3.4.14/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/zookeeper-3.4.14/bin/../lib/netty-3.10.6.Final.jar:/usr/local/zookeeper-3.4.14/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper-3.4.14/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.14/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper-3.4.14/bin/../zookeeper-3.4.14.jar:/usr/local/zookeeper-3.4.14/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/zookeeper-3.4.14/bin/../conf:
2020-03-18 23:09:00,113 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-03-18 23:09:00,113 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2020-03-18 23:09:00,113 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2020-03-18 23:09:00,113 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2020-03-18 23:09:00,113 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2020-03-18 23:09:00,114 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-514.26.2.el7.x86_64
2020-03-18 23:09:00,114 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2020-03-18 23:09:00,114 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2020-03-18 23:09:00,114 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper-3.4.14
2020-03-18 23:09:00,115 [myid:] - INFO [main:ZooKeeper@442] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5ce65a89
2020-03-18 23:09:00,146 [myid:] - INFO [main-SendThread(VM_0_15_centos:2181):ClientCnxn$SendThread@1025] - Opening socket connection to server VM_0_15_centos/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2020-03-18 23:09:00,305 [myid:] - INFO [main-SendThread(VM_0_15_centos:2181):ClientCnxn$SendThread@879] - Socket connection established to VM_0_15_centos/127.0.0.1:2181, initiating session
2020-03-18 23:09:00,377 [myid:] - INFO [main-SendThread(VM_0_15_centos:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server VM_0_15_centos/127.0.0.1:2181, sessionid = 0x1076e6657eb0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0]
(6) 常用的节点操作命令
-
创建节点
create [-s] [-e] path data acl
-s : 有序节点 ,节点名会尾部追加一串数字字符串
-e : 临时节点 , 客户端断开后,临时节点自动删除。
无:永久性节点 -
读取节点
ls path
: 列出所有的子节点,只能查看第一级的所有子节点
get path
: 获取指定节点的数据和属性信息 。 -
更新节点
set path data [version]
Znode 的数据是有版本的,这个参数指定的是操作哪个版本的数据 -
删除节点
delete path [version]
: 其中的version 也是有版本的 , 需要注意的是如果有子节点,必须要先删除子节点,才能删除当前节点。