Zookeeper, a distributed service framework — manage data in a distributed environment

table of Contents

Zookeeper installation and configuration details

Stand-alone mode

Cluster mode

Data model

How to use Zookeeper

List of common interfaces

Basic operation

Typical application scenarios of ZooKeeper

Unified naming service (Name Service)

Configuration Management

Cluster Management (Group Membership)

Shared locks (Locks)

Queue management

Concluding remarks


Zookeeper installation and configuration details

The Zookeeper introduced in this article is based on the stable version of 3.2.2. The latest version can be obtained through the official website  http://hadoop.apache.org/zookeeper/ . The installation of Zookeeper is very simple. The following will be from stand-alone mode and cluster mode Two aspects introduce the installation and configuration of Zookeeper.

Stand-alone mode

The stand-alone installation is very simple, just get the Zookeeper compressed package and unzip it to a directory such as: /home/zookeeper-3.2.2, the startup script of Zookeeper is in the bin directory, and the startup script under Linux is zkServer.sh. 3.2.2 This version of Zookeeper does not provide a startup script under windows, so if you want to start Zookeeper under windows, you have to write one manually, as shown in Listing 1:

Listing 1. Zookeeper startup script under Windows

1

2

3

4

5

6

7

8

9

10

11

12

13

setlocal

set ZOOCFGDIR=%~dp0%..\conf

set ZOO_LOG_DIR=%~dp0%..

set ZOO_LOG4J_PROP=INFO,CONSOLE

set CLASSPATH=%ZOOCFGDIR%

 

set CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH%

set CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH%

set ZOOCFG=%ZOOCFGDIR%\zoo.cfg

set ZOOMAIN=org.apache.zookeeper.server.ZooKeeperServerMain

java "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%"

-cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

Before you execute the startup script, there are a few basic configuration items that need to be configured. The Zookeeper configuration file is in the conf directory. There are zoo_sample.cfg and log4j.properties in this directory. All you need to do is to rename zoo_sample.cfg It is zoo.cfg, because Zookeeper will find this file as the default configuration file when it starts. The following is a detailed introduction to the meaning of each configuration item in this configuration file.

1

2

3

tickTime=2000

dataDir=D:/devtools/zookeeper-3.2.2/build

clientPort=2181

  • tickTime: This time is used as the time interval for maintaining heartbeats between Zookeeper servers or between client and server, that is, a heartbeat will be sent every tickTime.
  • dataDir: as the name implies
  • It is the directory where Zookeeper saves data. By default, Zookeeper saves the log files for writing data in this directory.
  • clientPort: This port is the port for the client to connect to the Zookeeper server. Zookeeper will listen to this port and accept the client's access request.

After these configuration items are configured, you can start Zookeeper now. After starting, check whether Zookeeper is already serving. You can use the netstat-ano command to check whether the clientPort port number you configured is listening to the service.

 

Cluster mode

Zookeeper can not only provide services on a single machine, but also support multiple machines to form a cluster to provide services. In fact, Zookeeper also supports another pseudo-cluster method, that is, multiple Zookeeper instances can be run on one physical machine. The installation and configuration of the cluster mode will be introduced below.

The installation and configuration of Zookeeper's cluster mode is not very complicated, all you have to do is to add a few configuration items. In addition to the above three configuration items, the cluster mode needs to add the following configuration items:

1

2

3

4

initLimit=5

syncLimit=2

server.1=192.168.211.1:2888:3888

