本节内容让你快速入门zookeeper。它主要针对想尝试使用zookeeper的开发者,并包含一个ZooKeeper单机服务器的安装说明,你可以用一些命令来验证它的运行,以及简单的编程实例。最后,为了考虑到方便性,有一些复杂的安装部分,例如运行集群式的部署安装,优化事务日志将不在本文档中说明。对于商业部署的完整说明,请参阅管理员指南。
一:前提准备条件
请看下管理员指南中的 System Requirements 。
二:下载
从Apache 镜像里面下载最近的一个稳定版本ZooKeeper 。
三:单机配置
在单机模式中配置一个ZooKeeper服务器是非常简单的。一个JAR文件里包含了这个服务,安装只需要创建一个配置文件。一旦你下载了一个稳定版的ZooKeeper,解压它并用cd命令进入ZooKeeper的根目录。
你需要配置一个文件来启动ZooKeeper,下面有个例子,创建一个文件conf/zoo.cfg:
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
可以用任何应用程序打开conf/zoo.cfg。并且可以通过改变 dataDir的值来指定一个新的目录。每个字段的含义如下:
-
tickTime:是zookeeper的最小时间单元的长度(以毫秒为单位),它被用来设置心跳检测和会话最小超时时间(tickTime的两倍)
-
dataDir:用来配置服务器存储数据快照的目录,除非特别配置说明,事务日志也会被存储到这个目录。
-
clientPort:用来配置监听客户端的连接的端口。
当这些都配置好之后,就可以使用如下命令启动zookeeper:
Zookeeper的日志使用了log4j,更多细节信息请查看zookeeper程序指南中的Logging章节,
你可以从控制台看到日志信息,或者从log4j的配置的日志文件中查看日志。 这一小节,主要讲了如何启动单机模式的zookeeper。在这里没有使用集群的设置,一旦ZooKeeper 进程出现故障,服务就会终止,这对于大多数时候的开发环境是没问题的,但想要运行以集群的方式来运行ZooKeeper ,请查看Running Replicated ZooKeeper。
四:Zookeeper的存储管理
对于长时间运行的生产系统 ,ZooKeeper存储必须要经常维护(dataDir和日志)。如果想了解更多细节请看maintenance章节。
五:连接Zookeeper
$ bin/zkCli.sh -server 127.0.0.1:2181
一旦Zookeeper运行起来,你可以有多种方式来连接它,一旦你连接上Zookeeper你会看到下面这些信息:
1 |
Connecting to localhost:2181 |
2 |
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper). |
3 |
log4j:WARN Please initialize the log4j system properly. |
4 |
Welcome to ZooKeeper! JLine support is enabled |
从shell端你输入help,获得一个可以在客户端执行的命令清单,如下所示:
02 |
ZooKeeper host:port cmd args |
05 |
set path data [version] |
20 |
setquota -n|-b val path |
从这里,你可以尝试一些简单的命令来了解这个命令行接口。首先,开始通过发一行命令,如ls,
[zkshell: 8] ls /
[zookeeper]
Next, create a new znode by running create /zk_test my_data. This creates a new znode and associates the string “my_data” with the node. You should see:
接下来,通过运行create /zk_test my_data命令来创建一个新的节点。这将创建一个新的znode和关联字符串”my_data”节点。您应该看到
[zkshell: 9] create /zk_test my_data
Created /zk_test
发出另一个ls /命令的目录是什么样子
[zkshell: 11] ls /
[zookeeper, zk_test]
现在注意到这个zk_test目录已经被创建,接下来我们还通过set命令来改变zk_test的数据,如下所示:
01 |
zkshell: 14] set /zk_test junk |
05 |
ctime = Fri Jun 05 13:57:06 PDT 2009 |
09 |
mtime = Fri Jun 05 14:01:52 PDT 2009 |
25 |
[zkshell: 15] get /zk_test |
31 |
ctime = Fri Jun 05 13:57:06 PDT 2009 |
35 |
mtime = Fri Jun 05 14:01:52 PDT 2009 |
(注意:我们可以在set命令执行之后,使用get来查正式数据是否已经改变) 最后让我们删除这个节点:
[zkshell: 16] delete /zk_test
[zkshell: 17] ls /
[zookeeper] [zkshell: 18]
六:编程
ZooKeeper 有C语言和java两个版本 ,它们功能上是一样的。C语言版本有2个不同点,单线程和多线程。这些差异仅仅在消息循环时候体现出来。更多细节,请查看Zookeeper编程指南中的的编程案例,演示了使用不同的API的样例代码。
七:集群模式运行
在单机模式下运行ZooKeeper主要用于学习,开发和测试。但是如果在产品中使用,你应该在集群模式下运行ZooKeeper。同一个应用服务器的一个集群组我们称为一个集群。在集群模式下, 在集群下的所有服务器可以复制同样的配置。
在集群模式中,至少需要三个服务器,强烈推荐你使用奇数数量的服务器。如果你仅仅只有两台服务器,一旦一个服务器挂了,你将会面临一个局面,没有足够的机器组成集群,两台服务器本来就比一台服务器更加不稳定,因为会有两个故障点。
集群模式和单点模式一样需要使用conf/zoo.cfg 文件,但是有一些不同,这里有一个例子:
2 |
dataDir=/var/lib/zookeeper |
6 |
server.1=zoo1:2888:3888 |
7 |
server.2=zoo2:2888:3888 |
8 |
server.3=zoo3:2888:3888 |
The new entry, initLimit is timeouts ZooKeeper uses to limit the length of time the ZooKeeper servers in quorum have to connect to a leader. The entry syncLimit limits how far out of date a server can be from a leader.
With both of these timeouts, you specify the unit of time using tickTime. In this example, the timeout for initLimit is 5 ticks at 2000 milleseconds a tick, or 10 seconds.
The entries of the form server.X list the servers that make up the ZooKeeper service. When the server starts up, it knows which server it is by looking for the filemyid in the data directory. That file has the contains the server number, in ASCII.
Finally, note the two port numbers after each server name: ” 2888″ and “3888”. Peers use the former port to connect to other peers. Such a connection is necessary so that peers can communicate, for example, to agree upon the order of updates. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port. Because the default leader election also uses TCP, we currently require another port for leader election. This is the second port in the server entry.
Note
If you want to test multiple servers on a single machine, specify the servername as localhost with unique quorum & leader election ports (i.e. 2888:3888, 2889:3889, 2890:3890 in the example above) for each server.X in that server’s config file. Of course separate dataDirs and distinct clientPorts are also necessary (in the above replicated example, running on a single localhost, you would still have three config files).
Please be aware that setting up multiple servers on a single machine will not create any redundancy. If something were to happen which caused the machine to die, all of the zookeeper servers would be offline. Full redundancy requires that each server have its own machine. It must be a completely separate physical server. Multiple virtual machines on the same physical host are still vulnerable to the complete failure of that host.
八:其他优化
There are a couple of other configuration parameters that can greatly increase performance:
- To get low latencies on updates it is important to have a dedicated transaction log directory. By default transaction logs are put in the same directory as the data snapshots and myid file. The dataLogDir parameters indicates a different directory to use for the transaction logs.
- [tbd: what is the other config param?]