世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序不断追求完美的过程。

分布式锁的原理其实很简单,当然从根本上说所有锁的实现原理其实是一致的,无论多么复杂的锁,底层都是CAS。这里遵循CAS的原则实现了一个zookeeper的分布式锁。

public class ZKTest {public static void main(String[] args) {for (int i=0; i<8; i++) {new Thread(ZKTest::run).start();}}private static void run() {String serviceId = serviceId();ZooKeeper zk = zk();boolean lock = getLock(zk, serviceId);if (lock) {for (int i = 0; i < 100; i++) {System.out.println(Thread.currentThread().getId() + " " + i);}}close(zk);}private static void close(ZooKeeper zk) {try {zk.close();} catch (Exception e) {e.printStackTrace();}}private static boolean getLock(ZooKeeper zk, String serviceId) {while (!isLeader(zk, serviceId)) {toBeLeader(zk, serviceId);}return true;}private static String serviceId() {return Long.toString(new Random().nextLong());}private static ZooKeeper zk() {try {ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 15000, new Watcher() {@Overridepublic void process(WatchedEvent event) {if (!Event.EventType.None.equals(event.getType())) {System.out.println("watchEvent : " + event);}}});return zk;} catch (Exception e) {e.printStackTrace();return null;}}private static final String MASTER = "/master";private static boolean isLeader(ZooKeeper zk, String serviceId) {try {byte[] data = zk.getData(MASTER, true, new Stat());if (null != data && new String(data).equals(serviceId)) {return true;} else {return false;}} catch (Exception e) {return false;}}private static boolean toBeLeader(ZooKeeper zk, String serviceId) {try {String path = zk.create(MASTER, serviceId.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);System.out.println("path : " + path);if (MASTER.equals(path)) {return true;} else {return false;}} catch (Exception e) {return false;}}
}

输出结果:

threadId : 18 no : 0
threadId : 18 no : 1
threadId : 18 no : 2
threadId : 18 no : 3
threadId : 18 no : 4
threadId : 18 no : 5
threadId : 18 no : 6
threadId : 18 no : 7
threadId : 18 no : 8
threadId : 18 no : 9
threadId : 18 no : 10
threadId : 18 no : 11
threadId : 18 no : 12
threadId : 18 no : 13
threadId : 18 no : 14
threadId : 18 no : 15
threadId : 18 no : 16
threadId : 18 no : 17
threadId : 18 no : 18
threadId : 18 no : 19
threadId : 18 no : 20
threadId : 18 no : 21
threadId : 18 no : 22
threadId : 18 no : 23
threadId : 18 no : 24
threadId : 18 no : 25
threadId : 18 no : 26
threadId : 18 no : 27
threadId : 18 no : 28
threadId : 18 no : 29
threadId : 18 no : 30
threadId : 18 no : 31
threadId : 18 no : 32
threadId : 18 no : 33
threadId : 18 no : 34
threadId : 18 no : 35
threadId : 18 no : 36
threadId : 18 no : 37
threadId : 18 no : 38
threadId : 18 no : 39
threadId : 18 no : 40
threadId : 18 no : 41
threadId : 18 no : 42
threadId : 18 no : 43
threadId : 18 no : 44
threadId : 18 no : 45
threadId : 18 no : 46
threadId : 18 no : 47
threadId : 18 no : 48
threadId : 18 no : 49
threadId : 18 no : 50
threadId : 18 no : 51
threadId : 18 no : 52
threadId : 18 no : 53
threadId : 18 no : 54
threadId : 18 no : 55
threadId : 18 no : 56
threadId : 18 no : 57
threadId : 18 no : 58
threadId : 18 no : 59
threadId : 18 no : 60
threadId : 18 no : 61
threadId : 18 no : 62
threadId : 18 no : 63
threadId : 18 no : 64
threadId : 18 no : 65
threadId : 18 no : 66
threadId : 18 no : 67
threadId : 18 no : 68
threadId : 18 no : 69
threadId : 18 no : 70
threadId : 18 no : 71
threadId : 18 no : 72
threadId : 18 no : 73
threadId : 18 no : 74
threadId : 18 no : 75
threadId : 18 no : 76
threadId : 18 no : 77
threadId : 18 no : 78
threadId : 18 no : 79
threadId : 18 no : 80
threadId : 18 no : 81
threadId : 18 no : 82
threadId : 18 no : 83
threadId : 18 no : 84
threadId : 18 no : 85
threadId : 18 no : 86
threadId : 18 no : 87
threadId : 18 no : 88
threadId : 18 no : 89
threadId : 18 no : 90
threadId : 18 no : 91
threadId : 18 no : 92
threadId : 18 no : 93
threadId : 18 no : 94
threadId : 18 no : 95
threadId : 18 no : 96
threadId : 18 no : 97
threadId : 18 no : 98
threadId : 18 no : 99threadId : 21 no : 0
threadId : 21 no : 1
threadId : 21 no : 2
threadId : 21 no : 3
threadId : 21 no : 4
threadId : 21 no : 5
threadId : 21 no : 6
threadId : 21 no : 7
threadId : 21 no : 8
threadId : 21 no : 9
threadId : 21 no : 10
threadId : 21 no : 11
threadId : 21 no : 12
threadId : 21 no : 13
threadId : 21 no : 14
threadId : 21 no : 15
threadId : 21 no : 16
threadId : 21 no : 17
threadId : 21 no : 18
threadId : 21 no : 19
threadId : 21 no : 20
threadId : 21 no : 21
threadId : 21 no : 22
threadId : 21 no : 23
threadId : 21 no : 24
threadId : 21 no : 25
threadId : 21 no : 26
threadId : 21 no : 27
threadId : 21 no : 28
threadId : 21 no : 29
threadId : 21 no : 30
threadId : 21 no : 31
threadId : 21 no : 32
threadId : 21 no : 33
threadId : 21 no : 34
threadId : 21 no : 35
threadId : 21 no : 36
threadId : 21 no : 37
threadId : 21 no : 38
threadId : 21 no : 39
threadId : 21 no : 40
threadId : 21 no : 41
threadId : 21 no : 42
threadId : 21 no : 43
threadId : 21 no : 44
threadId : 21 no : 45
threadId : 21 no : 46
threadId : 21 no : 47
threadId : 21 no : 48
threadId : 21 no : 49
threadId : 21 no : 50
threadId : 21 no : 51
threadId : 21 no : 52
threadId : 21 no : 53
threadId : 21 no : 54
threadId : 21 no : 55
threadId : 21 no : 56
threadId : 21 no : 57
threadId : 21 no : 58
threadId : 21 no : 59
threadId : 21 no : 60
threadId : 21 no : 61
threadId : 21 no : 62
threadId : 21 no : 63
threadId : 21 no : 64
threadId : 21 no : 65
threadId : 21 no : 66
threadId : 21 no : 67
threadId : 21 no : 68
threadId : 21 no : 69
threadId : 21 no : 70
threadId : 21 no : 71
threadId : 21 no : 72
threadId : 21 no : 73
threadId : 21 no : 74
threadId : 21 no : 75
threadId : 21 no : 76
threadId : 21 no : 77
threadId : 21 no : 78
threadId : 21 no : 79
threadId : 21 no : 80
threadId : 21 no : 81
threadId : 21 no : 82
threadId : 21 no : 83
threadId : 21 no : 84
threadId : 21 no : 85
threadId : 21 no : 86
threadId : 21 no : 87
threadId : 21 no : 88
threadId : 21 no : 89
threadId : 21 no : 90
threadId : 21 no : 91
threadId : 21 no : 92
threadId : 21 no : 93
threadId : 21 no : 94
threadId : 21 no : 95
threadId : 21 no : 96
threadId : 21 no : 97
threadId : 21 no : 98
threadId : 21 no : 99....................

由于zookeeper是全局一致的,所以,本程序无论是在单个服务器上,还是在集群中效果是一致的,都可以保证线程安全。

