系统需求
- 客户端:Java客户端库,用户连接ZooKeeper应用程序使用。
- 服务器端:运行在ZooKeeper节点上的Java服务端。
- 本地客户端:C客户端接口,和Java客户端类似,用于连接ZooKeeper的应用程序使用。
- 普通发布版:多个可选的附加组件。
系统支持度:
多服务器集群设置
1、安装JDK
查看 >>
2、设置JAVA堆内存大小。避免使用交换空间(swap),会极大拉低ZooKeeper的性能。通过压测,确定设定内存大小,建议最大4G。
3、下载安装ZooKeeper。
下载地址:http://mirror.bit.edu.cn/apache/zookeeper/ 这里选择下载3.4.10版本。http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
4、创建配置文件。
tickTime=2000
dataDir=/var/lib/zookeeper/
clientPort=2181
initLimit=5
syncLimit=2
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
每台机器都是机器的一部分,每台机器配置一行。
server.id=host:port:port
5、创建机器ID文件
每台机器都对应一个myid文件,文件内容为相应的数字ID。存放在对应dataDir目录下。
$ echo "1" > /tmp/data/myid #对应的dataDir位置
6、启动ZooKeeper
$ java -cp zookeeper.jar:lib/slf4j-api-1.6.1.jar:lib/slf4j-log4j12-1.6.1.jar:lib/log4j-1.2.15.jar:conf \ org.apache.zookeeper.server.quorum.QuorumPeerMain zoo.cfg
(java -cp等同于:classpath)
使用 bin/zkServer.sh启动实例
bin/zkServer.sh start
7、测试部署
$ bin/zkCli.sh -server 127.0.0.1:2181
数据模型
Znode,节点。绝对的,/zookeeper用于保存管理信息,比如关键配额信息。
艰巨文件和目录两种特点。既像文件一样维护着数据,原信息、ACL、时间戳等数据结构,又像目录一样可以作为路径标识的一部分。
Znode由3部分组成:
- stat:状态信息,描述该Znode的版本,权限等信息
- data:与该Znode关联的数据
- children:该Znode下的子节点
管理调度数据。每个Znode数据至多1M。
每一个节点都拥有自己的ACL。
节点类型:
- 临时节点
- 生命周期依赖于创建时的会话。Session结束,自动删除。
- 对所有的客户端课件
- 不允许拥有子节点
- 永久节点
- 不依赖于会话,显示地执行删除操作
顺序节点:
路径结尾添加一个递增的计数。对于此节点的父节点唯一。格式为“%10d”(10位数字,没有数值的位数用0补充)。当值大于2^32-1时,计数器溢出。
观察:
客户端可在节点上设置watch,称为监视器。当节点发生改变时(Znode的增、删、改)将会触发对应的操作。当watch被触发时,ZooKeeper将会向客户端发送且仅发送一条通知。
ZooKeeper中的时间
有多种记录时间的形式
1、Zxid
致使节点状态改变的每一个操作都将使节点接收到一个Zxid格式的时间戳。并且该时间戳全局有序。
节点改变,产生唯一Zxid。每个节点维护着3个Zxid值。
- cZxid:创建时间
- mZxid:修改时间
- pZxid:
实现中Zxid是一个64位的数字。高32位是epoch用来标识leader关系是否改变,每次一个leader被选出,都会有一个新的epoch。低32位是递增计数。
2、版本号
对节点的每一个操作,都将致使这个节点的版本号增加。
- version:节点数据版本号
- cversion:子节点版本号
- aversion:节点所拥有的ACL版本号
3、节点属性
- czxid
- mzxid
- ctime
- mtime
- version
- cversion
- aversion
- ephemeralOwner(临时节点,值为节点拥有者的SessionID;否则为0)
- dataLength
- numChildren
- pzxid(最新修改的zxid)
操作
9个基本操作
create:创建Znode(父Znode必须存在)
delete:删除Znode(Znode没有子节点)
exists:是否存在,并获取他的元数据
getACL/setACL:
getChildren:获取所有子节点列表
getData/setData:
sync:使客户端的Znode视图与ZooKeeper同步
更新操作是有限制的。delete或setData必须明确要更新的Znode的版本号,可以调用exists找到。
更新是非阻塞式的。
Watch触发器
可以为所有的读操作设置watch,(exists、getChildren、getData)。一次性的触发器,异步发送,一致性保证。
watch类型:
- 数据watch:getData和exists负责设置数据watch
- 孩子watch:getChildren负责
通过返回的数据来设置不同的watch:
- getData和exists:返回关于节点的数据信息
- getChildren:返回子节点列表
因此:
- setData触发数据watch
- create触发数据watch和子节点watch
- delete触发数据watch和子节点watch
注意:
watch实际上要处理两类事件:
- 连接状态事件(type=None, path=null)
- 不需要注册,也不需要连续触发
- 节点事件
- one time trigger
上面2类事件都在Watch中处理,即重载process(Event event)
节点事件的触发,通过exists,getData或getChildren来处理这类函数,有双重作用:
- 注册触发事件
- 函数本身的功能
函数本身的功能又可以异步的回调函数来实现,重载processResult()过程中处理函数本身的功能。
ACL Permissions
- CREATE
- READ
- WRITE
- DELETE
- ADMIN
Builtin ACL Schemes
- world
- auth
- digest
- ip
ZooKeeper C client API
- const int ZOO_PERM_READ; //can read node’s value and list its children
- const int ZOO_PERM_WRITE;// can set the node’s value
- const int ZOO_PERM_CREATE; //can create children
- const int ZOO_PERM_DELETE;// can delete children
- const int ZOO_PERM_ADMIN; //can execute set_acl()
- const int ZOO_PERM_ALL;// all of the above flags OR’d together
标准ACL IDs:
- struct Id ZOO_ANYONE_ID_UNSAFE; //(‘world’,’anyone’)
- struct Id ZOO_AUTH_IDS;// (‘auth’,’’)
3个标准ACL:
- struct ACL_vector ZOO_OPEN_ACL_UNSAFE; //(ZOO_PERM_ALL,ZOO_ANYONE_ID_UNSAFE)
- struct ACL_vector ZOO_READ_ACL_UNSAFE;// (ZOO_PERM_READ, ZOO_ANYONE_ID_UNSAFE)
- struct ACL_vector ZOO_CREATOR_ALL_ACL; //(ZOO_PERM_ALL,ZOO_AUTH_IDS)
The following ZooKeeper operations deal with ACLs:
-
int zoo_add_auth (zhandle_t *zh,const char* scheme,const char* cert, int certLen, void_completion_t completion, const void *data);
The application uses the zoo_add_auth function to authenticate itself to the server. The function can be called multiple times if the application wants to authenticate using different schemes and/or identities.
-
int zoo_create (zhandle_t *zh, const char *path, const char *value,int valuelen, const struct ACL_vector *acl, int flags,char *realpath, int max_realpath_len);
zoo_create(...) operation creates a new node. The acl parameter is a list of ACLs associated with the node. The parent node must have the CREATE permission bit set.
-
int zoo_get_acl (zhandle_t *zh, const char *path,struct ACL_vector *acl, struct Stat *stat);
This operation returns a node’s ACL info.
-
int zoo_set_acl (zhandle_t *zh, const char *path, int version,const struct ACL_vector *acl);
This function replaces node’s ACL list with a new one. The node must have the ADMIN permission set.