Zookeeper the use of distributed coordination services A

Source: Internet
Author: User
Tags zookeeper

Zookeeper is a high-performance, distributed application Coordination Service.


Services Provided:

1. Management of cluster members (group membership)

2. Distributed type Lock (locking)

3, select the main (Leader election)

4. Sync (synchronization)

5, Publish/subscribe (Publisher/subsriber)


first, the data model

Each node in a hierarchical attribute structure is called Znode each znode has data (byte[] type), or it can have child node paths: Slash segmentation (/zoo/duck); no relative path through data structure STAT class storage data change, ACL change and timestamp When the data changes, the version number increments to read and write the data in the Znode


second, the application scene

1. Data Publish/Subscribe

Publishers publish data to one or a series of ZK's nodes, subscribers subscribe to data, and when data changes, they can be notified of changes in the data in a timely manner.


2. Load balance

The essence is to take advantage of the Zookeeper configuration management function, the steps are:
The service provider registers the mapping of its domain name and IP port into ZK
The service consumer obtains the corresponding IP and the port through the domain name from ZK, this IP and the port has the multiple, only obtains one
When the service this when timely, for the domain name and IP for will reduce a mapping


3. Naming Service

In distributed systems, naming services (name Service) is an important application scenario, and the ZK naming service provides resource positioning and is essentially managed and searched through ZK's centralized configuration


4. Distributed Coordination/Notification

Implementation of the notification mechanism through watcher

Distributed lock

Distributed transactions


5. Cluster Management

Number of machines in the current cluster

Runtime state of machines in a cluster

Up and down operations of nodes in a cluster

Unified configuration of cluster nodes


6. Master Election

Temporary node

Sequential nodes

7. Distributed Type Lock

Exclusive lock

Shared locks

8. Distributed queue

FIFO mechanism


Iii. The mechanism of zookeeper

1. Cluster role

Leader: Providing read-write services to clients

Follow: Provide read service, all write service need to hand over to leader role, participate in the election

Observe: Provide read services, not participate in the electoral process, is generally to enhance the ZK cluster read request concurrency Ability


2, Sessions (session)

The connection between ZK's client and ZK server

Maintain client connection survival through heartbeat detection

Accept Watch event notifications from the service side

You can set the timeout time


3, Data node (znode)

A data node in the ZK tree structure that is used to store data

Persistent node: Once created, no person has been stored on the ZK until the delete action is invoked voluntarily

Temporary node: A reply binding to the client, once the client reply disappears, the temporary node created by the client is removed

Sequential Znode: When you create a node, you automatically append an integer number after the section name if you set the property sequential


4, version

Version: versions of current Znode

Gversion: Version of the child node of the current Znode

Aversion: ACL (access control) version of the current Znode


5, Watcher

Acting on the Znode node

Multiple event notifications: Data updates, child node status, etc.


6. ACL (access control Lists) permission controls

Create: Creating Permissions for child nodes

READ: Get Permissions for node data and child node lists

WRITE: Permissions to update node data

Delete: Remove permissions for child nodes

ADMIN: Set permissions on Node ACLs

Where: Create and delete are permissions control for child nodes


iv. Configuration deployment of zookeeper (stand-alone configuration)

1. Configure Environment variables

Configuring ZK's environment variables in the/etc/profile file

Export java_home=/usr/andy/jdk/jdk1.7.0_79
export zookeeper_home=/usr/andy/zookeeper/zookeeper-3.4.8
Export path= $JAVA _home/bin: $JAVA _home/jre/bin: $ZOOKEEPER _home/bin: $PATH
export classpath=.: $JAVA _home/lib:$ Java_home/lib/tools.jar: $ZOOKEEPER _home/lib

Effective configuration: Source/etc/profile

2, zookeeper Configuration

# The number of milliseconds of each tick ticktime=2000 # The number of ticks that initial # synchronization phase CA N Take initlimit=10 # The number of ticks that can pass between # Sending a request and getting a acknowledgement
Mit=5 # The directory where the snapshot is stored.
# do not use/tmp for storage,/tmp here is just # example sakes. Datadir=/usr/andy/zookeeper/zookeeper-3.4.8/data Datalogdir=/usr/andy/zookeeper/zookeeper-3.4.8/logs # The port at
which the clients would connect clientport=2181 # The maximum number of client connections. 
# Increase this if your need to handle more clients #maxClientCnxns =60 # is 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 da Tadir #autopurge. snapretaincount=3 # Purge Task interval in hours # Set to ' 0 ' to disable Auto Purge feature #autopurge. Pu
Rgeinterval=1 

1, will be under the Conf zoo_sample.cfg modification configuration for Zoo.cfg

2, Ticktime: The default 2000ms, as a basic unit, with its configuration to represent the system's internal time interval configurations, such as:

2*ticktime is the timeout for client reply

1*ticktime is the heartbeat time between the client and ZK server side

DataDir: Used to configure the directory where the snapshot files are stored, and if no DATALOGDIR is configured, the transaction log is also stored in the directory "need to configure", typically creating the data folder

Datalogdir: The transaction log is the parent of the path, typically creating the logs folder under Zookeeper

CLIENTPORT:ZK run port, default 2181

3. Start and close

CD Zookeeper-3.4.8/bin

./zkserver.sh [Start|start-foreground|stop|restart|status|upgrade|print-cmd]

[Root@localhost bin]#./zkserver.sh  start
zookeeper JMX enabled by default
Using config:/usr/andy/ zookeeper/zookeeper-3.4.8/bin/.. /conf/zoo.cfg
Starting Zookeeper ... Started
[root@localhost bin]# netstat-anp | grep 2181
TCP6       0      0::: 2181                 :::*                    LISTEN      14562/java          
Started successfully.


V. Configuration of Zookeeper Cluster

Temporarily.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.