Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Zookeeper in java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of Zookeeper in java, which is very detailed and has certain reference value. Friends who are interested must finish it!

Zookeeper is a distributed coordination service that can be used for service discovery, distributed locking, distributed leadership election, configuration management, etc. Zookeeper provides a tree structure similar to the Linux file system (can be thought of as a lightweight in-memory file system, but only suitable for storing a small amount of information, not suitable for storing a large number of files or large files), and provides a monitoring notification mechanism for each node.

Zookeeper role

A Zookeeper cluster is a highly available cluster based on master-slave replication. Each server assumes one of the following three roles

Leader

1. A Zookeeper cluster will have only one actual working Leader at a time, which initiates and maintains heartbeats with each Follwer and Observer.

two。 All writes must be done through Leader and then Leader broadcasts them to other servers. As long as more than half of the nodes (excluding observeer nodes) are successfully written, the write request will be submitted (2PC-like protocol).

Follower

1. A Zookeeper cluster may have multiple Follower at the same time, which responds to the heartbeat of the Leader

2.Follower can directly process and return the read request from the client, and forward the write request to Leader for processing

3. And is responsible for voting on the request when the Leader processes the write request.

Observer

The role is similar to Follower but does not have the right to vote. Zookeeper needs to ensure high availability and strong consistency. In order to support more clients, more Server;Server needs to be added, and voting delay increases, which affects performance; Observer,Observer is introduced to not participate in voting; Observers accepts client connections and forwards write requests to leader nodes; more Observer nodes are added to improve scalability without affecting throughput.

How Zookeeper works (Atomic broadcast)

The core of 1.Zookeeper is atomic broadcasting, which ensures synchronization between server. The protocol that implements this mechanism is called the Zab protocol. There are two modes of Zab protocol, which are recovery mode and broadcast mode.

two。 When the service starts or after the leader crashes, the Zab enters the recovery mode, and when the leader is elected and most of the server's completion is synchronized with the state of the leader, the recovery mode ends.

3. State synchronization ensures that leader and server have the same system state.

4. Once leader has synchronized his status with most of the follower, he can start broadcasting the message, that is, entering the broadcast state. At this point, when a server joins the zookeeper service, it starts in recovery mode, discovers the leader, and synchronizes the status with the leader. When the synchronization ends, it also participates in the message broadcast. The Zookeeper service remains in the Broadcast state until leader crashes or leader loses most of its followers support.

5. The broadcast mode needs to ensure that the proposal is processed sequentially, so zk uses an incremental transaction id number (zxid) to guarantee it. All proposals (proposal) are made with zxid when they are made.

6. In the implementation, zxid is a 64-bit number, and its high 32-bit epoch is used to identify whether the leader relationship has changed. Every time a leader is selected, it will have a new epoch. The lower 32 bits is an incremental count.

7. When leader crashes or leader loses most of its follower, the zk enters recovery mode, which requires a new leader to be re-elected so that all server are restored to the correct state.

Four forms of directory nodes in Znode

PERSISTENT: persistent node.

EPHEMERAL: temporary node.

PERSISTENT_SEQUENTIAL: persist sequentially numbered directory nodes.

EPHEMERAL_SEQUENTIAL: temporarily sequentially numbered directory nodes.

Installation and use of ZooKeeper

Download ZooKeeper using Docker

Docker pull zookeeper:3.5.8

Run ZooKeeper

Docker run-d-- name zookeeper-p 2181 zookeeper:3.5.8

Connect to the ZooKeeper service

First use docker ps to view the ContainerID of ZooKeeper, and then use the docker exec-it ContainerID / bin/bash command to enter the container.

Enter the bin directory and connect to the ZooKeeper service with the command. / zkCli.sh-server 127.0.0.1 server 2181

Root@eaf70fc620cb:/apache-zookeeper-3.5.8-bin# cd bin

Page after successful connection to ZooKeeper service

Common command

You can view common ZooKeeper commands through the help command

Create a node (create command)

The node1 node is created in the root directory through the create command, and the string associated with it is "node1"

[zk: 127.0.0.1 create (CONNECTED) 34] create / node1 "node1"

The node1 node is created in the root directory through the create command, and the content associated with it is the number 123

[zk: 127.0.0.1 create 2181 (CONNECTED) 1] create / node1/node1.1 123

Created / node1/node1.1

Update node data content (set command)

[zk: 127.0.0.1 set 2181 (CONNECTED) 11] set / node1 "set node1"

Get the data of a node (get command)

The get command can get the data content and state of the specified node, and you can see that we have changed the node data content to "set node1" through the set command.

Set node1cZxid = 0x47ctime = Sun Jan 20 10:22:59 CST 2019mZxid = 0x4bmtime = Sun Jan 20 10:41:10 CST 2019pZxid = 0x4acversion = 1dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 9numChildren = 1

View child nodes in a directory (ls command)

View the nodes in the root directory through the ls command

[zk: 127.0.0.1 ls 2181 (CONNECTED) 37]

[dubbo, ZooKeeper, node1]

View the nodes in the node1 directory through the ls command

[zk: 127.0.0.1 ls 2181 (CONNECTED) 5] ls / node1

[node1.1]

The ls command in ZooKeeper is similar to ls in the linux command, which lists all child node information under the absolute path path (level 1, not recursive)

View node status (stat command)

View node status through the stat command

[zk: 127.0.0.1 stat 2181 (CONNECTED) 10] stat / node1cZxid = 0x47ctime = Sun Jan 20 10:22:59 CST 2019mZxid = 0x47mtime = Sun Jan 20 10:22:59 CST 2019pZxid = 0x4acversion = 1dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 11numChildren = 1

View node information and status (ls2 command)

The ls2 command is more like a combination of the ls command and the stat command. The information returned by the ls2 command consists of two parts:

1. List of child nodes

two。 The stat information of the current node.

[zk: 127.0.0.1 ls2 2181 (CONNECTED) 7] ls2 / node1 [Node 1.1] cZxid = 0x47ctime = Sun Jan 20 10:22:59 CST 2019mZxid = 0x47mtime = Sun Jan 20 10:22:59 CST 2019pZxid = 0x4acversion = 1dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 11numChildren = 1

Delete a node (delete command)

This command is simple, but it is important to note that if you want to delete a node, the node must have no child nodes.

[zk: 127.0.0.1 delete 2181 (CONNECTED) 3] delete / node1/node1.1

The above is all the content of the article "sample Analysis of Zookeeper in java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report