Detailed zookeeper installation and use

Table of contents

1 Overview

1.1. Function

1.2. Features

1.3. Data structure

2. Install

2.1.Windows

2.2.Linux

3. Basic operation

3.1. Increase

3.2. Delete

3.3. Change

3.4. Check

3.5. Monitoring

4.JAVA operation Zookeeper

4.1. Dependency

4.2. Client

4.3. Increase

4.4. Delete

4.5. Check

4.6. Change


1 Overview

1.1. Function

Zookeeper, a distributed service coordination and management component of Apache. There are two main functions:

  • storage
  • notify

1. storage

Zookeeper's storage, similar to a database, is used to store data. It stores some small but important information that is needed globally. For example, it can be used as a registration center to store information about various services.

2. Notification

The client registers with the zookeeper server, declares what information it wants to observe about the zookeeper server, and becomes an observer of the zookeeper server. Once the information to be observed changes, zookeeper will report to the registered observer corresponding to the information. Send a notification.

1.2. Features

Zookeeper has two major features:

  • CP, strong consistency, partition fault tolerance.
  • With its own load balancing, each access to the cluster will be automatically mapped to the node with the smallest current load.

1.3. Data structure

Zookeeper's data model structure is very similar to the Unix file system. It is a tree. Each node is called a znode. Each znode can store 1MB of data by default. Each znode is identified by a unique path.

2. Install

2.1.Windows

download:

Download the compressed package directly on the official website and unzip it.

start up:

The instruction script is in the bin directory under the installation directory, and the Windows startup script is zkServer.cmd

The startup will report an error because the configuration file is missing:

The configuration file scanned under the config directory when zookeeper starts is named zoo.cfg, and there is only one zoo_sample.cfg after the default download and installation in this directory. This is a configuration file template officially given by zookeeper, which can be directly renamed zoo .cfg can start zookeeper normally.

Start the client:

After starting the zookeeper server, start the client to use instructions in the client to operate zookeeper.

2.2.Linux

Example configuration file for Linux:

tickTime: heartbeat time, communication interval, unit, number of heartbeats.

initLimit: The time spent in the synchronization phase when the server starts, the unit, and the number of heartbeats.

syncLimit: The waiting time from sending a notification to receiving a response, unit, the number of heartbeats.

server .number: cluster configuration, the number is unique in the cluster, aligned with the value in myid under the data directory.

Node IP: communication port: election port; service port.

3. Basic operation

3.1. Increase

Empty nodes cannot be created, only nodes with data can be created.

 

 

-e -s can be mixed to create ordered temporary nodes.

3.2. Delete

3.3. Change

3.4. Check

Check data:

 Check status:

  • cZxid: This is the transaction ID that caused the znode change to be created.
  • mZxid − This is the transaction ID that last modified the znode change.
  • oZxid: This is the transaction ID for znode changes where nodes were added or removed.
  • ctime: Indicates the znode creation time in milliseconds from 1970-01-01T00:00:00Z
  • mtime: Indicates the last modification time of the znode in milliseconds since 1970-01-01T00:00:00Z.
  • dataVersion: Indicates the number of changes made to the znode's data
  • cversion: This indicates the number of changes made to this znode's node
  • acIVersion: Indicates the number of changes made to the ACL of this znode ephemeralOwner: If the znode is an ephemera type node, this is the session D of the node owner, if the ode is not an ephemera# point, this field is set to zero.
  • dataLength: This is the length of the znode data field
  • numChildren: This indicates the number of children of the znode

3.5. Monitoring

Watch, the monitoring mechanism, the implementation of the zookeeper notification function, is a very important feature in Zookeeper. Based on the nodes created on zookeeper, we can bind monitoring events to these nodes. For example , events such as node data changes, node changes, and status changes can be monitored. Through this event mechanism, functions such as distributed locks and cluster management based on zookeeper can be realized.

When the data changes, zookeeper will generate a watcher event and send it to the client. But the client will only be notified once. If this node changes again later, the client that set the watcher before will not receive the message again ( watcher is a one-time operation ), and the permanent monitoring effect can be achieved through loop monitoring.

ls path watch monitors the changes of child nodes under the path

get path watch monitors the changes of data under this path

4.JAVA operation Zookeeper

4.1. Dependency

The version number, please confirm it yourself, the blogger here is only 3.5.8:

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.5.8</version>
</dependency>

4.2. Client

The constructor has three parameters:

  • Connection address, IP address of zookeeper
  • The session timeout time, if the server does not receive the client's heartbeat after this time, the session will be disconnected and the temporary data of the session will be cleared.
  • Watch notification, the notification returned when the watch data changes.

4.3. Increase

Three parameters:

  • path: path

  • data: data

  • ACL mechanism: Use the enumeration class that comes with the zookeeper API CreateMode: Create a temporary node or a permanent node (-e or -s), use the enumeration class that comes with the zookeeper API

4.4. Delete

zooKeeper.delete(znodePath, -1);

In ZooKeeper, when deleting a znode (node), a version number needs to be passed as a condition for version checking. This version number is used to ensure that the version of the znode is consistent with the specified version number when the delete operation is performed. If the version numbers do not match, the delete operation will fail.

In the delete method zooKeeper.delete(znodePath, version), the second parameter versionis used to specify the version number of the znode to be deleted. When versionthe parameter is set to -1, it means that the version check is not performed, that is, the version of the znode is ignored, and the znode is deleted directly. This means that no matter what version the znode is, it will be deleted.

4.5. Check

Check node:

Check data:

Check status:

 

4.6. Change

version, the version number, must be strictly aligned with dataVersion,

You can check the status to get the version number,

After the modification is successful, dataVersion will increase by 1.

 

Guess you like

Origin blog.csdn.net/Joker_ZJN/article/details/131906247