Distributed registry -Zookeeper

Zookeeper


  • Article Directory

1. What is Zookeeper?

Zookeeper is one for maintaining configuration information, naming, providing distributed synchronization, and centralized services group providing services. All of these types of services are being used in some form of distributed applications. Every time they realize, there is a lot of work needed to repair the inevitable bug and competitive conditions. Because of achieving these types of services is difficult, the application initially will usually reduce them, which makes them vulnerable when change occurs, difficult to manage. Even if performed correctly, when you deploy the application, different implementations of these services will also lead to management complexity. Zookeeper aimed at refining the nature of these different services is a very simple interface, with centralized coordination of services. Service itself is distributed, and highly reliable. Services will achieve consensus, group management and presence protocol, so that applications do not need to implement them. The use of these specific applications will include mixing the particular components and applications specific convention zoo administrator. Zookeeper Recipes shows how to use this simple service to build a more powerful abstraction.

2. build Zookeeper

According to their own choice, download the appropriate version Zookeeper Download


$ tar -zxvf apache-zookeeper-3.6.0-bin.tar.gz

$ cd /apache-zookeeper-3.6.0-bin

$ mkdir data #创建data目录

$ vim /apache-zookeeper-3.6.0-bin/conf/zoo.cfg

Edit the configuration file as follows:

#ZK中的一个时间单元。ZK中所有时间都是以这个时间单元为基础,进行整数倍配置的。例如,session的最小超时时间是2*tickTime
tickTime=2000

#Follower在启动过程中,会从Leader同步所有最新数据,然后确定自己能够对外服务的起始状态。Leader允许F在 initLimit 时间内完成这个工作。通常情况下,我们不用太在意这个参数的设置。如果ZK集群的数据量确实很大了,F在启动的时候,从Leader上同步数据的时间也会相应变长,因此在这种情况下,有必要适当调大这个参数了
initLimit=10

#在运行过程中,Leader负责与ZK集群中所有机器进行通信,例如通过一些心跳检测机制,来检测机器的存活状态。如果L发出心跳包在syncLimit之后,还没有从F那里收到响应,那么就认为这个F已经不在线了。注意:不要把这个参数设置得过大,否则可能会掩盖一些问题
syncLimit=5

#存储快照文件snapshot的目录。默认情况下,事务日志也会存储在这里。建议同时配置参数dataLogDir, 事务日志的写性能直接影响zk性能
dataDir=/data/apache-zookeeper-3.6.0-bin/data

#客户端连接server的端口,即对外服务端口,一般默认为2181
clientPort=2181 

Start the server

$ cd /data/apache-zookeeper-3.6.0-bin/bin/

$ ./zkServer.sh start

Shown below, then start successfully:

3. Cluster Setup Zookeeper

3.1 cluster server configuration required

hostname ipaddress mask gateway port
master 192.168.80.130 255.255.255.0 192.168.80.2 2181
slave-one 192.168.80.131 255.255.255.0 192.168.80.2 2181
slave-two 192.168.80.132 255.255.255.0 192.168.80.2 2181

3.2 Why is the number of clusters is at least three, and preferably an odd number?

zookeeper cluster is typically used to provide coordination services to users of distributed applications, in order to ensure data consistency, zookeeper cluster of such division three roles: leader, follower, observer correspond to the president, parliamentarians and observers .

  • President (leader): responsible for initiating and vote on the resolution, updates the system status.

  • Members (follower): for receiving a client request and return the results to the client to vote in the election process.

  • Observer (observer): you may also receive client connections, the write request forwarded to the leader node, but does not participate in the voting process, synchronize only leader of the state. Usually do loads of queries.

We know that maintaining consistent data in each machine, zookeeper cluster can ensure that the client initiates each query operation, a cluster node can return the same results.

But for client-initiated modification, change or delete data, does it? So many machines in a cluster, you change yours, I modified my, and returns the data cluster which machine it?

This is a mess and needs a leader, so the zookeeper cluster, the role of the leader is manifested only modify the operating data of the leader node have the right to initiate, and even received a follower node modification operation initiated by the client, but also the after its transfer to the leader to handle, leader receives a request to modify the data, it will broadcast a message to all the follower, let them perform an action, follower after the implementation, it will reply to the leader is finished. When the leader received more than half a follower of the confirmation message, it will determine whether the operation is finished, and then broadcast to all the action in force follower.

So zookeeper in the cluster leader is indispensable, but the leader node is how to generate it? In fact, generated by all nodes follower elections, pay attention to the democratic thing, and only one leader node, after all, a country can not have more than the president.

This time back to our subtitle, Why zookeeper number of nodes is odd, we have the following one by one to illustrate:

  • Fault tolerance

First, from the fault tolerance to illustrate :( need to ensure that the cluster can have half of the vote)

2 servers, running the job from at least two (2 is a half, more than half of at least 2), the normal operation of a server are not allowed to hang, but with respect to a single server node, there are two servers 2 single point of failure, so directly ruled out.

3 servers, at least two running job (half 3 is 1.5, more than half of at least 2), the normal operation of a server may be allowed to hang

4 servers, at least three running job (of 4 or more 2 half, half of a minimum of three), the normal operation of a server may be allowed to hang