server.2=192.168.211.2:2888:3888

  • initLimit: This configuration item is used to configure Zookeeper to accept clients (the client mentioned here is not the client of the user connecting to the Zookeeper server, but the follower server connected to the Leader in the Zookeeper server cluster). The number of heartbeat intervals. When the Zookeeper server has not received the return message from the client after the length of 10 heartbeats (ie tickTime) has passed, it indicates that the client connection failed. The total length of time is 5*2000=10 seconds
  • syncLimit: This configuration item identifies the length of the message sent between Leader and Follower, request and response time, the longest cannot exceed the length of tickTime, the total length of time is 2*2000=4 seconds
  • server.A=B: C: D: where A is a number, which indicates which server is this number; B is the ip address of this server; C is the port through which this server exchanges information with the Leader server in the cluster; D It means that in case the Leader server in the cluster hangs up, a port is needed to re-elect and select a new leader, and this port is the port used to communicate with each other when the election is performed. If it is a pseudo-cluster configuration method, since B is the same, the communication port numbers of different Zookeeper instances cannot be the same, so different port numbers should be assigned to them.

In addition to modifying the zoo.cfg configuration file, a file myid must be configured in cluster mode. This file is in the dataDir directory. There is a data in this file that is the value of A. When Zookeeper starts, it will read this file and get the file inside. The data is compared with the configuration information in zoo.cfg to determine which server it is.

Data model

Zookeeper will maintain a hierarchical data structure, which is very similar to a standard file system, as shown in Figure 1:

Figure 1 Zookeeper data structure

  Ã¾ 1 Zookeeper æ ° æ®ç »æ

The data structure of Zookeeper has the following characteristics:

  1. Each subdirectory item such as NameService is called a znode, and this znode is uniquely identified by the path where it is located. For example, Server1 is identified by the znode as /NameService/Server1
  2. A znode can have sub-node directories, and each znode can store data. Note that EPHEMERAL type directory nodes cannot have sub-node directories
  3. There are versions of znode, and the data stored in each znode can have multiple versions, that is, multiple copies of data can be stored in one access path
  4. The znode can be a temporary node. Once the client that created the znode loses contact with the server, the znode will also be deleted automatically. Zookeeper's client and server communication uses a long connection, and each client and server maintain a connection through a heartbeat. The connection state is called session. If the znode is a temporary node, the session becomes invalid and the znode is deleted.
  5. The directory name of znode can be automatically numbered. If App1 already exists, if it is created again, it will be automatically named App2
  6. The znode can be monitored, including the modification of the data stored in this directory node, the change of the sub-node directory, etc. Once the change is made, the client that sets the monitoring can be notified. This is the core feature of Zookeeper. Many functions of Zookeeper are based on this feature. Yes, there will be examples introduced in typical application scenarios later

How to use Zookeeper

As a distributed service framework, Zookeeper is mainly used to solve the consistency problem of application systems in distributed clusters. It can provide data storage based on a directory node tree similar to the file system, but Zookeeper is not used to store data specifically Yes, its function is mainly used to maintain and monitor the status changes of your stored data. By monitoring the changes of these data states, data-based cluster management can be achieved. Some typical problems that Zookeeper can solve will be described in detail later. Here, I will first introduce the operation interface and simple use examples of Zookeeper.

List of common interfaces

The client can connect to the Zookeeper server by creating an instance of org.apache.zookeeper. ZooKeeper and then calling the interface provided by this class to interact with the server.

As mentioned earlier, ZooKeeper is mainly used to maintain and monitor the state of the data stored in a directory node tree. All we can operate ZooKeeper are similar to the operation of the directory node tree, such as creating a directory node and setting data for a directory node. , Get all the sub-directory nodes of a certain directory node, set permissions for a certain directory node and monitor the status changes of this directory node.

These interfaces are shown in the following table:

Table 1 org.apache.zookeeper. ZooKeeper method list

