您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。