ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护、名字服务、分布式同步、组服务等。ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
Zookeeper是Google的Chubby一个开源的实现.是高有效和可靠的协同工作系统.Zookeeper能够用来leader选举,配置信息维护等.在一个分布式的环境中,我们需要一个Master实例或存储一些配置信息,确保文件写入的一致性等.Zookeeper能够保证如下3点:
在Zookeeper中,znode是一个跟Unix文件系统路径相似的节点,可以往这个节点存储或获取数据.如果在创建znode时Flag设置 为EPHEMERAL,那么当这个创建这个znode的节点和Zookeeper失去连接后,这个znode将不再存在在Zookeeper 里.Zookeeper使用Watcher察觉事件信息,当客户端接收到事件信息,比如连接超时,节点数据改变,子节点改变,可以调用相应的行为来处理数 据.Zookeeper的Wiki页面展示了如何使用Zookeeper来处理事件通知,队列,优先队列,锁,共享锁,可撤销的共享锁,两阶段提交.
Zookeeper官网
首先下载最新版 zookeeper-3.4.5.tar.gz
然后解压
tar zxvf zookeeper-3.4.5.tar.gz
然后进入conf目录修改zoo_sample.cfg为zoo.cfg
然后vi 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
编辑保存后进入bin目录执行
./zkServer.sh start
[root@localhost bin]# ./zkCli.sh Connecting to localhost:2181 2013-01-16 14:36:22,484 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT 2013-01-16 14:36:22,488 [myid:] - INFO [main:Environment@100] - Client environment:host.name=localhost.localdomain 2013-01-16 14:36:22,489 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.6.0_29 2013-01-16 14:36:22,489 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Sun Microsystems Inc. 2013-01-16 14:36:22,490 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.6.0_29/jre 2013-01-16 14:36:22,490 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper-3.4.5/bin/../build/classes:/usr/local/zookeeper-3.4.5/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/usr/local/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/usr/local/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/usr/local/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.5/bin/../conf:./:/usr/java/jdk1.6.0_29/lib:/usr/java/jdk1.6.0_29/jre/lib 2013-01-16 14:36:22,491 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/jdk1.6.0_29/jre/lib/i386/client:/usr/java/jdk1.6.0_29/jre/lib/i386:/usr/java/jdk1.6.0_29/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib 2013-01-16 14:36:22,491 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 2013-01-16 14:36:22,491 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA> 2013-01-16 14:36:22,492 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 2013-01-16 14:36:22,492 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=i386 2013-01-16 14:36:22,493 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.18-308.24.1.el5 2013-01-16 14:36:22,493 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root 2013-01-16 14:36:22,493 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root 2013-01-16 14:36:22,494 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper-3.4.5/bin 2013-01-16 14:36:22,496 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5ffb18 Welcome to ZooKeeper! 2013-01-16 14:36:22,592 [myid:] - INFO [main-SendThread(localhost.localdomain:2181):ClientCnxn$SendThread@966] - Opening socket connection to server localhost.localdomain/127.0.0.1:2181. Will not attempt to authenticate using SASL (无法定位登录配置) 2013-01-16 14:36:22,601 [myid:] - INFO [main-SendThread(localhost.localdomain:2181):ClientCnxn$SendThread@849] - Socket connection established to localhost.localdomain/127.0.0.1:2181, initiating session JLine support is enabled 2013-01-16 14:36:22,693 [myid:] - INFO [main-SendThread(localhost.localdomain:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server localhost.localdomain/127.0.0.1:2181, sessionid = 0x13c3c5224cc0000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: localhost: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 [zk: localhost:2181(CONNECTED) 1] create /my_data "data.ch.com" Created /my_data [zk: localhost:2181(CONNECTED) 2] get /my_data "data.ch.com" cZxid = 0x2 ctime = Wed Jan 16 14:38:20 CST 2013 mZxid = 0x2 mtime = Wed Jan 16 14:38:20 CST 2013 pZxid = 0x2 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 13 numChildren = 0 [zk: localhost:2181(CONNECTED) 3] set /my_date "123456.com" Node does not exist: /my_date [zk: localhost:2181(CONNECTED) 4] set /my_data "123456.com" cZxid = 0x2 ctime = Wed Jan 16 14:38:20 CST 2013 mZxid = 0x4 mtime = Wed Jan 16 14:38:58 CST 2013 pZxid = 0x2 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 12 numChildren = 0 [zk: localhost:2181(CONNECTED) 5] get /my_data "123456.com" cZxid = 0x2 ctime = Wed Jan 16 14:38:20 CST 2013 mZxid = 0x4 mtime = Wed Jan 16 14:38:58 CST 2013 pZxid = 0x2 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 12 numChildren = 0 [zk: localhost:2181(CONNECTED) 6] create /mytemp "你好吗" Created /mytemp [zk: localhost:2181(CONNECTED) 7] get /myt Node does not exist: /myt [zk: localhost:2181(CONNECTED) 8] get /mytemp "你好吗" cZxid = 0x5 ctime = Wed Jan 16 14:40:44 CST 2013 mZxid = 0x5 mtime = Wed Jan 16 14:40:44 CST 2013 pZxid = 0x5 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 11 numChildren = 0 [zk: localhost:2181(CONNECTED) 9] get /mytemp "你好吗" cZxid = 0x5 ctime = Wed Jan 16 14:40:44 CST 2013 mZxid = 0x5 mtime = Wed Jan 16 14:40:44 CST 2013 pZxid = 0x5 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 11 numChildren = 0 [zk: localhost:2181(CONNECTED) 10] get /my_data "123456.com" cZxid = 0x2 ctime = Wed Jan 16 14:38:20 CST 2013 mZxid = 0x4 mtime = Wed Jan 16 14:38:58 CST 2013 pZxid = 0x2 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 12 numChildren = 0 [zk: localhost:2181(CONNECTED) 11] ls 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 [zk: localhost:2181(CONNECTED) 12]