Method name Method function description
Stringcreate(String path, byte[] data, List<ACL> acl, CreateMode createMode) Create a given directory node path, and set data for it. CreateMode  identifies four types of directory nodes, namely PERSISTENT: persistent directory node, the data stored in this directory node will not be lost; PERSISTENT_SEQUENTIAL: sequence automatically numbered Directory node, this kind of directory node will automatically increase by 1 according to the number of nodes that currently exist, and then return to the client the name of the directory node that has been successfully created; EPHEMERAL: temporary directory node, once the node is created, the client and server ports are also Is the session timeout, this kind of node will be automatically deleted; EPHEMERAL_SEQUENTIAL: Temporary automatic numbering of nodes
Statexists(String path, boolean watch) Determine whether a path exists, and set whether to monitor this directory node. The watcher here is the watcher specified when the ZooKeeper instance is created. The exists method also has an overload method that can specify a specific watcher
Statexists(String path, Watcher watcher) 重载方法,这里给某个目录节点设置特定的 watcher,Watcher 在 ZooKeeper 是一个核心功能,Watcher 可以监控目录节点的数据变化以及子目录的变化,一旦这些状态发生变化,服务器就会通知所有设置在这个目录节点上的 Watcher,从而每个客户端都很快知道它所关注的目录节点的状态发生变化,而做出相应的反应
void delete(String path, int version) 删除 path 对应的目录节点,version 为 -1 可以匹配任何版本,也就删除了这个目录节点所有数据
List<StringgetChildren(String path, boolean watch) 获取指定 path 下的所有子目录节点,同样 getChildren方法也有一个重载方法可以设置特定的 watcher 监控子节点的状态
StatsetData(String path, byte[] data, int version) 给 path 设置数据,可以指定这个数据的版本号,如果 version 为 -1 怎可以匹配任何版本
byte[] getData(String path, boolean watch, Stat stat) 获取这个 path 对应的目录节点存储的数据,数据的版本等信息可以通过 stat 来指定,同时还可以设置是否监控这个目录节点数据的状态
void addAuthInfo(String scheme, byte[] auth) 客户端将自己的授权信息提交给服务器,服务器将根据这个授权信息验证客户端的访问权限。
StatsetACL(String path, List<ACL> acl, int version) 给某个目录节点重新设置访问权限,需要注意的是 Zookeeper 中的目录节点权限不具有传递性,父目录节点的权限不能传递给子目录节点。目录节点 ACL 由两部分组成:perms 和 id。
Perms 有 ALL、READ、WRITE、CREATE、DELETE、ADMIN 几种 
而 id 标识了访问目录节点的身份列表,默认情况下有以下两种:
ANYONE_ID_UNSAFE = new Id("world", "anyone") 和 AUTH_IDS = new Id("auth", "") 分别表示任何人都可以访问和创建者拥有访问权限。
List<ACLgetACL(String path, Stat stat) 获取某个目录节点的访问权限列表

除了以上这些上表中列出的方法之外还有一些重载方法,如都提供了一个回调类的重载方法以及可以设置特定 Watcher 的重载方法,具体的方法可以参考 org.apache.zookeeper. ZooKeeper 类的 API 说明。

基本操作

下面给出基本的操作 ZooKeeper 的示例代码,这样你就能对 ZooKeeper 有直观的认识了。下面的清单包括了创建与 ZooKeeper 服务器的连接以及最基本的数据操作:

清单 2. ZooKeeper 基本的操作示例

// 创建一个与服务器的连接
ZooKeeper zk = new ZooKeeper("localhost:" + CLIENT_PORT, 
       ClientBase.CONNECTION_TIMEOUT, new Watcher() { 
           // 监控所有被触发的事件
           public void process(WatchedEvent event) { 
               System.out.println("已经触发了" + event.getType() + "事件!"); 
           } 
       }); 
// 创建一个目录节点
zk.create("/testRootPath", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE,
  CreateMode.PERSISTENT); 
// 创建一个子目录节点
zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(),
  Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); 
System.out.println(new String(zk.getData("/testRootPath",false,null))); 
// 取出子目录节点列表
System.out.println(zk.getChildren("/testRootPath",true)); 
// 修改子目录节点数据
zk.setData("/testRootPath/testChildPathOne","modifyChildDataOne".getBytes(),-1); 
System.out.println("目录节点状态:["+zk.exists("/testRootPath",true)+"]"); 
// 创建另外一个子目录节点
zk.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(), 
  Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); 