5 servers, at least three running job (half of 5 is 2.5, more than half of a minimum of three), the normal operation may be allowed to hang servers 2

  • Anti-brain split

Brain split-brain split clusters usually occurs when the communication between nodes is unreachable, the cluster will be split into different clusters of small, small clusters each elect their own leader node, resulting in multiple nodes of the original cluster leader case, this is the split brain.

3 servers, vote for half of 1.5, a service split, and the other two servers can not pass, this time 2 server cluster (2 votes greater than half of the 1.5 votes), it is possible to elect a leader, and one server the cluster can not be elections.

4 servers, vote for half of 2, can be divided into two clusters 1,3 or 2,2 two clusters, cluster for 1,3, 3 clusters can elections; 2,2 for a cluster, you can not select, causing no leader node.

Five servers, vote for half of 2.5, can be divided into two clusters 1,4, or 2,3 two clusters, two clusters respectively can only elect a cluster, the cluster meet the zookeeper build number.

The above analysis, fault tolerance, and prevent us from brain split two 3 shows the minimum number of servers is to build a cluster, it will cause an error when no leader node of four split brain occurs.

3.3 cluster configuration file

3.3.1 Creating myid file

$ echo 0 > /data/apache-zookeeper-3.6.0-bin/data/myid #192.168.80.130

$ echo 1 > /data/apache-zookeeper-3.6.0-bin/data/myid #192.168.80.131

$ echo 2 > /data/apache-zookeeper-3.6.0-bin/data/myid #192.168.80.132

3.3.2 edit the configuration file zoo.cfg

tickTime=2000

initLimit=10

syncLimit=5

dataDir=/data/apache-zookeeper-3.6.0-bin/data

clientPort=2181

server.0=192.168.80.130:2888:3888
server.1=192.168.80.131:2888:3888
server.2=192.168.80.132:2888:3888

server.A=B:C:D

A: wherein A is a number that is the number (set value myid) server;

B: ip is the address of the server;

C: Port Leader elections;

D: Zookeeper between the server communication port.

3.2.3 Configuring the system environment variables

In order to be able to start zookeeper cluster in any directory, we need to configure the environment variables

$ vim /etc/profile

#set zookeeper environment
export ZK_HOME=/data/apache-zookeeper-3.6.0-bin
export PATH=$PATH:$ZK_HOME/bin

$ source /etc/profile

3.2.4 Start zookeeper cluster services

$ zkServer.sh start         #启动服务

$ zkServer.sh stop          #停止服务

$ zkServer.sh restart       #重启服务

$ zkServer.sh status        #查看服务状态

We are executing the startup command to the cluster of three machines. After machining, the view cluster node status were:

That is, a cluster appear as build success:

3.4 Zookeeper Basics

3.4.1 Zookeeper storage structure

3.4.2 Zookeeper distributed coordination tools scenarios

  • Naming Service (registry) Dubbo registry (dynamic load balancing)

  • Distributed configuration center, dynamic management profile

  • Messaging middleware, event notification (publish-subscribe)

  • Zookeeper distributed transaction (global coordinator)

  • Zookeeper distributed lock to achieve

    Distributed Lock Solution (Purpose: To ensure the sharing of data security issues in distributed in)

    1. The implementation of distributed database lock (not recommended, particularly low efficiency)

    2. Based on Redis implementation of distributed lock (consider the deadlock, the question of release) redissession Distributed Lock

    3. (Temporary easily controlled node releases the lock, the dead time) based on the implementation of distributed lock Zookeeper

    Zookeeper distributed lock achieve the classification of:

    1. Keep Exclusive: to maintain the so-called exclusive, is that all attempts to acquire the lock of the client, and ultimately only one can successfully get the lock. The usual practice is to put on a znode zk seen as a lock, which is achieved by create znode way. All clients to create / distribute_lock nodes, that create the ultimate success of the client that is owned the lock.

    2. Control Timing: that all views to get the client the lock, and ultimately will be scheduled for execution, but there is a global timing up. And substantially similar to the above practices, where only / distributelock already pre-existing, the client creates an ordered temporary node (which can be controlled by property node: CreateMode.EPHEMERALSEQUENTIAL specified) below it. Zk parent node (/ distribute_lock) maintain a sequence, to ensure that the timing of the creation of a child node, thus forming a global timing for each client.

    Zookeeper The principle:

    More of the same JVM create a temporary node in the same Zookeeper, due to the repetitive nature of sibling nodes allowed to ensure that only one node created by the JVM

    Zookeeper how to acquire a lock?

    JVM create a node that fast, you get the first lock

    How Zookeeper releases the lock?

    Get the lock JVM executing the program, close the current session session through event notification to the rest of the JVM re waiting to grab a lock

  • Zookeeper implement election strategy (Sentinel mechanism)

  • Zookeeper for local load balancing

3.4.3 Zookeeper Node Type

  • Persistent node
    • Node creation of a permanent persistence to the hard disk
  • Temporary node
    • Nodes and links remain current session, if the link is broken, the temporary node is deleted
Published 11 original articles · won praise 2 · Views 436

Guess you like

Origin blog.csdn.net/qq_41112063/article/details/105312845