文章目录

  • 1. 下载依赖的JDK
  • 2. 下载版本和安装
  • 2.1 上传至linux,并解压
  • 2.2 生成配置文件
  • 2.3 启动zookeeper
  • 2.3.1 windows启动zookeeper
  • 2.4 验证是否启动成功
  • 使用 telnet 和 stat 命令验证服务器启动是否正常
  • 查询进程验证服务器是否启动
  • 3. 客户端脚本
  • 3.1、连接
  • 3.2、创建
  • 参考


1. 下载依赖的JDK

安装 ZooKeeper 之前需要先安装 JDK, 关于 JDK 的安装这里不再赘述。本文以zookeeper-3.5.8,需依赖JDK1.8,关于 JDK 的安装这里不再赘述。

2. 下载版本和安装

ZooKeeper官方网站https://zookeeper.apache.org/

2.1 上传至linux,并解压

上传至/usr/zk

[root@EMS3 zk]# ls
apache-zookeeper-3.5.8-bin.tar.gz
[root@EMS3 zk]# tar -zxvf apache-zookeeper-3.5.8-bin.tar.gz  //解压
apache-zookeeper-3.5.8-bin/docs/
apache-zookeeper-3.5.8-bin/docs/skin/
....

解压完成后会在/usr/zk目录下生成apache-zookeeper-3.5.58-bin目录:

[root@EMS3 zk]# ls
apache-zookeeper-3.5.8-bin  apache-zookeeper-3.5.8-bin.tar.gz

进入目录,查看里面的结构:

[root@EMS3 zk]# cd apache-zookeeper-3.5.8-bin/
[root@EMS3 apache-zookeeper-3.5.8-bin]# ls
bin  conf  docs  lib  LICENSE.txt  NOTICE.txt  README.md  README_packaging.txt

2.2 生成配置文件

进入conf目录,该目录下有示例配置文件zoo_sample.cfg,将其拷贝为zoo.cfg:

[root@EMS3 apache-zookeeper-3.5.8-bin]# cd conf/
[root@EMS3 conf]# ls
configuration.xsl  log4j.properties  zoo_sample.cfg
[root@EMS3 conf]# cp zoo_sample.cfg zoo.cfg
[root@EMS3 conf]# ls
configuration.xsl  log4j.properties  zoo.cfg  zoo_sample.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

单独列出有效参数:

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181

单机模式下,主要配置项作用:

  • tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
  • dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
  • dataLogDir:顾名思义就是 Zookeeper 保存日志文件的目录。
  • clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。

2.3 启动zookeeper

在/usr/zk/apache-zookeeper-3.5.8-bin下执行:

[root@EMS3 apache-zookeeper-3.5.8-bin]# bin/zkServer.sh start conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: conf/zoo.cfg
Starting zookeeper ... STARTED

2.3.1 windows启动zookeeper

运行 apache-zookeeper-3.5.9-bin\bin\zkServer.cmd

2.4 验证是否启动成功

使用 telnet 和 stat 命令验证服务器启动是否正常

[root@EMS3 apache-zookeeper-3.5.8-bin]# telnet  127.0.0.1  2181
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stat       '#此时需要手动输入stat命令'
Zookeeper version: 3.5.8-f439ca583e70862c3068a1f2a7d4d068eec33315, built on 05/04/2020 15:07 GMT
Clients:
 /127.0.0.1:34920[0](queued=0,recved=1,sent=0)

'Latency min/avg/max: 0/0/0      //有类似标红的打印,则表示正常启动
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: standalone
Node count: 5'
Connection closed by foreign host.

查询进程验证服务器是否启动

[root@EMS3 apache-zookeeper-3.5.8-bin]# ps -ef|grep /usr/zk/apache-zookeeper-3.5.8-bin
root      5411     1 10 19:06 pts/2    00:00:01 /opt/openj9-jre/bin/java -Dzookeeper.log.dir='/usr/zk/apache-zookeeper-3.5.8-bin'/bin/../logs -Dzookeeper.log.file=zookeeper-root-server-EMS3.log -Dzookeeper.root.logger=INFO,CONSOLE -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError=kill -9 %p -cp /usr/zk/apache-zookeeper-3.5.8-bin/bin/../zookeeper-server/target/classes:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../build/classes:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../zookeeper-server/target/lib/*.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../build/lib/*.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/zookeeper-jute-3.5.8.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/zookeeper-3.5.8.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/slf4j-api-1.7.25.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/netty-transport-native-unix-common-4.1.48.Final.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/netty-transport-native-epoll-4.1.48.Final.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/netty-transport-4.1.48.Final.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/netty-resolver-4.1.48.Final.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/netty-handler-4.1.48.Final.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/netty-common-4.1.48.Final.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/netty-codec-4.1.48.Final.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/netty-buffer-4.1.48.Final.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/log4j-1.2.17.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/json-simple-1.1.1.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/jline-2.11.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/jetty-util-9.4.24.v20191120.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/jetty-servlet-9.4.24.v20191120.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/jetty-server-9.4.24.v20191120.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/jetty-security-9.4.24.v20191120.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/jetty-io-9.4.24.v20191120.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/jetty-http-9.4.24.v20191120.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/jackson-databind-2.10.3.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/jackson-core-2.10.3.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/jackson-annotations-2.10.3.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/commons-cli-1.2.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../lib/audience-annotations-0.5.0.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../zookeeper-*.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/zk/apache-zookeeper-3.5.8-bin/bin/../conf: -Xmx1000m -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain conf/zoo.cfg
root      6108 14907  0 19:06 pts/2    00:00:00 grep --color=auto /usr/zk/apache-zookeeper-3.5.8-bin      '//查询命令进程'
[root@EMS3 apache-zookeeper-3.5.8-bin]#

如果启动正常,能看到2条结果,其中短的哪个是当前的查询进程,长的正式zk的进程。

3. 客户端脚本

在Zookeeper的安装目录下的 /bin 文件夹中有一个 zkCli.sh 脚本,这个是官方提供的客户端脚本。

3.1、连接

可以直接运行zkCli.sh脚本,默认就是连接本地的Zookeeper服务器

sh zkCli.sh

等价于

sh zkCli.sh -server 127.0.0.1:2181

如果想连接指定的Zookeeper服务器,需要在后面添加一些参数。参照上文,替换ip和端口即可

3.2、创建

使用 create 命令可以创建一个 ZNode 节点。默认创建永久节点。用法如下:

create [-s] [-e] path data acl

-s:表示创建一个顺序节点。
-e:表示创建一个临时节点。
path:表示要创建的节点路径名。
data:表示要创建的节点数据。
acl:表示要创建的节点的访问控制列表。可以不写,默认为 world:anyone:cdrwa。

注意:

  • 不可以递归创建。例如:创建 /create/node 节点时,其父节点 /create 必须已经存在。
  • 临时节点下不可以创建子节点。

例子:

创建一个永久顺序节点,节点路径为 /node,节点数据为 node,访问控制列表为 ip:192.168.0.10:cdrwa**

create -s /node "node" ip:192.168.0.10:cdrwa

参考

《ZooKeeper安装及配置(Windows系统下)》《Zookeeper客户端使用》 更多的客户端命令