本节介绍单机的zookeeper安装,官方下载地址如下:
我这里使用的是3.4.11版本,所以找到相应的版本点击进去,复制到.tar.gz的下载链接到Linux上进行下载。命令如下:
[root@study-01 ~]# cd /usr/local/src/
[root@study-01 /usr/local/src]# wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
下载完成之后将其解压到/usr/local/目录下:
[root@study-01 /usr/local/src]# tar -zxvf zookeeper-3.4.11.tar.gz -C /usr/local/
[root@study-01 /usr/local/src]# cd ../zookeeper-3.4.11/
[root@study-01 /usr/local/zookeeper-3.4.11]# ls
bin dist-maven lib README_packaging.txt zookeeper-3.4.11.jar.asc
build.xml docs LICENSE.txt recipes zookeeper-3.4.11.jar.md5
conf ivysettings.xml NOTICE.txt src zookeeper-3.4.11.jar.sha1
contrib ivy.xml README.md zookeeper-3.4.11.jar
[root@study-01 /usr/local/zookeeper-3.4.11]#
安装完成之后,我们就可以配置环境变量了。如下:
[root@study-01 ~]# vim ./.bash_profile # 增加内容如下
export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.11/
export PATH=$PATH:$ZOOKEEPER_HOME/bin
[root@study-01 ~]# source ./.bash_profile
进入conf目录,拷贝官方提供的模板配置文件,然后进行编辑:
[root@study-01 /usr/local/zookeeper-3.4.11]# cd conf/
[root@study-01 /usr/local/zookeeper-3.4.11/conf]# cp zoo_sample.cfg zoo.cfg
[root@study-01 /usr/local/zookeeper-3.4.11/conf]# vim zoo.cfg # 配置内容如下
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper-3.4.11/dataDir
dataLogDir=/usr/local/zookeeper-3.4.11/dataLogDir
clientPort=2181
[root@study-01 /usr/local/zookeeper-3.4.11/conf]# cd ../
[root@study-01 /usr/local/zookeeper-3.4.11]# mkdir {dataDir,dataLogDir}
zoo.cfg配置:
以上我们就完成了单机的zookeeper安装与配置,接下来我们尝试启动一下zookeeper服务:
[root@study-01 ~]# zkServer.sh start # 启动zookeeper服务
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.11/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@study-01 ~]# netstat -lntp | grep java # 检查端口是否有正常监听
tcp6 0 0 :::58056 :::* LISTEN 3057/java
tcp6 0 0 :::2181 :::* LISTEN 3057/java
[root@study-01 ~]# zkServer.sh status # 查看zookeeper服务状态
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.11/bin/../conf/zoo.cfg
Mode: standalone
[root@study-01 ~]# ps aux |grep java # 检查服务进程
root 3057 0.9 0.9 5230652 79840 pts/0 Sl 04:26 0:01 /usr/local/jdk1.8/bin/java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /usr/local/zookeeper-3.4.11/bin/../build/classes:/usr/local/zookeeper-3.4.11/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.11/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper-3.4.11/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper-3.4.11/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper-3.4.11/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper-3.4.11/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.11/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper-3.4.11/bin/../zookeeper-3.4.11.jar:/usr/local/zookeeper-3.4.11/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.11/bin/../conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /usr/local/zookeeper-3.4.11/bin/../conf/zoo.cfg
root 3146 0.0 0.0 112680 976 pts/0 S+ 04:29 0:00 grep --color=auto java
[root@study-01 ~]#
然后再关闭zookeeper服务:
[root@study-01 ~]# zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.11/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@study-01 ~]#
能够正常开/关后,就代表我们的zookeeper成功安装好了。
zookeeper基本数据模型:
基本数据模型是一个树形结构,类似于前端开发中的tree.js组件。或者像一个网站的目录结构:
zk的数据模型也可以理解为linux/unix的文件目录,如:/usr/local/...。如下图:
启动zk服务端:
[root@study-01 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.11/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@study-01 ~]#
启动zk客户端连接:
[root@study-01 ~]# zkCli.sh
查看可以使用哪些命令:
[zk: localhost:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
[zk: localhost:2181(CONNECTED) 1]
查看znode,十分类似于在Linux下查看目录的操作:
[zk: localhost:2181(CONNECTED) 2] ls / # 根节点
[zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /zookeeper # 父节点
[quota]
[zk: localhost:2181(CONNECTED) 4] ls /zookeeper/quota # 子节点
[]
[zk: localhost:2181(CONNECTED) 5]
关闭zk客户端连接:
[zk: localhost:2181(CONNECTED) 5] quit # 退出
Quitting...
2018-04-21 17:48:35,534 [myid:] - INFO [main:ZooKeeper@687] - Session: 0x1000034ed8d0000 closed
2018-04-21 17:48:35,536 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@520] - EventThread shut down for session: 0x1000034ed8d0000
[root@study-01 ~]#
发布与订阅,类似消息队列MQ(amq,rmq...),dubbo发布者把数据存在znode上,订阅者会读取这个数据。
提供分布式锁,分布式环境中也会有不同进程之间争夺资源,这时候就需要锁机制来保证数据的一致性,类似于多线程中的锁。例如下图中这种多个服务器访问同一个文件时,就需要分布式锁来让他们进行排队访问:
原文地址:http://blog.51cto.com/zero01/2106042