System.out.println(new String(zk.getData("/testRootPath/testChildPathTwo",true,null))); 
// 删除子目录节点
zk.delete("/testRootPath/testChildPathTwo",-1); 
zk.delete("/testRootPath/testChildPathOne",-1); 
// 删除父目录节点
zk.delete("/testRootPath",-1); 
// 关闭连接
zk.close();

输出的结果如下:

已经触发了 None 事件!
 testRootData 
 [testChildPathOne] 
目录节点状态:[5,5,1281804532336,1281804532336,0,1,0,0,12,1,6] 
已经触发了 NodeChildrenChanged 事件!
 testChildDataTwo 
已经触发了 NodeDeleted 事件!
已经触发了 NodeDeleted 事件!

当对目录节点监控状态打开时,一旦目录节点的状态发生变化,Watcher 对象的 process 方法就会被调用。

ZooKeeper 典型的应用场景

Zookeeper 从设计模式角度来看,是一个基于观察者模式设计的分布式服务管理框架,它负责存储和管理大家都关心的数据,然后接受观察者的注册,一旦这些数据的状态发生变化,Zookeeper 就将负责通知已经在 Zookeeper 上注册的那些观察者做出相应的反应,从而实现集群中类似 Master/Slave 管理模式,关于 Zookeeper 的详细架构等内部细节可以阅读 Zookeeper 的源码

下面详细介绍这些典型的应用场景,也就是 Zookeeper 到底能帮我们解决那些问题?下面将给出答案。

统一命名服务(Name Service)

分布式应用中,通常需要有一套完整的命名规则,既能够产生唯一的名称又便于人识别和记住,通常情况下用树形的名称结构是一个理想的选择,树形的名称结构是一个有层次的目录结构,既对人友好又不会重复。说到这里你可能想到了 JNDI,没错 Zookeeper 的 Name Service 与 JNDI 能够完成的功能是差不多的,它们都是将有层次的目录结构关联到一定资源上,但是 Zookeeper 的 Name Service 更加是广泛意义上的关联,也许你并不需要将名称关联到特定资源上,你可能只需要一个不会重复名称,就像数据库中产生一个唯一的数字主键一样。

Name Service 已经是 Zookeeper 内置的功能,你只要调用 Zookeeper 的 API 就能实现。如调用 create 接口就可以很容易创建一个目录节点。

配置管理(Configuration Management)

配置的管理在分布式应用环境中很常见,例如同一个应用系统需要多台 PC Server 运行,但是它们运行的应用系统的某些配置项是相同的,如果要修改这些相同的配置项,那么就必须同时修改每台运行这个应用系统的 PC Server,这样非常麻烦而且容易出错。

像这样的配置信息完全可以交给 Zookeeper 来管理,将配置信息保存在 Zookeeper 的某个目录节点中,然后将所有需要修改的应用机器监控配置信息的状态,一旦配置信息发生变化,每台应用机器就会收到 Zookeeper 的通知,然后从 Zookeeper 获取新的配置信息应用到系统中。

图 2. 配置管理结构图

å¾ 2. é置管çç »æå¾

集群管理(Group Membership)

Zookeeper 能够很容易的实现集群管理的功能,如有多台 Server 组成一个服务集群,那么必须要一个“总管”知道当前集群中每台机器的服务状态,一旦有机器不能提供服务,集群中其它集群必须知道,从而做出调整重新分配服务策略。同样当增加集群的服务能力时,就会增加一台或多台 Server,同样也必须让“总管”知道。

Zookeeper 不仅能够帮你维护当前的集群中机器的服务状态,而且能够帮你选出一个“总管”,让这个总管来管理集群,这就是 Zookeeper 的另一个功能 Leader Election。

它们的实现方式都是在 Zookeeper 上创建一个 EPHEMERAL 类型的目录节点,然后每个 Server 在它们创建目录节点的父目录节点上调用 getChildren(String path, boolean watch) 方法并设置 watch 为 true,由于是 EPHEMERAL 目录节点,当创建它的 Server 死去,这个目录节点也随之被删除,所以 Children 将会变化,这时 getChildren上的 Watch 将会被调用,所以其它 Server 就知道已经有某台 Server 死去了。新增 Server 也是同样的原理。

