Implementation of queue source code based on zookeeper

Implementation principle

The first in first out queue is the most commonly used queue. Using zookeeper to implement the first in first out queue is to create persistence in a specific directory_ Equivalent node. When the creation is successful, watcher notifies the waiting queue. The queue deletes the node with the lowest serial number for consumption. In this scenario, zookeeper's znode is used for message storage. The data stored in znode is the message content in the message queue, and the serial number is the message number, which can be taken out in order. Since the created nodes are persistent, there is no need to worry about the loss of queue messages.

Queue

Distributed queue is a general data structure. In order to implement distributed queue in zookeeper, you first need to specify a znode node as the queue node. Each distributed client puts data into the queue by calling create() function. When calling create(), the point path name ends with "QN -", And set the sequence node flag. Due to the setting of the sequence flag of the node, the new pathname has the following string pattern: "_path-to-queue-node_qn-x", X is the unique increment sign. Clients that need to get / remove data from the queue first call the getchildren() function, If there is data, it can be obtained (it can be deleted or not deleted after obtaining the data). If there is no data, set watch to true on the queue node and wait for the node with the smallest sequence number to be triggered and processed (that is, take data from the node with the smallest sequence number).

Application scenario

Zookeeper queue is not suitable for occasions requiring high performance, but it can be considered when the amount of data is small. For example, if zookeeper has been used in the project and requires a small-scale queue application, the queue implemented by zookeeper can be used; After all, the introduction of a message oriented middleware will increase the complexity of the system and the pressure of operation and maintenance.

Detailed code

Zookeeperclient tool class

ZooKeeperQueue

test

Run the main method to the output result of the machine

summary

The above is all about the introduction of queue and queue source code based on zookeeper in this paper. I hope it will be helpful to you.

Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>