Curator Zookeeper分布式锁
Curator Zookeeper分布式锁
pom.xml中添加如下配置
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>2.10.0</version>
</dependency>
zookeeper配置
下载zookeeper并解压至D:\java\zookeeper-3.4.6
:
http://www.eu.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
zookeeper配置文件:
zoo-1.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=D:/java/zookeeper-3.4.6/data/1
#日志位置
dataLogDir=D:/java/zookeeper-3.4.6/log/1
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http:/zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889
zoo-2.cfg
和zoo-3.cfg
修改如下配置并创建相应的目录
修改clientPort:
zoo-1.cfg:clientPort=2181
zoo-2.cfg:clientPort=2182
zoo-3.cfg:clientPort=2183
创建目录:
zoo-1.cfg:D:/java/zookeeper-3.4.6/data/1
zoo-2.cfg:D:/java/zookeeper-3.4.6/data/2
zoo-3.cfg:D:/java/zookeeper-3.4.6/data/3
分别创建文件:myid
,内容分别为各自的id:1、2和3
D:/java/zookeeper-3.4.6/data/1/myid:1
D:/java/zookeeper-3.4.6/data/2/myid:2
D:/java/zookeeper-3.4.6/data/3/myid:3
分别自动各个zookeeper实例
代码测试
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;import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;public class CuratorLockTest {public static void main(String[] args) throws InterruptedException {CountDownLatch latch = new CountDownLatch(5);String zookeeperConnectionString = "localhost:2181,localhost:2182,localhost:2183";RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);client.start();System.out.println("客户端启动。。。。");ExecutorService exec = Executors.newCachedThreadPool();for (int i = 0; i < 5; i++) {exec.submit(new MyLock("client" + i, client, latch));}exec.shutdown();latch.await();System.out.println("所有任务执行完毕");client.close();System.out.println("客户端关闭。。。。");}static class MyLock implements Runnable {private String name;private CuratorFramework client;private CountDownLatch latch;public MyLock(String name, CuratorFramework client, CountDownLatch latch) {this.name = name;this.client = client;this.latch = latch;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void run() {InterProcessMutex lock = new InterProcessMutex(client, "/test_group");try {System.out.println("------" + this.name + "---------等待获取锁。--------");if (lock.acquire(120, TimeUnit.SECONDS)) {try {System.out.println("----------" + this.name + "获得资源----------");System.out.println("----------" + this.name + "正在处理资源----------");Thread.sleep(10 * 1000);System.out.println("----------" + this.name + "资源使用完毕----------");latch.countDown();} finally {lock.release();System.out.println("----------" + this.name + "释放----------");}}} catch (Exception e) {e.printStackTrace();}}}
}
运行结果:
客户端启动。。。。
------client1---------等待获取锁。--------
------client2---------等待获取锁。--------
------client0---------等待获取锁。--------
------client4---------等待获取锁。--------
------client3---------等待获取锁。--------
----------client1获得资源----------
----------client1正在处理资源----------
----------client1资源使用完毕----------
----------client1释放----------
----------client3获得资源----------
----------client3正在处理资源----------
----------client3资源使用完毕----------
----------client3释放----------
----------client0获得资源----------
----------client0正在处理资源----------
----------client0资源使用完毕----------
----------client0释放----------
----------client4获得资源----------
----------client4正在处理资源----------
----------client4资源使用完毕----------
----------client4释放----------
----------client2获得资源----------
----------client2正在处理资源----------
----------client2资源使用完毕----------
所有任务执行完毕
参考文档:
- http://blog.csdn.net/xiao_jun_0820/article/details/19110387
Curator Zookeeper分布式锁相关推荐
- 分布式锁(一) Zookeeper分布式锁
什么是Zookeeper? Zookeeper(业界简称zk)是一种提供配置管理.分布式协同以及命名的中心化服务,这些提供的功能都是分布式系统中非常底层且必不可少的基本功能,但是如果自己实现这些功能而 ...
- Zookeeper分布式锁的使用
由于公司引入了dubbo+zookeeper框架,里面不可避免的引入的zookeeper分布式锁,所以自己大致了解了一下.由于是自己研究,有不正确的地方还请大佬批评指正. 首先先介绍一下自己对zook ...
- zookeeper分布式锁原理及实现
前言 本文介绍下 zookeeper方式 实现分布式锁 原理简介 zookeeper实现分布式锁的原理就是多个节点同时在一个指定的节点下面创建临时会话顺序节点,谁创建的节点序号最小,谁就获得了锁,并且 ...
- 分布式架构-ZK客户端工具Curator框架分布式锁及基本使用
分布式架构-基于Curator分布式锁及基本使用 一.Curator Curator是Netflix公司开源的一套zookeeper客户端框架,解决了很多Zookeeper客户端非常底层的细节开发工作 ...
- Zookeeper分布式锁
原文作者:阿凡卢 出处:http://www.cnblogs.com/luxiaoxun/ 原文连接:https://www.cnblogs.com/luxiaoxun/p/4889764.html ...
- ZooKeeper - 分布式锁
与Master选举区别: 分布式锁:分布式集群服务并行,防止并发问题,同步控制 例如时间戳.(选择序号最小的) Master选举:分布式集群种选择一个提供服务,例如 双击热备方式.(抢主) 在分布式环 ...
- Zookeeper分布式锁中的羊群效应及解决方案
羊群是一种很散乱的组织,平时在一起也是盲目地左冲右撞,但一旦有一只头羊动起来,其他的羊也会不假思索地一哄而上,全然不顾前面可能有狼或者不远处有更好的草.因此,"羊群效应"就是比喻人 ...
- Zookeeper分布式锁原理
1.分布式锁介绍 单机应用开发,涉及并发同步的时候,我们往往采用synchronized 或者Lock的方式来解决多线程间的代码同步问题,这时多线程的运行都是在同一个JVM之下,没有任何问题. 但当我 ...
- Zookeeper 分布式锁
Zookeeper 分布式锁 在分布式场景中,采用传统的锁并不能解决跨进程并发的问题,所以需要引入一个分布式锁,来解决多个节点之间的访问控制 一.Zookeeper如何解决分布式锁 基于Zookeep ...
- 关于分布式锁的面试题都在这里了|Reids分布式锁|ZooKeeper分布式锁
我今天班儿都没上,就为了赶紧把这篇文章分布式锁早点写完.我真的不能再贴心了. 边喝茶边构思,你们可不要白嫖了! 三连来一遍? 引言 为什么要学习分布式锁? 最简单的理由就是作为一个社招程序员,面试的时 ...
最新文章
- 收藏 | 数据智能与计算机图形学领域2019推荐论文列表(附链接)
- Access 数据库连接字符串 (有密码)
- Angular中提示:Can't bind to 'ngModel' since it isn't a known property of 'input'
- Java-IntelliJ IDEA【@Override is not allowed when implementing interface method 解决方法】
- 【报错笔记】程序报错:Cause: java.sql.SQLException: Invalid value for getInt() - ‘4ab72edc-c02f-423f-ae9e-18c30
- 为WPF, UWP 及 Xamarin实现一个简单的消息组件
- Knowledge is Power Gym - 102822K
- java 继承示例_Java中的继承类型以及示例
- kube-scheduler 磁盘调度源码分析
- 论软件工程师的自我修养:角色、重构与质量
- 【JVM】JVM客户端 server模式 client 模式
- 在线教学视频的设计与实现
- python实现的简版iconv
- [RHEL5企业级Linux服务攻略]--第1季 Linux服务器的搭建与测试
- js base64编码_使用psd.js将PSD转成SVG -- 基础篇(文字amp;图片)
- matlab数字电路仿真,MATLAB环境下的数字电路仿真
- MATLAB中调用eemd函数
- win10系统崩溃(UNEXPECTED_STORE_EXCEPTION)解决方法
- 华为mate40pro和p40pro参数对比 华为mate40pro和p40pro哪个好
- grad-cam原理