安装和运行Zookeeper

我们采用standalone模式,安装运行一个单独的zookeeper服务。安装前请确认您已经安装了Java运行环境。

我们去Apache ZooKeeper releases page下载zookeeper安装包,并解压到本地:

  1. % tar xzf zookeeper-x.y.z.tar.gz

ZooKeeper提供了一些可执行程序的工具,为了方便起见,我们将这些工具的路径加入到PATH环境变量中:

  1. % export ZOOKEEPER_HOME=~/sw/zookeeper-x.y.z
  2. % export PATH=$PATH:$ZOOKEEPER_HOME/bin

运行ZooKeeper之前我们需要编写配置文件。配置文件一般在安装目录下的conf/zoo.cfg。我们可以把这个文件放在/etc/zookeeper下,或者放到其他目录下,并在环境变量设置ZOOCFGDIR指向这个个目录。下面是配置文件的内容:

  1. tickTime=2000
  2. dataDir=/Users/tom/zookeeper
  3. clientPort=2181

tickTime是zookeeper中的基本时间单元,单位是毫秒。datadir是zookeeper持久化数据存放的目录。clientPort是zookeeper监听客户端连接的端口,默认是2181.

启动命令:

  1. % zkServer.sh start

我们通过nc或者telnet命令访问2181端口,通过执行ruok(Are you OK?)命令来检查zookeeper是否启动成功:

  1. % echo ruok | nc localhost 2181
  2. imok

那么我看见zookeeper回答我们“I’m OK”。下表中是所有的zookeeper的命名,都是由4个字符组成。

CategoryCommandDescription
Server statusruokPrints imok if the server is running and not in an error state.
confPrints the server configuration (from zoo.cfg).
enviPrints the server environment, including ZooKeeper version, Java version, and other system properties.
srvrPrints server statistics, including latency statistics, the number of znodes, and the server mode (standalone, leader, or follower).
statPrints server statistics and connected clients.
srstResets server statistics.
isroShows whether the server is in read-only (ro) mode (due to a network partition) or read/write mode (rw).
Client connectionsdumpLists all the sessions and ephemeral znodes for the ensemble. You must connect to the leader (see srvr) for this command.
consLists connection statistics for all the server’s clients.
crstResets connection statistics.
WatcheswchsLists summary information for the server’s watches.
wchcLists all the server’s watches by connection. Caution: may impact server performance for a large number of watches.
wchpLists all the server’s watches by znode path. Caution: may impact server performance for a large number of watches.
MonitoringmntrLists server statistics in Java properties format, suitable as a source for monitoring systems such as Ganglia and Nagios.

3.5.0以上的版本会有一个内嵌的web服务,通过访问http://localhost:8080/commands来访问以上的命令列表。