Ants gold dress decade architect teach you how to learn from scratch Distributed Lock

Overview lock

Distributed Lock

Distributed Lock can actually be understood as: control of a distributed system to share resources and orderly operation, to maintain consistency through mutually exclusive . For a very appropriate example: Suppose a shared resource is a house, there are all kinds of books, distributed systems is to come in person to read, distributed lock is to ensure that the house has only one door and only one person can enter , and the door only a key. Many people then go read a book, you can line up, the first person took the key and opened the door into the house and locked the door read, and then the second person is not the key, then waited out a ranking, then you take the key in, then so is

Why lock

Ants gold dress decade architect teach you how to learn from scratch Distributed Lock

  1. Multitasking environment needed only
  2. Tasks need to write to the same shared resources;
  3. Access to resources are mutually exclusive

In order to ensure a method can only be performed in the same thread at the same time high concurrency, in case of the conventional single stand-alone application deployment, Java can be used concurrently process related API (such as the synchronized or ReentrantLcok) for exclusive control. However, as the business development needs, the original single-machine deployment system is evolved into a distributed system, due to the distributed system multi-threaded, multi-process and distributed on different machines, which will enable concurrency control at the original stand-alone deployment locking strategy fails to solve this problem we need a mutual exclusion mechanism across JVM to control access to shared resources, which is distributed lock problem to be solved.

Distributed Lock plan comparison

Ants gold dress decade architect teach you how to learn from scratch Distributed Lock

Template Method Pattern

Arrangement main flow in the parent class, the step of effecting a delay to the sub-class to implement.

Ants gold dress decade architect teach you how to learn from scratch Distributed Lock

  1. Defined lock interface Lock
  2. GetLock implemented method AbstractLock template inside the lock, to achieve a common logic.
  3. You can not really step as a virtual method, thrown pot to subclass implementation.
  4. Subclasses need only focus his small step logic, implemented tryLock, waitLock, unLock method

Distributed Lock based Zookpper

Zookeeper data structure

Ants gold dress decade architect teach you how to learn from scratch Distributed Lock

  • ZooKeeper data structure similar to Linux
  • Each node has a value

zookeeper at the bottom really only provides two functions

  1. Management (storage, read) the data submitted by the user program;
  2. And provides data node monitoring service for user programs

zookeeper node type

  • ** PERSISTENT- after persistent directory node ** zookeeper client is disconnected, the node is still present
  • PERSISTENT_SEQUENTIAL- persistent directory node sequence number after zookeeper client disconnects, the node is still present, but Zookeeper numbered sequentially to the node name
  • EPHEMERAL- temporary directory node after the client disconnects from the zookeeper, the node is deleted
  • ** After EPHEMERAL_SEQUENTIAL- temporary sequentially numbered directory node ** zookeeper client is disconnected, the node is removed, but Zookeeper numbered sequentially to the node name

watcher mechanisms icon

Ants gold dress decade architect teach you how to learn from scratch Distributed Lock

Watcher (observer pattern)

Ants gold dress decade architect teach you how to learn from scratch Distributed Lock

Observer pattern elements:

  • Target - shop owner (event source)
  • Observer - Customer (event consumer)
  • Event - the phone rang

zookeeper node of the same name based on the Distributed Lock

Ants gold dress decade architect teach you how to learn from scratch Distributed Lock

This concludes the paper like friends remember little praise and attention Ha, thanks!

Guess you like

Origin blog.csdn.net/qwe123147369/article/details/92005439