Zookeeper简介
zookeeper:动物园管理员
■ 曾经是Hadoop子项目,现为顶级项目
■ zookeeper是协同服务
■ zookeeper为分布式应用提供服务
■ zookeeper支持Java和C语言
zookeeper服务基于内存,服务器之间相互感知,数据同步
v zookeeper为分布式应用提供的服务
■ 配置维护
■ 名字服务
■ 分布式同步
■ 组服务等
角色:
leader-领导 集群中只能有一个
follower-追随者 集群中可以有多个
v Zookeeper数据模型与层次命名空间
■ 树状结构
■ 通过/分隔开路径名
■ `每个路径代表一个节点Znode(zookeeper node)
zookeeper集群节点储存在内存中数据结构完全相同
在内存中的结构:/根文件夹,类似Linux的文件系统
v zookeeper节点专用名词:ZNode
■ 每个znode有自身信息、数据、长度、创建时间、修改时间
■ znode维护数据、ACL访问控制列表、时间戳等交换版本号数据结构,它通过对这些数据的管理来让缓存生效并且命名协调更新。每当znode中的数据更新后版本号将增加。
v znode读写操作
■ 读写数据原子性,读就读取所有数据,写入时完全覆盖。要么成功,要么失败,不存在一般成功一半失败的情况,znode的ACL存储用户操作权限。
■ 临时节点,和session相关,session结束,节点删除
zk中的选举机制叫 election 当leader宕机之后,集群节点会选出新的leader
Zookeeper安装
1. 下载安装JDK6+
参考我的另一篇笔记:CentOS6.4 64位系统安装JDK
2. 下载并解压zookeeper
首先我们需要在/data下面创建个zookeeper文件夹,把刚才下载下来的zookeeper-3.4.6.tar.gz软件包放到这里。用tar -zxvf来解压zookeeper的安装包。
|
进入到conf文件夹下面,将zoo_sample.cfg这个文件复制为zoo.cfg(必须是这个文件):
|
|
3. 配置环境变量
上面的操作都完事之后,我们需要配置一下环境变量,配置环境变量的命令如下:
|
4. 启动Zookeeper
进入到Zookeeper的bin目录下面运行zkServer.sh脚步启动zookeeper
|
通过 ./zkServer.sh status命令查看Zookeeper的运行状态:
|
也可以用 ps -ef | grep zookeeper来查看Zookeeper的运行状态:
[[email protected] bin]
# ps -ef | grep zookeeper
root 4057 1 0 05:47 pts/0 00:00:00 /usr/java/jdk1.7.0_79/bin/java
-Dzookeeper
.log.
dir
=.
-Dzookeeper
.root.logger=INFO,CONSOLE
-cp
/data/zookeeper/zookeeper-3.4.6/bin/../build/classes:/data/zookeeper/zookeeper-3.4.6/bin/../build/lib/*.jar:/data/zookeeper/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/data/zookeeper/zookeeper-3.4.6/bin/../lib/slf4j
-api
-1.6.1.jar:/data/zookeeper/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/data/zookeeper/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/data/zookeeper/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/data/zookeeper/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/data/zookeeper/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/data/zookeeper/zookeeper-3.4.6/bin/../conf:.:/usr/java/jdk1.7.0_79/lib/dt.jar:/usr/java/jdk1.7.0_79/lib/tools.jar:/usr/java/jdk1.7.0_79/jre/lib
-Dcom
.sun.management.jmxremote
-Dcom
.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /data/zookeeper/zookeeper-3.4.6/bin/../conf/zoo.cfg
root 4103 3626 0 05:48 pts/0 00:00:00 grep zookeeper
注意:如果发现Zookeeper不是在运行状态的话,可以通过cat zookeeper.out来查看启动过程中的出错日志。
|
zookeeper的服务端起来之后我们需要启动Zookeeper的客户端,启动命令如下:./zkCli.sh。启动结果如下所示:
[[email protected] bin]
# ./zkCli.sh
Connecting to localhost:2181
2016-11-04 05:52:00,446 [myid:] - INFO [main:[email protected]] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2016-11-04 05:52:00,461 [myid:] - INFO [main:[email protected]] - Client environment:host.name=CentOS1
2016-11-04 05:52:00,461 [myid:] - INFO [main:[email protected]] - Client environment:java.version=1.7.0_79
2016-11-04 05:52:00,463 [myid:] - INFO [main:[email protected]] - Client environment:java.vendor=Oracle Corporation
2016-11-04 05:52:00,463 [myid:] - INFO [main:[email protected]] - Client environment:java.home=/usr/java/jdk1.7.0_79/jre
2016-11-04 05:52:00,463 [myid:] - INFO [main:[email protected]] - Client environment:java.class.path=/data/zookeeper/zookeeper-3.4.6/bin/../build/classes:/data/zookeeper/zookeeper-3.4.6/bin/../build/lib/*.jar:/data/zookeeper/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/data/zookeeper/zookeeper-3.4.6/bin/../lib/slf4j
-api
-1.6.1.jar:/data/zookeeper/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/data/zookeeper/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/data/zookeeper/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/data/zookeeper/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/data/zookeeper/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/data/zookeeper/zookeeper-3.4.6/bin/../conf:.:/usr/java/jdk1.7.0_79/lib/dt.jar:/usr/java/jdk1.7.0_79/lib/tools.jar:/usr/java/jdk1.7.0_79/jre/lib
2016-11-04 05:52:00,463 [myid:] - INFO [main:[email protected]] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2016-11-04 05:52:00,463 [myid:] - INFO [main:[email protected]] - Client environment:java.io.tmpdir=/tmp
2016-11-04 05:52:00,463 [myid:] - INFO [main:[email protected]] - Client environment:java.compiler=<NA>
2016-11-04 05:52:00,463 [myid:] - INFO [main:[email protected]] - Client environment:os.name=Linux
2016-11-04 05:52:00,464 [myid:] - INFO [main:[email protected]] - Client environment:os.arch=amd64
2016-11-04 05:52:00,464 [myid:] - INFO [main:[email protected]] - Client environment:os.version=2.6.32-431.el6.x86_64
2016-11-04 05:52:00,464 [myid:] - INFO [main:[email protected]] - Client environment:user.name=root
2016-11-04 05:52:00,468 [myid:] - INFO [main:[email protected]] - Client environment:user.home=/root
2016-11-04 05:52:00,469 [myid:] - INFO [main:[email protected]] - Client environment:user.
dir
=/data/zookeeper/zookeeper-3.4.6/bin
2016-11-04 05:52:00,478 [myid:] - INFO [main:[email protected]] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain
$MyWatcher
@2765c17
Welcome to ZooKeeper!
2016-11-04 05:52:00,563 [myid:] - INFO [main
-SendThread
(localhost:2181):ClientCnxn
$SendThread
@975] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
2016-11-04 05:52:00,590 [myid:] - INFO [main
-SendThread
(localhost:2181):ClientCnxn
$SendThread
@852] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
JLine support is enabled
2016-11-04 05:52:00,676 [myid:] - INFO [main
-SendThread
(localhost:2181):ClientCnxn
$SendThread
@1235] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x1582c299ebe0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected
type
:None path:null
[zk: localhost:2181(CONNECTED) 0]