ZooKeeper是一个分布式开源框架,提供了协调分布式应用的基本服务,它向外部应用暴露一组通用服务——分布式同步(Distributed Synchronization)、命名服务(Naming Service)、集群维护(Group Maintenance)等,简化分布式应用协调及其管理的难度,提供高性能的分布式服务。ZooKeeper本身可以以Standalone模式安装运行,不过它的长处在于通过分布式ZooKeeper集群(一个Leader,多个Follower),基于一定的策略来保证ZooKeeper集群的稳定性和可用性,从而实现分布式应用的可靠性。
有关ZooKeeper的介绍,网上很多,也可以参考文章后面,我整理的一些相关链接。
下面,我们简单说明一下ZooKeeper的配置。
ZooKeeper Standalone模式从Apache网站上(zookeeper.apache.org)下载ZooKeeper软件包,我选择了3.3.4版本的(zookeeper-3.3.4.tar.gz),在一台Linux机器上安装非常容易,只需要解压缩后,简单配置一下即可以启动ZooKeeper服务器进程。
将zookeeper-3.3.4/conf目录下面的 zoo_sample.cfg修改为zoo.cfg,配置文件内容如下所示:
tickTime=2000
dataDir=/home/Hadoop/storage/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
上面各个配置参数的含义也非常简单,引用如下所示:
tickTime —— the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.
dataDir —— the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database.
clientPort —— the port to listen for client connections
下面启动ZooKeeper服务器进程:
cd zookeeper-3.3.4/
bin/zkServer.sh start
通过jps命令可以查看ZooKeeper服务器进程,名称为QuorumPeerMain。
在客户端连接ZooKeeper服务器,执行如下命令:
bin/zkCli.sh -server dynamic:2181
上面dynamic是我的主机名,如果在本机执行,则执行如下命令即可:
bin/zkCli.sh
客户端连接信息如下所示:
hadoop@master:~/installation/zookeeper-3.3.4$ bin/zkCli.sh -server dynamic:2181
Connecting to dynamic:2181
2012-01-08 21:30:06,178 - INFO [main:Environment@97] - Client environment:zookeeper.version=3.3.3-1203054, built on 11/17/2011 05:47 GMT
2012-01-08 21:30:06,188 - INFO [main:Environment@97] - Client environment:host.name=master
2012-01-08 21:30:06,191 - INFO [main:Environment@97] - Client environment:Java.version=1.6.0_30
2012-01-08 21:30:06,194 - INFO [main:Environment@97] - Client environment:java.vendor=Sun Microsystems Inc.
2012-01-08 21:30:06,200 - INFO [main:Environment@97] - Client environment:java.home=/home/hadoop/installation/jdk1.6.0_30/jre
2012-01-08 21:30:06,203 - INFO [main:Environment@97] - Client environment:java.class.path=/home/hadoop/installation/zookeeper-3.3.4/bin/../build/classes:/home/hadoop/installation/zookeeper-3.3.4/bin/../build/lib/*.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../zookeeper-3.3.4.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/log4j-1.2.15.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/jline-0.9.94.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-lang-2.4.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-collections-3.2.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-cli-1.1.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/apache-rat-tasks-0.6.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/apache-rat-core-0.6.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../src/java/lib/*.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../conf:/home/hadoop/installation/jdk1.6.0_30/lib/*.jar:/home/hadoop/installation/jdk1.6.0_30/jre/lib/*.jar
2012-01-08 21:30:06,206 - INFO [main:Environment@97] - Client environment:java.library.path=/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386/client:/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386:/home/hadoop/installation/jdk1.6.0_30/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
2012-01-08 21:30:06,213 - INFO [main:Environment@97] - Client environment:java.io.tmpdir=/tmp
2012-01-08 21:30:06,216 - INFO [main:Environment@97] - Client environment:java.compiler=<NA>
2012-01-08 21:30:06,235 - INFO [main:Environment@97] - Client environment:os.name=Linux
2012-01-08 21:30:06,244 - INFO [main:Environment@97] - Client environment:os.arch=i386
2012-01-08 21:30:06,246 - INFO [main:Environment@97] - Client environment:os.version=3.0.0-14-generic
2012-01-08 21:30:06,251 - INFO [main:Environment@97] - Client environment:user.name=hadoop
2012-01-08 21:30:06,254 - INFO [main:Environment@97] - Client environment:user.home=/home/hadoop
2012-01-08 21:30:06,255 - INFO [main:Environment@97] - Client environment:user.dir=/home/hadoop/installation/zookeeper-3.3.4
2012-01-08 21:30:06,264 - INFO [main:ZooKeeper@379] - Initiating client connection, connectString=dynamic:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@bf32c
2012-01-08 21:30:06,339 - INFO [main-SendThread():ClientCnxn$SendThread@1061] - Opening socket connection to server dynamic/192.168.0.107:2181
Welcome to ZooKeeper!
2012-01-08 21:30:06,397 - INFO [main-SendThread(dynamic:2181):ClientCnxn$SendThread@950] - Socket connection established to dynamic/192.168.0.107:2181, initiating session
JLine support is enabled
2012-01-08 21:30:06,492 - INFO [main-SendThread(dynamic:2181):ClientCnxn$SendThread@739] - Session establishment complete on server dynamic/192.168.0.107:2181, sessionid = 0x134b9b714f9000c, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: dynamic:2181(CONNECTED) 0]
接着,可以使用help查看Zookeeper客户端可以使用的基本操作命令。
ZooKeeper 的详细介绍:请点这里
ZooKeeper 的下载地址:请点这里
相关阅读:
分布式服务框架 ZooKeeper -- 管理分布式环境中的数据