当前位置:   article > 正文

zookeeper分布式锁实例_zk的分布式所代码实例

zk的分布式所代码实例
  1. <dependency>
  2. <groupId>com.101tec</groupId>
  3. <artifactId>zkclient</artifactId>
  4. <version>0.10</version>
  5. </dependency>
  1. 如果有一把锁,被多个人给竞争,此时多个人会排队,第一个拿到锁的人会执行,然后释放锁;后面的每个人都会去监听**排在自己前面**的那个人创建的 node 上,一旦某个人释放了锁,排在自己后面的人就会被 zookeeper 给通知,一旦被通知了之后,就 ok 了,自己就获取到了锁,就可以执行代码了。
  2. ```java
  3. public class ZooKeeperDistributedLock {
  4. //private ZooKeeper zk;
  5. private ZkClient zk;
  6. private String locksRoot = "/locks";
  7. private String productId;
  8. private String waitNode;
  9. private String lockNode;
  10. private CountDownLatch latch;
  11. private int sessionTimeout = 30000;
  12. public ZooKeeperDistributedLock() {
  13. try {
  14. String address="192.168.31.187:2181,192.168.31.19:2181,192.168.31.227:2181";
  15. zk = new ZkClient(address, sessionTimeout, sessionTimeout);
  16. } catch (IOException e) {
  17. throw new LockException(e);
  18. } catch (KeeperException e) {
  19. throw new LockException(e);
  20. } catch (InterruptedException e) {
  21. throw new LockException(e);
  22. }
  23. }
  24. public void acquireDistributedLock() {
  25. try {
  26. if (this.tryLock()) {
  27. return;
  28. } else {
  29. waitForLock(waitNode, sessionTimeout);
  30. acquireDistributedLock();
  31. }
  32. } catch (KeeperException e) {
  33. throw new LockException(e);
  34. } catch (InterruptedException e) {
  35. throw new LockException(e);
  36. }
  37. }
  38. public boolean tryLock() {
  39. try {
  40. lockNode = this.client.createEphemeralSequential(locksRoot+ "/", "1");
  41. // 看看刚创建的节点是不是最小的节点
  42. // locks:100000000001000000000110000000002
  43. List<String> locks = zk.getChildren(locksRoot);
  44. Collections.sort(locks);
  45. if(lockNode.equals(locksRoot+"/"+ locks.get(0))){
  46. System.out.println(Thread.currentThread().getName() + ":获取锁成功");
  47. //如果是最小的节点,则表示取得锁
  48. return true;
  49. }
  50. //如果不是最小的节点,找到比自己小1的节点
  51. int previousLockIndex = -1;
  52. for(int i = 0; i < locks.size(); i++) {
  53. if(lockNode.equals(locksRoot +/+ locks.get(i))) {
  54. previousLockIndex = i - 1;
  55. break;
  56. }
  57. }
  58. this.waitNode = locks.get(previousLockIndex);
  59. } catch (KeeperException e) {
  60. throw new LockException(e);
  61. } catch (InterruptedException e) {
  62. throw new LockException(e);
  63. }
  64. return false;
  65. }
  66. private boolean waitForLock(String waitNode, long waitTime) throws
  67. InterruptedException, KeeperException {
  68. Stat stat = zk.exists(locksRoot + "/" + waitNode, true);
  69. this.latch = new CountDownLatch(1);
  70. IZkDataListener listener = new IZkDataListener() {
  71. @Override
  72. public void handleDataChange(String arg0, Object arg1) throws Exception {
  73. }
  74. @Override
  75. public void handleDataDeleted(String arg0)
  76. throws Exception {
  77. System.out.println("节点被删除了,开始抢锁");
  78. cdl.countDown();
  79. }
  80. };
  81. // 完成watcher注册
  82. this.client.subscribeDataChanges(beforePath, listener);
  83. if (stat != null) {
  84. this.latch.await(waitTime, TimeUnit.MILLISECONDS);
  85. this.latch = null;
  86. }
  87. // 取消注册
  88. this.client.unsubscribeDataChanges(beforePath, listener);
  89. return true;
  90. }
  91. public void unlock() {
  92. try {
  93. // 删除/locks/10000000000节点
  94. // 删除/locks/10000000001节点
  95. System.out.println("unlock " + lockNode);
  96. zk.delete(lockNode);
  97. System.out.println(Thread.currentThread().getName()
  98. + "所有锁释放成功:删除zk节点");
  99. lockNode = null;
  100. zk.close();
  101. } catch (InterruptedException e) {
  102. e.printStackTrace();
  103. } catch (KeeperException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. public class LockException extends RuntimeException {
  108. private static final long serialVersionUID = 1L;
  109. public LockException(String e) {
  110. super(e);
  111. }
  112. public LockException(Exception e) {
  113. super(e);
  114. }
  115. }
  116. }
  1. public class OrderCodeGenerator {
  2. private int i = 0;
  3. public String getOrderCode() {
  4. Date now = new Date();
  5. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-");
  6. return sdf.format(now) + ++i;
  7. }
  8. }
  1. public class OrderServiceImplWithZkDis implements OrderService {
  2. private static OrderCodeGenerator org = new OrderCodeGenerator();
  3. private Lock lock = new ZooKeeperDistributedLock();
  4. @Override
  5. public void createOrder() {
  6. String orderCode = null;
  7. try {
  8. lock.acquireDistributedLock();
  9. orderCode = org.getOrderCode();
  10. //TestReLock();
  11. System.out.println(Thread.currentThread().getName() + "生成订单:" + orderCode);
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. } finally {
  15. lock.unlock();
  16. }
  17. }
  18. /**
  19. public void TestReLock() {
  20. lock.acquireDistributedLock();
  21. System.out.println(Thread.currentThread().getName() + "测试重入锁成功...");
  22. lock.unlock();
  23. }
  24. **/
  25. public static void main(String[] args) {
  26. int num = 5;
  27. CyclicBarrier cyclicBarrier = new CyclicBarrier(num);
  28. for (int i = 0; i < num; i++) {
  29. new Thread(new Runnable() {
  30. @Override
  31. public void run() {
  32. OrderService orderService = new OrderServiceImplWithZkDis();
  33. System.out.println(Thread.currentThread().getName() + ": 我准备好了");
  34. try {
  35. cyclicBarrier.await();
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. orderService.createOrder();
  40. }
  41. }).start();
  42. }
  43. }
  44. }
  1. Thread-2:我准备好了
  2. Thread-3:我准备好了
  3. Thread-1:我准备好了
  4. Thread-5:我准备好了
  5. Thread-4:我准备好了
  6. Thread-4:获取锁成功
  7. Thread-4生成订单:2018-07-12-13-12-09-1
  8. Thread-4所有锁释放成功:删除ZK节点
  9. 节点删除了,开始抢锁
  10. Thread-5:获取锁成功
  11. Thread-5生成订单:2018-07-12-13-12-11-2
  12. Thread-5所有锁释放成功:删除ZK节点
  13. 节点删除了,开始抢锁
  14. Thread-2:获取锁成功
  15. Thread-2生成订单:2018-07-12-13-12-14-3
  16. Thread-2所有锁释放成功:删除ZK节点
  17. 节点删除了,开始抢锁
  18. Thread-3:获取锁成功
  19. Thread-3生成订单:2018-07-12-13-12-17-4
  20. Thread-3所有锁释放成功:删除ZK节点
  21. 节点删除了,开始抢锁
  22. Thread-1:获取锁成功
  23. Thread-1生成订单:2018-07-12-13-12-21-5
  24. Thread-1所有锁释放成功:删除ZK节点
  25. 节点删除了,开始抢锁

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/965764
推荐阅读
相关标签
  

闽ICP备14008679号