Curator framework to achieve ZooKeeper Distributed Lock

 Exclusive lock (X)

Here mainly to talk about distributed lock in an exclusive lock. Exclusive lock (Exclusive Locks, referred to as X lock), also known as a write lock or exclusive lock, the lock is a basic type. If the transaction T1 data object O1 plus the exclusive lock, then the duration of the lock, allowing only T1 for O1 read and update data for any other transaction can be any type of operation O1, straight T1 release the exclusive lock.

Defined lock

In ZooKeeper, the lock may be represented by creating a data node in ZooKeeper. For example, / exclusive_lock / lock node (znode) can be expressed as a lock.

Acquire a lock

In needs to obtain an exclusive lock, all clients are trying to create () interfaces, create a temporary child node / exclusive_lock / lock in / exclusive_lock node, but strong consistency ZooKeeper final will ensure only a single customer can successfully created, then that the client obtains a lock. At the same time, not all client transactions to acquire the lock can only be in a wait state, the wait state of the client in advance can register a child node change Watcher listens on / exclusive_lock node, in order to monitor in real time to change the situation of child nodes.

Release the lock

 

import java.util.concurrent.TimeUnit;
import lombok.Cleanup;
import lombok.SneakyThrows;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;


public class ZkLock {

  @SneakyThrows
  public static void main(String[] args) {

    finalConnectString = String "localhost: 2181, localhost: 2182, localhost: 2183" ; 

    // retry strategy, the waiting time required between each retry initialization, the reference waiting time of 1 second. 
    RetryPolicy = RetryPolicy new new ExponentialBackoffRetry (1000,. 3 ); 

    // Use default session time (60 seconds) and the connection time (15 seconds) to create a client Zookeeper 
    @Cleanup CuratorFramework Client = CuratorFrameworkFactory.builder (). 
        ConnectString (connectString) . 
        connectionTimeoutMs ( 15 * 1000 .) 
        sessionTimeoutMs ( 60 * 100 .) 
        retryPolicy (retryPolicy). 
        Build (); 

    // start the client 
    client.start (); 

    Final String lockNode = "/ lock_node";
    InterProcessMutex lock = new InterProcessMutex(client, lockNode);
    try {
      // 1. Acquire the mutex - blocking until it's available.
      lock.acquire();

      // OR

      // 2. Acquire the mutex - blocks until it's available or the given time expires.
      if (lock.acquire(60, TimeUnit.MINUTES)) {
        Stat stat = client.checkExists().forPath(lockNode);
        if (null != stat){
          // Dot the transaction
        }
      }
    } finally {
      if (lock.isAcquiredInThisProcess()) {
        lock.release();
      }
    }
  }

}

 

Guess you like

Origin www.cnblogs.com/frankyou/p/11412394.html