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资源使用完毕----------
所有任务执行完毕
参考文档:
Curator Zookeeper分布式锁的更多相关文章
- Curator框架实现ZooKeeper分布式锁
排他锁(X) 这里主要讲讲分布式锁中的排他锁.排他锁(Exclusive Locks,简称X锁),又称为写锁或独占锁,是一种基本的锁类型.如果事务T1对数据对象O1加上了排他锁,那么在整个加锁期间,只 ...
- Curator实现zookeeper分布式锁的基本原理
一.写在前面 之前写过一篇文章(<拜托,面试请不要再问我Redis分布式锁的实现原理>),给大家说了一下Redisson这个开源框架是如何实现Redis分布式锁原理的,这篇文章再给大家聊一 ...
- ZooKeeper 分布式锁 Curator 源码 02:可重入锁重复加锁和锁释放
ZooKeeper 分布式锁 Curator 源码 02:可重入锁重复加锁和锁释放 前言 加锁逻辑已经介绍完毕,那当一个线程重复加锁是如何处理的呢? 锁重入 在上一小节中,可以看到加锁的过程,再回头看 ...
- ZooKeeper 分布式锁 Curator 源码 03:可重入锁并发加锁
前言 在了解了加锁和锁重入之后,最需要了解的还是在分布式场景下或者多线程并发加锁是如何处理的? 并发加锁 先来看结果,在多线程对 /locks/lock_01 加锁时,是在后面又创建了新的临时节点. ...
- ZooKeeper 分布式锁 Curator 源码 04:分布式信号量和互斥锁
前言 分布式信号量,之前在 Redisson 中也介绍过,Redisson 的信号量是将计数维护在 Redis 中的,那现在来看一下 Curator 是如何基于 ZooKeeper 实现信号量的. 使 ...
- Zookeeper分布式锁实现Curator十一问
前面我们剖析了Redisson的源码,主要分析了Redisson实现Redis分布式锁的15问,理清了Redisson是如何实现的分布式锁和一些其它的特性.这篇文章就来接着剖析Zookeeper分布式 ...
- 女朋友也能看懂的Zookeeper分布式锁原理
前言 关于分布式锁,在互联网行业的使用场景还是比较多的,比如电商的库存扣减,秒杀活动,集群定时任务执行等需要进程互斥的场景.而实现分布式锁的手段也很多,大家比较常见的就是redis跟zookeep ...
- 分布式锁(一) Zookeeper分布式锁
什么是Zookeeper? Zookeeper(业界简称zk)是一种提供配置管理.分布式协同以及命名的中心化服务,这些提供的功能都是分布式系统中非常底层且必不可少的基本功能,但是如果自己实现这些功能而 ...
- ZooKeeper 分布式锁
在Redis分布式锁一文中, 作者介绍了如何使用Redis开发分布式锁. Redis分布式锁具有轻量高吞吐量的特点,但是一致性保证较弱.我们可以使用Zookeeper开发分布式锁,来满足对高一致性的要 ...
随机推荐
- Gulp:自动化构建工具
一.介绍: gulp是一个基于流的构建工具,可以自动执行指定的任务,简洁且高效 二.优点: 开发环境下,想要能够按模块组织代码,监听实时变化 css/js预编译,postcss等方案,浏览器前缀自动补 ...
- oracle数据查询
select * from XMBL_EM_DBBACK where f_djbh='DB01201612270013'select * from flow_task_list where biz_d ...
- openvpn 启动
安装 yum -y install openvpn 配置文件可以放在: /etc/openvpn 例如,我这里的路径: [mslagee@centos-dev ~]$ cd /etc/openvpn/ ...
- mysql更新密码
mysql -u root mysql> use mysql; mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE us ...
- CodeForces - 453A Little Pony and Expected Maximum
http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...
- py-faster-rcnn几个辅助脚本
py-faster-rcnn本身代码很棒. 不过使用它的时候,还是需要自己写一些脚本,作为辅助. 1 所有.py文件顶部添加utf8编码声明.因为有时候需要添加中文注释,不声明编码会报错 #inser ...
- Redis之个人简单理解
1.什么是redis? 在过去的几年中,NoSQL数据库一度成为高并发.海量数据存储解决方案的代名词,与之相应的产品也呈现出雨后春笋般的生机.然而在众多产品中能够脱颖而出的却屈指可数,如Redis.M ...
- posgresql
ubuntu下 修改postgres数据库用户的密码为123456 sudo -u postgres psql postgres=# ALTER USER postgres WITH PASSWORD ...
- easyUI时间控件 使用
1,时间格式化,分隔符为 “—” // 日期格式化 function myformatter(date){ var y = date.getFullYear(); var m = date.g ...
- php-7.1.0 rpm包制作
nginx-1.8.0 rpm包制作见上篇文章:http://www.cnblogs.com/xiaoming279/p/6251149.html spec文件 Name: php Version: ...