Zookeeper 如何实现 Leader Election,也就是选出一个 Master Server。和前面的一样每台 Server 创建一个 EPHEMERAL 目录节点,不同的是它还是一个 SEQUENTIAL 目录节点,所以它是个 EPHEMERAL_SEQUENTIAL 目录节点。之所以它是 EPHEMERAL_SEQUENTIAL 目录节点,是因为我们可以给每台 Server 编号,我们可以选择当前是最小编号的 Server 为 Master,假如这个最小编号的 Server 死去,由于是 EPHEMERAL 节点,死去的 Server 对应的节点也被删除,所以当前的节点列表中又出现一个最小编号的节点,我们就选择这个节点为当前 Master。这样就实现了动态选择 Master,避免了传统意义上单 Master 容易出现单点故障的问题。

图 3. 集群管理结构图

å¾ 3. é群管çç »æå¾

这部分的示例代码如下,完整的代码请看附件:

清单 3. Leader Election 关键代码

void findLeader() throws InterruptedException { 
       byte[] leader = null; 
       try { 
           leader = zk.getData(root + "/leader", true, null); 
       } catch (Exception e) { 
           logger.error(e); 
       } 
       if (leader != null) { 
           following(); 
       } else { 
           String newLeader = null; 
           try { 
               byte[] localhost = InetAddress.getLocalHost().getAddress(); 
               newLeader = zk.create(root + "/leader", localhost, 
               ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); 
           } catch (Exception e) { 
               logger.error(e); 
           } 
           if (newLeader != null) { 
               leading(); 
           } else { 
               mutex.wait(); 
           } 
       } 
   }

共享锁(Locks)

共享锁在同一个进程中很容易实现,但是在跨进程或者在不同 Server 之间就不好实现了。Zookeeper 却很容易实现这个功能,实现方式也是需要获得锁的 Server 创建一个 EPHEMERAL_SEQUENTIAL 目录节点,然后调用 getChildren方法获取当前的目录节点列表中最小的目录节点是不是就是自己创建的目录节点,如果正是自己创建的,那么它就获得了这个锁,如果不是那么它就调用 exists(String path, boolean watch) 方法并监控 Zookeeper 上目录节点列表的变化,一直到自己创建的节点是列表中最小编号的目录节点,从而获得锁,释放锁很简单,只要删除前面它自己所创建的目录节点就行了。

图 4. Zookeeper 实现 Locks 的流程图

å¾ 4. Zookeeper å®ç ° Locks çæµç¨å¾

同步锁的实现代码如下,完整的代码请看附件:

清单 4. 同步锁的关键代码

void getLock() throws KeeperException, InterruptedException{ 
       List<String> list = zk.getChildren(root, false); 
       String[] nodes = list.toArray(new String[list.size()]); 
       Arrays.sort(nodes); 
       if(myZnode.equals(root+"/"+nodes[0])){ 
           doAction(); 
       } 
       else{ 
           waitForLock(nodes[0]); 
       } 
   } 
   void waitForLock(String lower) throws InterruptedException, KeeperException {
       Stat stat = zk.exists(root + "/" + lower,true); 
       if(stat != null){ 
           mutex.wait(); 
       } 
       else{ 
           getLock(); 
       } 
   }

队列管理

Zookeeper 可以处理两种类型的队列:

  1. 当一个队列的成员都聚齐时,这个队列才可用,否则一直等待所有成员到达,这种是同步队列。
  2. 队列按照 FIFO 方式进行入队和出队操作,例如实现生产者和消费者模型。

同步队列用 Zookeeper 实现的实现思路如下:

