ZK implements distributed transaction lock code and principle verification

Let's review the principle of ZK's implementation of distributed locks:

When each client locks a method, a unique instantaneous ordered node is generated in the directory of the specified node corresponding to the method on zookeeper. The way to determine whether to acquire a lock is very simple, you only need to determine the one with the smallest sequence number in the ordered node. When releasing the lock, just delete the transient node. At the same time, it can avoid the deadlock problem that the lock cannot be released due to the service downtime.

Verify through code that transient ordered nodes are generated

package com.jv.zookeeper.curator;

import java.util.concurrent.TimeUnit;

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;

public class TestInterProcessMutex {
	public static void main(String[] args) throws Exception {
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.245.101:2181", retryPolicy);
		client.start();
		InterProcessMutex lock = new InterProcessMutex(client, "/mylock");
		//lock.acquire(1000, TimeUnit.MILLISECONDS) 获取锁,超时时间为1000毫秒
		if ( lock.acquire(1000, TimeUnit.MILLISECONDS) ) 
		{
		    try 
		    {
		       System.out.println("得到锁,并执行");
		       //模拟线程需要执行很长时间,观察ZK中/mylock下的临时ZNODE情况
		       Thread.sleep(10000000);
		    }
		    finally
		    {
		        lock.release();
		        System.out.println("释放锁");
		    }
		}
	}
}
package com.jv.zookeeper.curator;

import java.util.concurrent.TimeUnit;

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;

public class TestInterProcessMutex2 {
	public static void main(String[] args) throws Exception {
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.245.101:2181", retryPolicy);
		client.start();
		InterProcessMutex lock = new InterProcessMutex(client, "/mylock");
		//将超时时间设置足够长,观察ZK中ZNODE的情况,以验证分布式锁的原理是否是使用建立临时顺序ZNODE实现的
		if ( lock.acquire(1000000, TimeUnit.MILLISECONDS) ) 
		{
		    try 
		    {
		       System.out.println("得到锁,并执行");
		       Thread.sleep(10000000);
		    }
		    finally
		    {
		        lock.release();
		        System.out.println("释放锁");
		    }
		}
	}
}

To run the code, add the following dependencies to pom.xml

		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>4.0.0</version>
		</dependency>

 

Run TestInterProcessMutex first, then run TestInterProcessMutex2

Log in to the zookeeper host using xshell or securityCRT

Go to the installation directory/bin of zookeeper

./zkCli.sh

ls / mylock

It can be seen that two instantaneous ordered nodes are indeed generated, and the client with the smaller serial number obtains the lock

It is really convenient to use curator after packaging

 

In addition, curator can also easily implement elections

LeaderSelectorListener listener = new LeaderSelectorListenerAdapter()
{
    public void takeLeadership(CuratorFramework client) throws Exception
    {
        // 这是你变成leader时执行的方法,你可以在这里执行leader的所有操作
        // 如果你想放弃leader,你必须退出此方法
    }
}

LeaderSelector selector = new LeaderSelector(client, path, listener);
selector.autoRequeue();  // not required, but this is behavior that you will probably expect
selector.start();

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326521227&siteId=291194637