溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

zookeeper中怎么實現(xiàn)分布式鎖

發(fā)布時間:2021-08-06 15:07:38 來源:億速云 閱讀:180 作者:Leah 欄目:編程語言

zookeeper中怎么實現(xiàn)分布式鎖,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

一、理論基礎

    利用zk的臨時有序節(jié)點機制實現(xiàn)分布式鎖

    zk節(jié)點的四種類型:持久節(jié)點、持久順序節(jié)點、臨時節(jié)點、臨時順序節(jié)點。

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

zookeeper中怎么實現(xiàn)分布式鎖

二、代碼實現(xiàn)

1、資源類FakeLimitedResource

package com.lau.distributed.lock;

/** 
* @ClassName: FakeLimitedResource 
* @Description: TODO
* @author Liu 
* @date 2021年4月17日 下午9:30:14 
*/
public class FakeLimitedResource {
	private Integer ticket = 250;
	
	public void use(){
		System.out.println("分布式鎖-線程:" + Thread.currentThread().getName() + "賣了1張票,火車票還剩:" + (--ticket) + "張");
	}
}

2、資源控制類ExampleClientThatLocks(鎖操作)

package com.lau.distributed.lock;

import java.util.concurrent.TimeUnit;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;

/** 
* @ClassName: ExampleClientThatLocks 
* @Description: TODO
* @author Liu 
* @date 2021年4月17日 下午9:56:32 
*/
public class ExampleClientThatLocks {
	//鎖對象
	private final InterProcessMutex lock;
	
	//火車票共享資源
	private final FakeLimitedResource resource;
	
	//客戶端名稱
	private final String clientName;
	
	public ExampleClientThatLocks(CuratorFramework client, String lockPath, FakeLimitedResource resource, String clientName) {
		this.resource = resource;
		this.clientName = clientName;
		
		lock = new InterProcessMutex(client, lockPath);
	}
	
	public void doWork(long time, TimeUnit unit) throws Exception{
		//嘗試獲取鎖,過時不候(超時機制)
		if(!lock.acquire(time, unit)) {
			throw new IllegalStateException(clientName + "could not acquire the lock!");
		}
		
		try {
			System.out.println(clientName + " has acquired the lock!");
			resource.use();
		} 
		finally {
			System.out.println(clientName + " has released the lock!");
			lock.release();
		}
	}
}

3、測試類LockingExample

package com.lau.distributed.lock;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/** 
* @ClassName: LockingExample 
* @Description: TODO
* @author Liu 
* @date 2021年4月17日 下午9:49:44 
*/
public class LockingExample{
	private static final int QTY = 5;
	private static final int PERMISSIONS = 50;
	private static final String CONNECTION_HOST = "192.168.1.106:2181";
	private static final String PATH = "/lock";
	
	public static void main(String[] args){
		//模擬火車票一次只能由一個進程訪問
		final FakeLimitedResource resource = new FakeLimitedResource();
		
		//定義默認初始化有5個線程的線程池
		ExecutorService service = Executors.newFixedThreadPool(QTY);
		
		try{
			for(int i = 0; i < QTY; i++){
				final int index = i;
				
				Callable<Void> task = new Callable<Void>(){
					public Void call() throws Exception{
						//獲取zk集群連接
						CuratorFramework client = CuratorFrameworkFactory.newClient(CONNECTION_HOST, 
							new ExponentialBackoffRetry(1000, 3, Integer.MAX_VALUE));
							
						try{
							client.start();
							
							ExampleClientThatLocks example = new ExampleClientThatLocks(client, PATH, resource, "client" + index);
							
							//模擬一個線程賣50張票
							for(int j = 0; j < PERMISSIONS; j++){
								example.doWork(10, TimeUnit.SECONDS);
							}
						}
						catch(Exception ex){
							ex.printStackTrace();
						}
						finally{
//							ClosableUtils.closeQuietly(client);
							client.close();
						}
							
						return null;
					}
				};
				
				service.submit(task);
			}
		
			service.shutdown();
			service.awaitTermination(10, TimeUnit.MINUTES);
		}
		catch(Exception ex){
		
		}
	}

}

4、pom.xml

<dependencies>
  	<dependency>
	    <groupId>org.apache.curator</groupId>
	    <artifactId>curator-recipes</artifactId>
	    <version>4.0.0</version>
	</dependency>
	<dependency>
	    <groupId>org.apache.curator</groupId>
	    <artifactId>curator-framework</artifactId>
	    <version>4.0.0</version>
	</dependency>
	
	<dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
		<version>3.4.10</version>
    </dependency>
  </dependencies>

5、關(guān)鍵點

① 客戶端端口即將斷開連接

zookeeper中怎么實現(xiàn)分布式鎖

② 斷開連接后臨時節(jié)點消失

zookeeper中怎么實現(xiàn)分布式鎖

6、測試結(jié)果