创建一个父目录 /synchronizing,每个成员都监控标志(Set Watch)位目录 /synchronizing/start 是否存在,然后每个成员都加入这个队列,加入队列的方式就是创建 /synchronizing/member_i 的临时目录节点,然后每个成员获取 / synchronizing 目录的所有目录节点,也就是 member_i。判断 i 的值是否已经是成员的个数,如果小于成员个数等待 /synchronizing/start 的出现,如果已经相等就创建 /synchronizing/start。

用下面的流程图更容易理解:

图 5. 同步队列流程图

å¾ 5. åæ­¥éåæµç¨å¾

同步队列的关键代码如下,完整的代码请看附件:

清单 5. 同步队列

void addQueue() throws KeeperException, InterruptedException{ 
       zk.exists(root + "/start",true); 
       zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE, 
       CreateMode.EPHEMERAL_SEQUENTIAL); 
       synchronized (mutex) { 
           List<String> list = zk.getChildren(root, false); 
           if (list.size() < size) { 
               mutex.wait(); 
           } else { 
               zk.create(root + "/start", new byte[0], Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT); 
           } 
       } 
}

当队列没满是进入 wait(),然后会一直等待 Watch 的通知,Watch 的代码如下:

1

2

3

4

5

6

7

8

public void process(WatchedEvent event) {

       if(event.getPath().equals(root + "/start") &&

        event.getType() == Event.EventType.NodeCreated){

           System.out.println("得到通知");

           super.process(event);

           doAction();

       }

   }

FIFO 队列用 Zookeeper 实现思路如下:

实现的思路也非常简单,就是在特定的目录下创建 SEQUENTIAL 类型的子目录 /queue_i,这样就能保证所有成员加入队列时都是有编号的,出队列时通过 getChildren( ) 方法可以返回当前所有的队列中的元素,然后消费其中最小的一个,这样就能保证 FIFO。

下面是生产者和消费者这种队列形式的示例代码,完整的代码请看附件:

清单 6. 生产者代码

boolean produce(int i) throws KeeperException, InterruptedException{ 
       ByteBuffer b = ByteBuffer.allocate(4); 
       byte[] value; 
       b.putInt(i); 
       value = b.array(); 
       zk.create(root + "/element", value, ZooDefs.Ids.OPEN_ACL_UNSAFE, 
                   CreateMode.PERSISTENT_SEQUENTIAL); 
       return true; 
   }

 清单 7. 消费者代码

int consume() throws KeeperException, InterruptedException{ 
       int retvalue = -1; 
       Stat stat = null; 
       while (true) { 
           synchronized (mutex) { 
               List<String> list = zk.getChildren(root, true); 
               if (list.size() == 0) { 
                   mutex.wait(); 
               } else { 
                   Integer min = new Integer(list.get(0).substring(7)); 
                   for(String s : list){ 
                       Integer tempValue = new Integer(s.substring(7)); 
                       if(tempValue < min) min = tempValue; 
                   } 
                   byte[] b = zk.getData(root + "/element" + min,false, stat); 
                   zk.delete(root + "/element" + min, 0); 
                   ByteBuffer buffer = ByteBuffer.wrap(b); 
                   retvalue = buffer.getInt(); 
                   return retvalue; 
               } 
           } 
       } 
}

结束语

Zookeeper 作为 Hadoop 项目中的一个子项目,是 Hadoop 集群管理的一个必不可少的模块,它主要用来控制集群中的数据,如它管理 Hadoop 集群中的 NameNode,还有 Hbase 中 Master Election、Server 之间状态同步等。

This article introduces the basic knowledge of Zookeeper and introduces several typical application scenarios. These are the basic functions of Zookeeper. The most important thing is that Zookeeper provides a good distributed cluster management mechanism, which is based on the hierarchical directory tree data structure and effectively manages the nodes in the tree. In this way, a variety of distributed data management models can be designed, not just limited to the several common application scenarios mentioned above.

 

 

Original link: https://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/index.html  Xu Lingbo

Guess you like

Origin blog.csdn.net/weixin_44961794/article/details/100055900