zk - zookeeper实现分布式锁代码相关推荐

  1. 面试官:ZK(ZooKeeper)分布式锁实现,你了解了吗?

    准备 本文会使用到 三台 独立服务器,可以自行提前搭建好. 不知道如何搭建的,可以看我之前 ZooKeeper集群 搭建:Zookeeper 集群部署的那些事儿 关于ZooKeeper 一些基础命令可 ...

  2. ZK(ZooKeeper)分布式锁实现

    点赞再看,养成习惯,微信搜索[牧小农]关注我获取更多资讯,风里雨里,小农等你. 本文中案例都会在上传到git上,请放心浏览 git地址:https://github.com/muxiaonong/Zo ...

  3. Zookeeper系列四:Zookeeper实现分布式锁、Zookeeper实现配置中心

    一.Zookeeper实现分布式锁 分布式锁主要用于在分布式环境中保证数据的一致性. 包括跨进程.跨机器.跨网络导致共享资源不一致的问题. 1. 分布式锁的实现思路 说明: 这种实现会有一个缺点,即当 ...

  4. ieee39节点系统介绍_Java秒杀系统实战系列-基于ZooKeeper的分布式锁优化秒杀逻辑...

    本文是"Java秒杀系统实战系列文章"的第十六篇,本文我们将继续秒杀系统的优化之路,采用统一协调调度中心中间件ZooKeeper控制秒杀系统中高并发多线程对于共享资源~代码块的并发 ...

  5. 【Zookeeper】基于Zookeeper实现分布式锁

    1.概述 转载:基于Zookeeper实现分布式锁 1.1 为什么使用分布式锁 我们在开发应用的时候,如果需要对某一个共享变量进行多线程同步访问的时候,我们往往采用synchronized或者Lock ...

  6. 基于Zookeeper的分布式锁

    实现分布式锁目前有三种流行方案,分别为基于数据库.Redis.Zookeeper的方案,其中前两种方案网络上有很多资料可以参考,本文不做展开.我们来看下使用Zookeeper如何实现分布式锁. 什么是 ...

  7. 基于ZooKeeper的分布式锁和队列

    分布式锁的几种实现: 1.zookeeper分布式锁,基于自增节点 2.Redis分布式锁,基于setnx命令, 基于Redis实现分布式锁:http://blog.csdn.net/daiyudon ...

  8. 利用Zookeeper实现 - 分布式锁

    微信原文: 利用Zookeeper实现 - 分布式锁 博客原文:利用Zookeeper实现 - 分布式锁 在许多场景中,数据一致性是一个比较重要的话题,在单机环境中,我们可以通过Java提供的并发AP ...

  9. redis和zookeeper实现分布式锁的区别

    Redis实现分布式锁 1.根据lockKey区进行setnx(set not exist,如果key值为空,则正常设置,返回1,否则不会进行设置并返回0)操作,如果设置成功,表示已经获得锁,否则并没 ...

最新文章

  1. 互联网技术的技术名词
  2. linux make 命令简介
  3. 消息通信库ZeroMQ 4.0.4安装指南
  4. 从mysql到大数据(一)--开宗明义
  5. 苏宁双11战报:0点~1点 线上订单同比增72%
  6. 【分布式】分布式架构-ESB SOA
  7. ps字体识别_秒抠毛发,去除海报字体……解锁PS的3个隐藏工具
  8. Npm安装node-sass包依赖时报错 Cannot download “https://github.com/sass/node-sass/releases/download
  9. windows10系统如何设置标题栏显色
  10. 洛谷—— P1268 树的重量
  11. BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索
  12. Cloudera Manager 安装 CDH5.x 心得
  13. Hadoop2.7.4 HA centos6.8
  14. c4dr20怎么安装oc渲染器怎么安装_[C4D插件] OTOY正式发布OC渲染器OctaneRender4 For C4D 支持R16-R20 Demo版已开放下载(Win)...
  15. linux4 系统下载,syslinux下载|
  16. C++中不能重载的运算符
  17. 长沙“一江两岸”新网红,看铜官古镇的“文和游”
  18. 基于劈窗算法的地表温度反演算法
  19. 音频编解码标准G.711与G.729
  20. Linux系统常用命令:CentOS,RedHat包的安装和卸载

热门文章

  1. 完美解决序微信小程序不能用本地ip调试的问题,不在以下 request 合法域名列表中,请参考文档:https://d
  2. CPU系统级验证——测试激励——imperas公司riscvOVPsimPlus文件分析
  3. Django打造大型企业官网-项目实战(四)
  4. CEF Debug模式运行打开网页白屏
  5. OpenCV~图像深度
  6. 会计凭证抬头文本增强的问题
  7. 2013.6.3 正能量
  8. 算法之动态规划算法简介
  9. 微信摇一摇(copy的)
  10. ldap端口超时设置_ldap安装配置