代码改变世界

Zookeeper 初体验之——伪分布式安装

2012-07-19 21:09  Haippy  阅读(26008)  评论(6编辑  收藏  举报

简介

Apache Zookeeper 是由 Apache Hadoop 的 Zookeeper 子项目发展而来,现在已经成为了 Apache 的顶级项目。Zookeeper 为分布式系统提供了高效可靠且易于使用的协同服务,它可以为分布式应用提供相当多的服务,诸如统一命名服务,配置管理,状态同步和组服务等。Zookeeper 接口简单,开发人员不必过多地纠结在分布式系统编程难于处理的同步和一致性问题上,你可以使用 Zookeeper 提供的现成(off-the-shelf)服务来实现分布式系统的配置管理,组管理,Leader 选举等功能。

Zookeeper 维护了大规模分布式系统中的常用对象,比如配置信息,层次化命名空间等,本文将从开发者的角度详细介绍 Zookeeper 的配置信息的意义以及 Zookeeper 的典型应用场景(配置文件的管理、集群管理、分布式队列、同步锁、Leader 选举、队列管理等)。

Zookeeper 安装与配置

本文采用 Zookeeper-3.4.0 以基础介绍它的安装步骤以及配置信息,最新的代码可以到 Zookeeper 的官网http://zookeeper.apache.org/下载。Zookeeper功能强大,但是安装却十分简单,下面重点以伪分布式模式来介绍 Zookeeper 的安装。

伪分布式模式安装

Zookeeper 安装模式包括:单机模式,伪分布式模式和完全的集群模式。单机模式最简单,本文将跳过单机模式安装(单机模式安装步骤参见 Zeekeeper 官方文档http://zookeeper.apache.org/doc/current/zookeeperStarted.html),伪分布式模式与集群模式配置差别不大,由于手头机器有限,所以本文采用了在单台机器上伪分布式安装。

本文在Ubuntu 12.04上操作,Java 环境为 OpenJDK 1.7。安装 Zookeeper 前首先下载你需要的版本,暂时解压到指定目录(本文解压至~/zookeeper/目录下),并修改配置(可能需要多次修改配置文件),本次伪分布式模拟 5 个 Zookeeper 节点,事先在/tmpzookeeper目录下建立5个文件夹,分别命名为:server001,server002,server003,server004,server005,然后在每个server00#文件夹下面新建 data 和 logs 子文件夹。

Zookeeper 的配置文件主要在 conf 目录,包括zoo.cfg (zoo_sample.cfg)和log4j.properties,修改 zoo_sample.cfg,重命名为zoo.cgf,打开zoo.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
#
# 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

将内容修改为(server001节点的配置文件):

# 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/server001/data
dataLogDir=/tmp/zookeeper/server001/logs
# the port at which the clients will connect
clientPort=2181
#
# 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
server.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773
server.4=127.0.0.1:8884:7774
server.5=127.0.0.1:8885:7775
  • tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
  • dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
  • clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
  • initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 5个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000=10 秒
  • syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2*2000=4 秒
  • server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。

然后将此 zookeeper 包拷贝至 /tmp/zookeeper/server001/ 目录下,并在 /tmp/zookeeper/server001/data/ 下建立一个myid文件,文件内容为 1,

echo "1" >> /tmp/zookeeper/server001/data/myid


继续修改~/zookeeper/目录中的zookeeper配置文件文件(server002的配置文件,注意 clientPort=2182,与 server001 中的 clientPort=2181 不同,后续修改配置均需设置不同的 clientPort),内容如下:

# 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/server002/data
dataLogDir=/tmp/zookeeper/server002/logs
# the port at which the clients will connect
clientPort=2182
#
# 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
server.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773
server.4=127.0.0.1:8884:7774
server.5=127.0.0.1:8885:7775

然后将此 zookeeper 包拷贝至 /tmp/zookeeper/server002/ 目录下,并在 /tmp/zookeeper/server002/data/ 下建立一个myid文件,文件内容为 2,

echo "2" >> /tmp/zookeeper/server001/data/myid

依次修改配置文件,建立 server003,server004,server005节点文件夹,完成上述步骤后/tmp/zookeeper目录结构如下:

forhappy@forhappy-lenovo:/tmp/zookeeper$ tree -d -L 2
.
├── server001
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
├── server002
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
├── server003
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
├── server004
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
└── server005
    ├── data
    ├── logs
    └── zookeeper-3.4.0

然后依次进入每个文件夹节点的zookeeper目录中,启动zookeeper服务,

$ bin/zkServer.sh start

如果一切顺利,Zookeeper 伪分布式模式安装成功,下面验证 Zookeeper 安装的正确性。

进入任意一个文件夹节点的zookeeper包所在的目录,执行一下命令:

$ bin/zkCli.sh  -server 127.0.0.1:2181

执行成功后:

forhappy@forhappy-lenovo:/tmp/zookeeper/server001/zookeeper-3.4.0$ bin/zkCli.sh  -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181

Welcome to ZooKeeper!

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: 127.0.0.1:2181(CONNECTED) 0]

help 帮助:

[zk: 127.0.0.1:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
    connect host:port
    get path [watch]
    ls path [watch]
    set path data [version]
    rmr path
    delquota [-n|-b] path
    quit 
    printwatches on|off
    create [-s] [-e] path data acl
    stat path [watch]
    close 
    ls2 path [watch]
    history 
    listquota path
    setAcl path acl
    getAcl path
    sync path
    redo cmdno
    addauth scheme auth
    delete path [version]
    setquota -n|-b val path

至此,Zookeeper 安装完成,下一篇博客将介绍 Zookeeper Java API,并给出 Zookeeper 典型的应用场景。

参考资料:

http://zookeeper.apache.org/

http://zookeeper.apache.org/doc/current/zookeeperStarted.html

http://zookeeper.apache.org/doc/current/index.html

http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/

http://blog.csdn.net/franklysun/article/details/6424582