zookeeper中怎么实现分布式锁
zookeeper中怎么实现分布式锁,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
网站设计制作过程拒绝使用模板建站;使用PHP+MYSQL原生开发可交付网站源代码;符合网站优化排名的后台管理系统;网站制作、成都网站制作收费合理;免费进行网站备案等企业网站建设一条龙服务.我们是一家持续稳定运营了10余年的成都创新互联公司网站建设公司。
一、理论基础
利用zk的临时有序节点机制实现分布式锁
zk节点的四种类型:持久节点、持久顺序节点、临时节点、临时顺序节点。
二、代码实现
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; Callabletask = new Callable (){ 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
org.apache.curator curator-recipes 4.0.0 org.apache.curator curator-framework 4.0.0 org.apache.zookeeper zookeeper 3.4.10
5、关键点
① 客户端端口即将断开连接
② 断开连接后临时节点消失
6、测试结果
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!
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
文章标题:zookeeper中怎么实现分布式锁
转载源于:http://myzitong.com/article/ihpods.html