Zookeeper distributed lock []

I. Overview

  • Distributed Lock Solution (Purpose: To ensure the sharing of data security issues in distributed field)
  • Implementation of distributed database lock (not recommended, particularly low efficiency)
  • Based on Redis implementation of distributed lock setNx (very troublesome to consider the deadlock, the question of release), redission distributed lock
  • (Highly recommended) based on the realization Zookeeper distributed lock
  • SpringCloud built to achieve a very simple global lock (popular) to implement, using a temporary node releases the lock (maximum efficiency), time to failure is easy to control

  1. Distributed Lock (the causes: Because the server generates clusters)
  2. How to generate a set number on a single server (guaranteed unique) UUId, timestamp, (large companies) redis
  3. Why redis to generate orders in advance to generate a good number 1.5 million order number, stored in redis, when after the client orders, and direct access to redis corresponding order number can get, because redis itself is single threaded, but also if redis only 500,000 order number, when there continues to generate 1 million order number.
  4. If you use UUId in a cluster environment, the time stamp to ensure the uniqueness of the issue order number? Can not

The principle

  1. At the same time create multiple jvm one and the same node (/ lock) on the zookeeper, zookeeper because the node is the only, if only, then if there are multiple clients simultaneously create the same node / lock, then the end, only to see who can quickly grab resources, who can create / lock node, this time using a temporary node type should type.
  2. If jvm1 node has been created successfully, then this time jvm2, when jvm3 node is created, which will be reported / lock node already exists. This time jvm2 and jvm3 wait. If jvm1 program has now been finished, then,
  3. How Zookeepre achieve the lock is released? If jvm1 program has now been finished, then the current jvm1 current Session of ZK has been closed session.
  4. How Zookeepre acquire a lock? See who is requesting node to create fast, and anyone can get a lock.
  5. This time jvm2 and jvm3 using Watcher (event notifications) to obtain / lock has been deleted, this time to re-enter the acquisition request.
  6. If the program has not been processed, may lead to a deadlock, you can set expiration date, look at the business case, 60 seconds, zk connection not forced to close, full of delays, direct calls to close, the probability of a delay is very small.

Implementation code

Lock Code

Guess you like

Origin www.cnblogs.com/haoworld/p/zookeeper-fen-bu-shi-suo.html