client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:50張
client2 has released the lock!
client3 has acquired the lock!
分布式鎖-線程:pool-1-thread-4賣了1張票,火車票還剩:49張
client3 has released the lock!
client1 has acquired the lock!
分布式鎖-線程:pool-1-thread-2賣了1張票,火車票還剩:48張
client1 has released the lock!
client4 has acquired the lock!
分布式鎖-線程:pool-1-thread-5賣了1張票,火車票還剩:47張
client4 has released the lock!
client0 has acquired the lock!
分布式鎖-線程:pool-1-thread-1賣了1張票,火車票還剩:46張
client0 has released the lock!
client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:45張
client2 has released the lock!
client3 has acquired the lock!
分布式鎖-線程:pool-1-thread-4賣了1張票,火車票還剩:44張
client3 has released the lock!
client1 has acquired the lock!
分布式鎖-線程:pool-1-thread-2賣了1張票,火車票還剩:43張
client1 has released the lock!
client4 has acquired the lock!
分布式鎖-線程:pool-1-thread-5賣了1張票,火車票還剩:42張
client4 has released the lock!
client0 has acquired the lock!
分布式鎖-線程:pool-1-thread-1賣了1張票,火車票還剩:41張
client0 has released the lock!
client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:40張
client2 has released the lock!
client3 has acquired the lock!
分布式鎖-線程:pool-1-thread-4賣了1張票,火車票還剩:39張
client3 has released the lock!
client1 has acquired the lock!
分布式鎖-線程:pool-1-thread-2賣了1張票,火車票還剩:38張
client1 has released the lock!
client4 has acquired the lock!
分布式鎖-線程:pool-1-thread-5賣了1張票,火車票還剩:37張
client4 has released the lock!
client0 has acquired the lock!
分布式鎖-線程:pool-1-thread-1賣了1張票,火車票還剩:36張
client0 has released the lock!
client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:35張
client2 has released the lock!
client3 has acquired the lock!
分布式鎖-線程:pool-1-thread-4賣了1張票,火車票還剩:34張
client3 has released the lock!
client1 has acquired the lock!
分布式鎖-線程:pool-1-thread-2賣了1張票,火車票還剩:33張
client1 has released the lock!
client4 has acquired the lock!
分布式鎖-線程:pool-1-thread-5賣了1張票,火車票還剩:32張
client4 has released the lock!
client0 has acquired the lock!
分布式鎖-線程:pool-1-thread-1賣了1張票,火車票還剩:31張
client0 has released the lock!
client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:30張
client2 has released the lock!
client3 has acquired the lock!
分布式鎖-線程:pool-1-thread-4賣了1張票,火車票還剩:29張
client3 has released the lock!
client1 has acquired the lock!
分布式鎖-線程:pool-1-thread-2賣了1張票,火車票還剩:28張
client1 has released the lock!
client4 has acquired the lock!
分布式鎖-線程:pool-1-thread-5賣了1張票,火車票還剩:27張
client4 has released the lock!
client0 has acquired the lock!
分布式鎖-線程:pool-1-thread-1賣了1張票,火車票還剩:26張
client0 has released the lock!
client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:25張
client2 has released the lock!
client3 has acquired the lock!
分布式鎖-線程:pool-1-thread-4賣了1張票,火車票還剩:24張
client3 has released the lock!
client1 has acquired the lock!
分布式鎖-線程:pool-1-thread-2賣了1張票,火車票還剩:23張
client1 has released the lock!
client4 has acquired the lock!
分布式鎖-線程:pool-1-thread-5賣了1張票,火車票還剩:22張
client4 has released the lock!
client0 has acquired the lock!
分布式鎖-線程:pool-1-thread-1賣了1張票,火車票還剩:21張
client0 has released the lock!
client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:20張
client2 has released the lock!
client3 has acquired the lock!
分布式鎖-線程:pool-1-thread-4賣了1張票,火車票還剩:19張
client3 has released the lock!
client1 has acquired the lock!
分布式鎖-線程:pool-1-thread-2賣了1張票,火車票還剩:18張
client1 has released the lock!
client4 has acquired the lock!
分布式鎖-線程:pool-1-thread-5賣了1張票,火車票還剩:17張
client4 has released the lock!
client0 has acquired the lock!
分布式鎖-線程:pool-1-thread-1賣了1張票,火車票還剩:16張
client0 has released the lock!
client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:15張
client2 has released the lock!
client3 has acquired the lock!
分布式鎖-線程:pool-1-thread-4賣了1張票,火車票還剩:14張
client3 has released the lock!
client1 has acquired the lock!
分布式鎖-線程:pool-1-thread-2賣了1張票,火車票還剩:13張
client1 has released the lock!
client4 has acquired the lock!
分布式鎖-線程:pool-1-thread-5賣了1張票,火車票還剩:12張
client4 has released the lock!
client0 has acquired the lock!
分布式鎖-線程:pool-1-thread-1賣了1張票,火車票還剩:11張
client0 has released the lock!
client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:10張
client2 has released the lock!
client3 has acquired the lock!
分布式鎖-線程:pool-1-thread-4賣了1張票,火車票還剩:9張
client3 has released the lock!
client1 has acquired the lock!
分布式鎖-線程:pool-1-thread-2賣了1張票,火車票還剩:8張
client1 has released the lock!
client4 has acquired the lock!
分布式鎖-線程:pool-1-thread-5賣了1張票,火車票還剩:7張
client4 has released the lock!
client0 has acquired the lock!
分布式鎖-線程:pool-1-thread-1賣了1張票,火車票還剩:6張
client0 has released the lock!
client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:5張
client2 has released the lock!
client3 has acquired the lock!
分布式鎖-線程:pool-1-thread-4賣了1張票,火車票還剩:4張
client3 has released the lock!
client1 has acquired the lock!
分布式鎖-線程:pool-1-thread-2賣了1張票,火車票還剩:3張
client1 has released the lock!
client4 has acquired the lock!
分布式鎖-線程:pool-1-thread-5賣了1張票,火車票還剩:2張
client4 has released the lock!
client0 has acquired the lock!
分布式鎖-線程:pool-1-thread-1賣了1張票,火車票還剩:1張
client0 has released the lock!
client2 has acquired the lock!
分布式鎖-線程:pool-1-thread-3賣了1張票,火車票還剩:0張
client2 has released the lock!

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI