标签:
原文地址: http://blog.csdn.net/salonzhou/article/details/47401069
Apache Zookeeper 是由 Apache Hadoop 的 Zookeeper 子项目发展而来,现在已经成为了 Apache 的顶级项目。Zookeeper 为分布式系统提供了高效可靠且易于使用的协同服务,它可以为分布式应用提供相当多的服务,诸如统一命名服务,配置管理,状态同步和组服务等。Zookeeper 接口简单,开发人员不必过多地纠结在分布式系统编程难于处理的同步和一致性问题上,你可以使用 Zookeeper 提供的现成(off-the-shelf)服务来实现分布式系统的配置管理,组管理,Leader 选举等功能。
Zookeeper 维护了大规模分布式系统中的常用对象,比如配置信息,层次化命名空间等,本文将从开发者的角度详细介绍 Zookeeper 的配置信息的意义以及 Zookeeper 的典型应用场景(配置文件的管理、集群管理、分布式队列、同步锁、Leader 选举、队列管理等)。
本文采用 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
然后将此 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
前面两个起的时候可能会有错误提示:
2012-11-21 01:28:12,848 - WARN [QuorumPeer:/0:0:0:0:0:0:0:0:2184:QuorumCnxManager@379] - Cannot open channel to 3 at election address localhost/127.0.0.1:3890
Java.NET.ConnectException: Connection refused
2012-11-21 01:28:12,847 - WARN [QuorumPeer:/0:0:0:0:0:0:0:0:2184:QuorumCnxManager@379] - Cannot open channel to 2 at election address localhost/127.0.0.1:3889
java.net.ConnectException: Connection refused
这个可以不用管,等服务都起来之后就不会有这个提示了.
如果一切顺利,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 安装完成
标签:
原文地址:http://www.cnblogs.com/hadoop-dev/p/5926156.html