安装和运行Zookeeper
我们采用standalone模式,安装运行一个单独的zookeeper服务。安装前请确认您已经安装了Java运行环境。
我们去Apache ZooKeeper releases page下载zookeeper安装包,并解压到本地:
% tar xzf zookeeper-x.y.z.tar.gz
ZooKeeper提供了一些可执行程序的工具,为了方便起见,我们将这些工具的路径加入到PATH环境变量中:
% export ZOOKEEPER_HOME=~/sw/zookeeper-x.y.z
% export PATH=$PATH:$ZOOKEEPER_HOME/bin
运行ZooKeeper之前我们需要编写配置文件。配置文件一般在安装目录下的conf/zoo.cfg
。我们可以把这个文件放在/etc/zookeeper
下,或者放到其他目录下,并在环境变量设置ZOOCFGDIR
指向这个个目录。下面是配置文件的内容:
tickTime=2000
dataDir=/Users/tom/zookeeper
clientPort=2181
tickTime是zookeeper中的基本时间单元,单位是毫秒。datadir是zookeeper持久化数据存放的目录。clientPort是zookeeper监听客户端连接的端口,默认是2181.
启动命令:
% zkServer.sh start
我们通过nc
或者telnet
命令访问2181
端口,通过执行ruok(Are you OK?)命令来检查zookeeper是否启动成功:
% echo ruok | nc localhost 2181
imok
那么我看见zookeeper回答我们“I’m OK”。下表中是所有的zookeeper的命名,都是由4个字符组成。
Category | Command | Description |
---|---|---|
Server status | ruok | Prints imok if the server is running and not in an error state. |
conf | Prints the server configuration (from zoo.cfg). | |
envi | Prints the server environment, including ZooKeeper version, Java version, and other system properties. | |
srvr | Prints server statistics, including latency statistics, the number of znodes, and the server mode (standalone, leader, or follower). | |
stat | Prints server statistics and connected clients. | |
srst | Resets server statistics. | |
isro | Shows whether the server is in read-only (ro) mode (due to a network partition) or read/write mode (rw). | |
Client connections | dump | Lists all the sessions and ephemeral znodes for the ensemble. You must connect to the leader (see srvr) for this command. |
cons | Lists connection statistics for all the server’s clients. | |
crst | Resets connection statistics. | |
Watches | wchs | Lists summary information for the server’s watches. |
wchc | Lists all the server’s watches by connection. Caution: may impact server performance for a large number of watches. | |
wchp | Lists all the server’s watches by znode path. Caution: may impact server performance for a large number of watches. | |
Monitoring | mntr | Lists 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
来访问以上的命令列表。