您好,登錄后才能下訂單哦!
zookeeper中怎么實現分布式鎖,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
利用zk的臨時有序節點機制實現分布式鎖
zk節點的四種類型:持久節點、持久順序節點、臨時節點、臨時順序節點。
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) + "張"); } }
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(); } } }
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){ } } }
<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>
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!
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。