Basic functions and application scenarios of ZooKeeper

Ii. The overall operating mechanism of ZooKeeper

3. Data storage form

Zookeeper stores user data in KV format, but ZK is a bit special. Key: is represented in the form of a path, which means that there is a parent-child relationship between keys, for example

/ is the top-level key

A key created by a user can only be used as a child node under /. For example, a key: /aa can contain value data

You can also create a key: /bb

You can also build key: /aa/xx

In ZooKeeper, each data key is called a ZNode

To sum up, the data storage form in ZK is as follows:

4. Znode type

Znodes in ZooKeeper are of various types:

This class of nodes will persist in the ZK cluster even if the creator disconnects from the cluster

Once the creator is EPHEMERAL and disconnected from the cluster, ZK removes the node

SEQUENTIAL: For these nodes, ZK will automatically concatenate a SEQUENTIAL number, and the SEQUENTIAL number is incremental

Combination type:

PERSISTENT: PERSISTENT without serial number

EPHEMERAL: EPHEMERAL

PERSISTENT and SEQUENTIAL: PERSISTENT and SEQUENTIAL

EPHEMERAL and SEQUENTIAL: short and SEQUENTIAL

5. Cluster deployment of ZooKeeper

Upload the installation package to the cluster server. 2. Decompress the installation package. 3 tickTime=2000 initLimit=10 syncLimit=5 dataDir=/root/zkdata clientPort=2181 server.1=hdp20-01:2888:3888 Server. 2= hdP20-02:2888:3888 server.3= hdP20-03:2888:3888 *********************** Create a directory mkdir /root/zkdata for all three nodes. Generate myid file in working directory, but the contents should be the respective id: 1,2,3 on hdp20-01:echo1 > /root/zkdata/myid hdp20-02echo2 > /root/zkdata/myid hdp20-03echo3 > /root/zkdata/myid 4, SCP -r zookeeper-3.4.6/ hdp20-02$PWDSCP - r zookeeper - 3.4.6 / hdp20-03:$PWDZookeeper does not provide an automatic batch startup script. You need to manually start the ZooKeeper process on each node and run the bin/ zkserver. sh start command to start the ZooKeeper process. Run the bin/ zkserver. sh status command to check the zK status. If the role mode is leader or follower, the ZK is normal. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * want to use zkClient link zookeeper, first perform the following commands, connected to the zookeeper server command: Sh -server localhost:2181 or: zkcli. sh start Script One-click startup:#! /bin/bash

for host in hadoop2 hadoop3 hadoop4
do
echo "${host}:The ${1}ing....."
ssh $host "source /etc/profile; / usr/local/they/they are - 3.4.10 / bin/zkServer. ShThe $1"
done

Copy the code

Vi. Data Management Functions:

Create node: create /aaa ‘PPPPP’

View the child nodes under the node: LS/AAA

Get node value: get/AAA

Value: set /aaa ‘MMMMM’

Deleting a node: RMR/AAA

Vii. Data monitoring function:

ls /aaa watch

While viewing the child node of/AAA, a listener is registered to listen for “node child change events”

get /aaa watch

When the value of /aaa is obtained, a listener is registered to listen for “node value change events”

Note: Registered listeners are deactivated after normally receiving a listening event

Zookeeper API

9. Cluster deployment roles

leader

  • 1. Restore data
  • Maintain a heartbeat with Learner
  • Receive the Learner write request
  • Receives read/write requests from the Client

Follower

  • 1. Send a request to the leader
  • The Leader message is received and processed
  • Receives the request from the Client. If it is a write request, the request is sent to the Leader for voting.

10. Zookeeper features

  • 1. Simplicity (streamlined file system, simple file operations: sorting and notification)
  • 2. Easy to express
  • 3. High availability
  • 4. Loosely coupled interaction
  • 5. Rich apis

11. Zookeeper Application Scenarios

1. Configuration management

2. Load balancing

3. Distributed exclusive lock

4. Distributed read/write lock

5. Distributed queue -FIFO

6. Distributed queue -SyncQueue

7. Cluster management

One-click Start Zookeeper and one-click check Zookeeper status#! /bin/bash
ZOOKEEPER_HOME=/usr/local/zookeeper

if [ $ZOOKEEPER_HOME! ="" ]; then
    ZOOCFG=$ZOOKEEPER_HOME/conf/zoo.cfg
    SLAVES=$(cat $ZOOCFG | sed '/^server/! d; s/^.*=//; s/:.*$//g; /^$/d')
    for s in $SLAVES
     do
      echo "INFO:starting zookeeper on ${s}"
      ssh $s "source /etc/profile;$ZOOKEEPER_HOME/bin/zkServer.sh The $1"
      if[$?! = 0];then
                echo "Can not starting zookeeper server on host $s"
                exit 1
      fi
     done
fi
Copy the code

Zookeeper implements HA

Single point of failure