zookeeper分布式锁实现秒杀案例(jfinal框架)

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

ZooKeeper的架构通过冗余服务实现高可用性。因此,如果第一次无应答,客户端就可以询问另一台ZooKeeper主机。ZooKeeper节点将它们的数据存储于一个分层的命名空间,非常类似于一个文件系统或一个前缀树结构。客户端可以在节点读写,从而以这种方式拥有一个共享的配置服务。更新是全序的。

基于ZooKeeper分布式锁的流程

在zookeeper指定节点(locks)下创建临时顺序节点node_n

获取locks下所有子节点children

对子节点按节点自增序号从小到大排序

判断本节点是不是第一个子节点,若是,则获取锁;若不是,则监听比该节点小的那个节点的删除事件

若监听事件生效,则回到第二步重新进行判断,直到获取到锁

具体实现

下面就具体使用java和zookeeper实现分布式锁,操作zookeeper使用的是apache提供的zookeeper的包。


通过实现Watch接口,实现process(WatchedEvent event)方法来实施监控,使CountDownLatch来完成监控,在等待锁的时候使用CountDownLatch来计数,等到后进行countDown,停止等待,继续运行。

以下整体流程基本与上述描述流程一致,只是在监听的时候使用的是CountDownLatch来监听前一个节点。


下边来看简单的实现源码,不用在乎代码的质量,这里只是简单地做个演示!!!

分布式锁:

import org.apache.zookeeper.*;

import org.apache.zookeeper.data.Stat;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

/**

* Created by liuyang on 2017/4/20.

*/

public class DistributedLock implements Lock, Watcher {

    private ZooKeeper zk = null;

    // 根节点

    private String ROOT_LOCK = "/locks";

    // 竞争的资源

    private String lockName;

    // 等待的前一个锁

    private String WAIT_LOCK;

    // 当前锁

    private String CURRENT_LOCK;

    // 计数器

    private CountDownLatch countDownLatch;

    private int sessionTimeout = 30000;

    private List<Exception> exceptionList = new ArrayList<Exception>();

    /**

    * 配置分布式锁

    * @param config 连接的url

    * @param lockName 竞争资源

    */

    public DistributedLock(String config, String lockName) {

        this.lockName = lockName;

        try {

            // 连接zookeeper

            zk = new ZooKeeper(config, sessionTimeout, this);

            Stat stat = zk.exists(ROOT_LOCK, false);

            if (stat == null) {

                // 如果根节点不存在,则创建根节点

                zk.create(ROOT_LOCK, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            }

        } catch (IOException e) {

            e.printStackTrace();

        } catch (InterruptedException e) {

            e.printStackTrace();

        } catch (KeeperException e) {

            e.printStackTrace();

        }

    }

    // 节点监视器

    public void process(WatchedEvent event) {

        if (this.countDownLatch != null) {

            this.countDownLatch.countDown();

        }

    }

    public void lock() {

        if (exceptionList.size() > 0) {

            throw new LockException(exceptionList.get(0));

        }

        try {

            if (this.tryLock()) {

                System.out.println(Thread.currentThread().getName() + " " + lockName + "获得了锁");

                return;

            } else {

                // 等待锁

                waitForLock(WAIT_LOCK, sessionTimeout);

            }

        } catch (InterruptedException e) {

            e.printStackTrace();

        } catch (KeeperException e) {

            e.printStackTrace();

        }

    }

    public boolean tryLock() {

        try {

            String splitStr = "_lock_";

            if (lockName.contains(splitStr)) {

                throw new LockException("锁名有误");

            }

            // 创建临时有序节点

            CURRENT_LOCK = zk.create(ROOT_LOCK + "/" + lockName + splitStr, new byte[0],

                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);

            System.out.println(CURRENT_LOCK + " 已经创建");

            // 取所有子节点

            List<String> subNodes = zk.getChildren(ROOT_LOCK, false);

            // 取出所有lockName的锁

            List<String> lockObjects = new ArrayList<String>();

            for (String node : subNodes) {

                String _node = node.split(splitStr)[0];

                if (_node.equals(lockName)) {

                    lockObjects.add(node);

                }

            }

            Collections.sort(lockObjects);

            System.out.println(Thread.currentThread().getName() + " 的锁是 " + CURRENT_LOCK);

            // 若当前节点为最小节点,则获取锁成功

            if (CURRENT_LOCK.equals(ROOT_LOCK + "/" + lockObjects.get(0))) {

                return true;

            }

            // 若不是最小节点,则找到自己的前一个节点

            String prevNode = CURRENT_LOCK.substring(CURRENT_LOCK.lastIndexOf("/") + 1);

            WAIT_LOCK = lockObjects.get(Collections.binarySearch(lockObjects, prevNode) - 1);

        } catch (InterruptedException e) {

            e.printStackTrace();

        } catch (KeeperException e) {

            e.printStackTrace();

        }

        return false;

    }

    public boolean tryLock(long timeout, TimeUnit unit) {

        try {

            if (this.tryLock()) {

                return true;

            }

            return waitForLock(WAIT_LOCK, timeout);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return false;

    }

    // 等待锁

    private boolean waitForLock(String prev, long waitTime) throws KeeperException, InterruptedException {

        Stat stat = zk.exists(ROOT_LOCK + "/" + prev, true);

        if (stat != null) {

            System.out.println(Thread.currentThread().getName() + "等待锁 " + ROOT_LOCK + "/" + prev);

            this.countDownLatch = new CountDownLatch(1);

            // 计数等待,若等到前一个节点消失,则precess中进行countDown,停止等待,获取锁

            this.countDownLatch.await(waitTime, TimeUnit.MILLISECONDS);

            this.countDownLatch = null;

            System.out.println(Thread.currentThread().getName() + " 等到了锁");

        }

        return true;

    }

    public void unlock() {

        try {

            System.out.println("释放锁 " + CURRENT_LOCK);

            zk.delete(CURRENT_LOCK, -1);

            CURRENT_LOCK = null;

            zk.close();

        } catch (InterruptedException e) {

            e.printStackTrace();

        } catch (KeeperException e) {

            e.printStackTrace();

        }

    }

    public Condition newCondition() {

        return null;

    }

    public void lockInterruptibly() throws InterruptedException {

        this.lock();

    }

    public class LockException extends RuntimeException {

        private static final long serialVersionUID = 1L;

        public LockException(String e){

            super(e);

        }

        public LockException(Exception e){

            super(e);

        }

    }

}

入口类:

39 号人 获取锁:/locks/test1_lock_0000001385 抢到了第1 件商品;

释放锁 /locks/test1_lock_0000001385

35 号人 获取锁:/locks/test1_lock_0000001386 抢到了第2 件商品;

释放锁 /locks/test1_lock_0000001386

25 号人 获取锁:/locks/test1_lock_0000001387 抢到了第3 件商品;

释放锁 /locks/test1_lock_0000001387

42 号人 获取锁:/locks/test1_lock_0000001388 抢到了第4 件商品;

释放锁 /locks/test1_lock_0000001388

19 号人 获取锁:/locks/test1_lock_0000001389 抢到了第5 件商品;

释放锁 /locks/test1_lock_0000001389

38 号人 获取锁:/locks/test1_lock_0000001390 抢到了第6 件商品;

释放锁 /locks/test1_lock_0000001390

22 号人 获取锁:/locks/test1_lock_0000001391 抢到了第7 件商品;

释放锁 /locks/test1_lock_0000001391

18 号人 获取锁:/locks/test1_lock_0000001392 抢到了第8 件商品;

释放锁 /locks/test1_lock_0000001392

27 号人 获取锁:/locks/test1_lock_0000001393 抢到了第9 件商品;

释放锁 /locks/test1_lock_0000001393

28 号人 获取锁:/locks/test1_lock_0000001394 抢到了第10 件商品;

释放锁 /locks/test1_lock_0000001394

2 号人 获取锁:/locks/test1_lock_0000001395 抢到了第11 件商品;

释放锁 /locks/test1_lock_0000001395

14 号人 获取锁:/locks/test1_lock_0000001396 抢到了第12 件商品;

释放锁 /locks/test1_lock_0000001396

20 号人 获取锁:/locks/test1_lock_0000001397 抢到了第13 件商品;

释放锁 /locks/test1_lock_0000001397

11 号人 获取锁:/locks/test1_lock_0000001398 抢到了第14 件商品;

释放锁 /locks/test1_lock_0000001398

6 号人 获取锁:/locks/test1_lock_0000001399 抢到了第15 件商品;

释放锁 /locks/test1_lock_0000001399

7 号人 获取锁:/locks/test1_lock_0000001400 抢到了第16 件商品;

释放锁 /locks/test1_lock_0000001400

29 号人 获取锁:/locks/test1_lock_0000001401 抢到了第17 件商品;

释放锁 /locks/test1_lock_0000001401

3 号人 获取锁:/locks/test1_lock_0000001402 抢到了第18 件商品;

释放锁 /locks/test1_lock_0000001402

8 号人 获取锁:/locks/test1_lock_0000001403 抢到了第19 件商品;

释放锁 /locks/test1_lock_0000001403

32 号人 获取锁:/locks/test1_lock_0000001404 抢到了第20 件商品;

释放锁 /locks/test1_lock_0000001404

33 号人 获取锁:/locks/test1_lock_0000001405 抢到了第21 件商品;

释放锁 /locks/test1_lock_0000001405

23 号人 获取锁:/locks/test1_lock_0000001406 抢到了第22 件商品;

释放锁 /locks/test1_lock_0000001406

12 号人 获取锁:/locks/test1_lock_0000001407 抢到了第23 件商品;

释放锁 /locks/test1_lock_0000001407

15 号人 获取锁:/locks/test1_lock_0000001408 抢到了第24 件商品;

释放锁 /locks/test1_lock_0000001408

13 号人 获取锁:/locks/test1_lock_0000001409 抢到了第25 件商品;

释放锁 /locks/test1_lock_0000001409

34 号人 获取锁:/locks/test1_lock_0000001410 抢到了第26 件商品;

释放锁 /locks/test1_lock_0000001410

41 号人 获取锁:/locks/test1_lock_0000001411 抢到了第27 件商品;

释放锁 /locks/test1_lock_0000001411

37 号人 获取锁:/locks/test1_lock_0000001412 抢到了第28 件商品;

释放锁 /locks/test1_lock_0000001412

1 号人 获取锁:/locks/test1_lock_0000001413 抢到了第29 件商品;

释放锁 /locks/test1_lock_0000001413

52 号人 获取锁:/locks/test1_lock_0000001414 抢到了第30 件商品;

释放锁 /locks/test1_lock_0000001414

56 号人 获取锁:/locks/test1_lock_0000001415 抢到了第31 件商品;

释放锁 /locks/test1_lock_0000001415

26 号人 获取锁:/locks/test1_lock_0000001416 抢到了第32 件商品;

释放锁 /locks/test1_lock_0000001416

17 号人 获取锁:/locks/test1_lock_0000001417 抢到了第33 件商品;

释放锁 /locks/test1_lock_0000001417

40 号人 获取锁:/locks/test1_lock_0000001418 抢到了第34 件商品;

释放锁 /locks/test1_lock_0000001418

4 号人 获取锁:/locks/test1_lock_0000001419 抢到了第35 件商品;

释放锁 /locks/test1_lock_0000001419

46 号人 获取锁:/locks/test1_lock_0000001420 抢到了第36 件商品;

释放锁 /locks/test1_lock_0000001420

5 号人 获取锁:/locks/test1_lock_0000001421 抢到了第37 件商品;

释放锁 /locks/test1_lock_0000001421

54 号人 获取锁:/locks/test1_lock_0000001422 抢到了第38 件商品;

释放锁 /locks/test1_lock_0000001422

24 号人 获取锁:/locks/test1_lock_0000001423 抢到了第39 件商品;

释放锁 /locks/test1_lock_0000001423

48 号人 获取锁:/locks/test1_lock_0000001424 抢到了第40 件商品;

释放锁 /locks/test1_lock_0000001424

50 号人 获取锁:/locks/test1_lock_0000001425 抢到了第41 件商品;

释放锁 /locks/test1_lock_0000001425

21 号人 获取锁:/locks/test1_lock_0000001426 抢到了第42 件商品;

释放锁 /locks/test1_lock_0000001426

49 号人 获取锁:/locks/test1_lock_0000001427 抢到了第43 件商品;

释放锁 /locks/test1_lock_0000001427

36 号人 获取锁:/locks/test1_lock_0000001428 抢到了第44 件商品;

释放锁 /locks/test1_lock_0000001428

43 号人 获取锁:/locks/test1_lock_0000001429 抢到了第45 件商品;

释放锁 /locks/test1_lock_0000001429

44 号人 获取锁:/locks/test1_lock_0000001430 抢到了第46 件商品;

释放锁 /locks/test1_lock_0000001430

30 号人 获取锁:/locks/test1_lock_0000001431 抢到了第47 件商品;

释放锁 /locks/test1_lock_0000001431

47 号人 获取锁:/locks/test1_lock_0000001432 抢到了第48 件商品;

释放锁 /locks/test1_lock_0000001432

10 号人 获取锁:/locks/test1_lock_0000001433 抢到了第49 件商品;

释放锁 /locks/test1_lock_0000001433

9 号人 获取锁:/locks/test1_lock_0000001434 抢到了第50 件商品;

释放锁 /locks/test1_lock_0000001434

51 号人 获取锁:/locks/test1_lock_0000001435 抢到了第51 件商品;

释放锁 /locks/test1_lock_0000001435

31 号人 获取锁:/locks/test1_lock_0000001436 抢到了第52 件商品;

释放锁 /locks/test1_lock_0000001436

16 号人 获取锁:/locks/test1_lock_0000001437 抢到了第53 件商品;

释放锁 /locks/test1_lock_0000001437

45 号人 获取锁:/locks/test1_lock_0000001438 抢到了第54 件商品;

释放锁 /locks/test1_lock_0000001438

55 号人 获取锁:/locks/test1_lock_0000001439 抢到了第55 件商品;

释放锁 /locks/test1_lock_0000001439

53 号人 获取锁:/locks/test1_lock_0000001440 抢到了第56 件商品;

释放锁 /locks/test1_lock_0000001440

61 号人 获取锁:/locks/test1_lock_0000001441 抢到了第57 件商品;

释放锁 /locks/test1_lock_0000001441

59 号人 获取锁:/locks/test1_lock_0000001442 抢到了第58 件商品;

释放锁 /locks/test1_lock_0000001442

58 号人 获取锁:/locks/test1_lock_0000001443 抢到了第59 件商品;

释放锁 /locks/test1_lock_0000001443

57 号人 获取锁:/locks/test1_lock_0000001444 抢到了第60 件商品;

释放锁 /locks/test1_lock_0000001444

60 号人 获取锁:/locks/test1_lock_0000001445 抢到了第61 件商品;

释放锁 /locks/test1_lock_0000001445

92 号人 获取锁:/locks/test1_lock_0000001446 抢到了第62 件商品;

释放锁 /locks/test1_lock_0000001446

95 号人 获取锁:/locks/test1_lock_0000001447 抢到了第63 件商品;

释放锁 /locks/test1_lock_0000001447

94 号人 获取锁:/locks/test1_lock_0000001448 抢到了第64 件商品;

释放锁 /locks/test1_lock_0000001448

96 号人 获取锁:/locks/test1_lock_0000001449 抢到了第65 件商品;

释放锁 /locks/test1_lock_0000001449

98 号人 获取锁:/locks/test1_lock_0000001450 抢到了第66 件商品;

释放锁 /locks/test1_lock_0000001450

100 号人 获取锁:/locks/test1_lock_0000001451 抢到了第67 件商品;

释放锁 /locks/test1_lock_0000001451

101 号人 获取锁:/locks/test1_lock_0000001452 抢到了第68 件商品;

释放锁 /locks/test1_lock_0000001452

102 号人 获取锁:/locks/test1_lock_0000001453 抢到了第69 件商品;

释放锁 /locks/test1_lock_0000001453

104 号人 获取锁:/locks/test1_lock_0000001454 抢到了第70 件商品;

释放锁 /locks/test1_lock_0000001454

117 号人 获取锁:/locks/test1_lock_0000001455 抢到了第71 件商品;

释放锁 /locks/test1_lock_0000001455

116 号人 获取锁:/locks/test1_lock_0000001456 抢到了第72 件商品;

释放锁 /locks/test1_lock_0000001456

113 号人 获取锁:/locks/test1_lock_0000001457 抢到了第73 件商品;

释放锁 /locks/test1_lock_0000001457

119 号人 获取锁:/locks/test1_lock_0000001458 抢到了第74 件商品;

释放锁 /locks/test1_lock_0000001458

120 号人 获取锁:/locks/test1_lock_0000001459 抢到了第75 件商品;

释放锁 /locks/test1_lock_0000001459

111 号人 获取锁:/locks/test1_lock_0000001460 抢到了第76 件商品;

释放锁 /locks/test1_lock_0000001460

115 号人 获取锁:/locks/test1_lock_0000001461 抢到了第77 件商品;

释放锁 /locks/test1_lock_0000001461

107 号人 获取锁:/locks/test1_lock_0000001462 抢到了第78 件商品;

释放锁 /locks/test1_lock_0000001462

114 号人 获取锁:/locks/test1_lock_0000001463 抢到了第79 件商品;

释放锁 /locks/test1_lock_0000001463

109 号人 获取锁:/locks/test1_lock_0000001464 抢到了第80 件商品;

释放锁 /locks/test1_lock_0000001464

63 号人 获取锁:/locks/test1_lock_0000001465 抢到了第81 件商品;

释放锁 /locks/test1_lock_0000001465

62 号人 获取锁:/locks/test1_lock_0000001466 抢到了第82 件商品;

释放锁 /locks/test1_lock_0000001466

88 号人 获取锁:/locks/test1_lock_0000001467 抢到了第83 件商品;

释放锁 /locks/test1_lock_0000001467

81 号人 获取锁:/locks/test1_lock_0000001468 抢到了第84 件商品;

释放锁 /locks/test1_lock_0000001468

82 号人 获取锁:/locks/test1_lock_0000001469 抢到了第85 件商品;

释放锁 /locks/test1_lock_0000001469

87 号人 获取锁:/locks/test1_lock_0000001470 抢到了第86 件商品;

释放锁 /locks/test1_lock_0000001470

64 号人 获取锁:/locks/test1_lock_0000001471 抢到了第87 件商品;

释放锁 /locks/test1_lock_0000001471

84 号人 获取锁:/locks/test1_lock_0000001472 抢到了第88 件商品;

释放锁 /locks/test1_lock_0000001472

68 号人 获取锁:/locks/test1_lock_0000001473 抢到了第89 件商品;

释放锁 /locks/test1_lock_0000001473

67 号人 获取锁:/locks/test1_lock_0000001474 抢到了第90 件商品;

释放锁 /locks/test1_lock_0000001474

99 号人 获取锁:/locks/test1_lock_0000001475 抢到了第91 件商品;

释放锁 /locks/test1_lock_0000001475

78 号人 获取锁:/locks/test1_lock_0000001476 抢到了第92 件商品;

释放锁 /locks/test1_lock_0000001476

73 号人 获取锁:/locks/test1_lock_0000001477 抢到了第93 件商品;

释放锁 /locks/test1_lock_0000001477

71 号人 获取锁:/locks/test1_lock_0000001478 抢到了第94 件商品;

释放锁 /locks/test1_lock_0000001478

83 号人 获取锁:/locks/test1_lock_0000001479 抢到了第95 件商品;

释放锁 /locks/test1_lock_0000001479

91 号人 获取锁:/locks/test1_lock_0000001480 抢到了第96 件商品;

释放锁 /locks/test1_lock_0000001480

90 号人 获取锁:/locks/test1_lock_0000001481 抢到了第97 件商品;

释放锁 /locks/test1_lock_0000001481

65 号人 获取锁:/locks/test1_lock_0000001482 抢到了第98 件商品;

释放锁 /locks/test1_lock_0000001482

77 号人 获取锁:/locks/test1_lock_0000001483 抢到了第99 件商品;

释放锁 /locks/test1_lock_0000001483

76 号人 获取锁:/locks/test1_lock_0000001484 抢到了第100 件商品;

释放锁 /locks/test1_lock_0000001484

释放锁 /locks/test1_lock_0000001485

75 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001486

69 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001487

89 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001488

105 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001489

85 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001490

74 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001491

66 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001492

112 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001493

释放锁 /locks/test1_lock_0000001494

释放锁 /locks/test1_lock_0000001495

70 号人 太悲剧了,没抢到商品...

108 号人 太悲剧了,没抢到商品...

79 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001496

释放锁 /locks/test1_lock_0000001497

93 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001498

106 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001499

103 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001500

释放锁 /locks/test1_lock_0000001501

72 号人 太悲剧了,没抢到商品...

80 号人 太悲剧了,没抢到商品...

118 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001502

86 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001503

97 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001504

110 号人 太悲剧了,没抢到商品...

入口类:



package com.demo.index;


import com.demo.utils.DistributedLock;

import com.jfinal.core.Controller;

import com.jfinal.plugin.activerecord.Db;

import com.jfinal.plugin.activerecord.Record;


/**

 * 秒杀/zookeeper 分布式锁

 * 

 * @author sun

 */

public class SecKillForZookeeperController extends Controller {


    public static int goodsTotalNum = 100;


    public void index() {

        //每一次新的请求都会模拟抢购100件商品,所以先恢复商品的数量,这个可以不要,也可以手动去更新数据库

        Db.update("update t_goods set num = ? where id = 1", goodsTotalNum);


        Record goods = Db.findFirst("select * from t_goods where id = 1");

        Integer num = 0;

        if(goods!=null){

            num = goods.getInt("num");

        }


        SecKillZookeeper sk = new SecKillZookeeper(num);

        // 模拟120个人抢购。 模拟120人分别在不同的服务器进行发起请求

        for (int i = 1; i <= 120; i++) {

            new Thread(sk, i + " 号人").start();

        }


        renderHtml("<center><h2>抢购成功!</h2></center>");

    }

}


/**

 * 这里我们只是简单演示实现方式,至于事务等其他的都不添加了。

 * 

 * @author sun

 */

class SecKillZookeeper implements Runnable {


    private int num = 0;


    public SecKillZookeeper(int num) {

        this.num = num;

    }


    @Override

    public void run() {

        DistributedLock lock = null;

        try {

            lock = new DistributedLock("127.0.0.1:2181", "test1");

            lock.lock();

            if (num > 0) {

                sale(lock);

                return ;

            }

        } finally {

            if (lock != null) {

                lock.unlock();

            }

        }

        System.err.println(Thread.currentThread().getName() + " 太悲剧了,没抢到商品...");

    }


    private void sale(DistributedLock lock) {

        if(num>0){

            Db.update("update t_goods set num=num-1 where id = 1");

            num--;

            System.out.println(Thread.currentThread().getName()+" 获取锁:"+lock.CURRENT_LOCK+" 抢到了第"+(SecKillForZookeeperController.goodsTotalNum - num)+" 件商品;");

        }

    }

}

打印结果: 

39 号人 获取锁:/locks/test1_lock_0000001385 抢到了第1 件商品;

释放锁 /locks/test1_lock_0000001385

35 号人 获取锁:/locks/test1_lock_0000001386 抢到了第2 件商品;

释放锁 /locks/test1_lock_0000001386

25 号人 获取锁:/locks/test1_lock_0000001387 抢到了第3 件商品;

释放锁 /locks/test1_lock_0000001387

42 号人 获取锁:/locks/test1_lock_0000001388 抢到了第4 件商品;

释放锁 /locks/test1_lock_0000001388

19 号人 获取锁:/locks/test1_lock_0000001389 抢到了第5 件商品;

释放锁 /locks/test1_lock_0000001389

38 号人 获取锁:/locks/test1_lock_0000001390 抢到了第6 件商品;

释放锁 /locks/test1_lock_0000001390

22 号人 获取锁:/locks/test1_lock_0000001391 抢到了第7 件商品;

释放锁 /locks/test1_lock_0000001391

18 号人 获取锁:/locks/test1_lock_0000001392 抢到了第8 件商品;

释放锁 /locks/test1_lock_0000001392

27 号人 获取锁:/locks/test1_lock_0000001393 抢到了第9 件商品;

释放锁 /locks/test1_lock_0000001393

28 号人 获取锁:/locks/test1_lock_0000001394 抢到了第10 件商品;

释放锁 /locks/test1_lock_0000001394

2 号人 获取锁:/locks/test1_lock_0000001395 抢到了第11 件商品;

释放锁 /locks/test1_lock_0000001395

14 号人 获取锁:/locks/test1_lock_0000001396 抢到了第12 件商品;

释放锁 /locks/test1_lock_0000001396

20 号人 获取锁:/locks/test1_lock_0000001397 抢到了第13 件商品;

释放锁 /locks/test1_lock_0000001397

11 号人 获取锁:/locks/test1_lock_0000001398 抢到了第14 件商品;

释放锁 /locks/test1_lock_0000001398

6 号人 获取锁:/locks/test1_lock_0000001399 抢到了第15 件商品;

释放锁 /locks/test1_lock_0000001399

7 号人 获取锁:/locks/test1_lock_0000001400 抢到了第16 件商品;

释放锁 /locks/test1_lock_0000001400

29 号人 获取锁:/locks/test1_lock_0000001401 抢到了第17 件商品;

释放锁 /locks/test1_lock_0000001401

3 号人 获取锁:/locks/test1_lock_0000001402 抢到了第18 件商品;

释放锁 /locks/test1_lock_0000001402

8 号人 获取锁:/locks/test1_lock_0000001403 抢到了第19 件商品;

释放锁 /locks/test1_lock_0000001403

32 号人 获取锁:/locks/test1_lock_0000001404 抢到了第20 件商品;

释放锁 /locks/test1_lock_0000001404

33 号人 获取锁:/locks/test1_lock_0000001405 抢到了第21 件商品;

释放锁 /locks/test1_lock_0000001405

23 号人 获取锁:/locks/test1_lock_0000001406 抢到了第22 件商品;

释放锁 /locks/test1_lock_0000001406

12 号人 获取锁:/locks/test1_lock_0000001407 抢到了第23 件商品;

释放锁 /locks/test1_lock_0000001407

15 号人 获取锁:/locks/test1_lock_0000001408 抢到了第24 件商品;

释放锁 /locks/test1_lock_0000001408

13 号人 获取锁:/locks/test1_lock_0000001409 抢到了第25 件商品;

释放锁 /locks/test1_lock_0000001409

34 号人 获取锁:/locks/test1_lock_0000001410 抢到了第26 件商品;

释放锁 /locks/test1_lock_0000001410

41 号人 获取锁:/locks/test1_lock_0000001411 抢到了第27 件商品;

释放锁 /locks/test1_lock_0000001411

37 号人 获取锁:/locks/test1_lock_0000001412 抢到了第28 件商品;

释放锁 /locks/test1_lock_0000001412

1 号人 获取锁:/locks/test1_lock_0000001413 抢到了第29 件商品;

释放锁 /locks/test1_lock_0000001413

52 号人 获取锁:/locks/test1_lock_0000001414 抢到了第30 件商品;

释放锁 /locks/test1_lock_0000001414

56 号人 获取锁:/locks/test1_lock_0000001415 抢到了第31 件商品;

释放锁 /locks/test1_lock_0000001415

26 号人 获取锁:/locks/test1_lock_0000001416 抢到了第32 件商品;

释放锁 /locks/test1_lock_0000001416

17 号人 获取锁:/locks/test1_lock_0000001417 抢到了第33 件商品;

释放锁 /locks/test1_lock_0000001417

40 号人 获取锁:/locks/test1_lock_0000001418 抢到了第34 件商品;

释放锁 /locks/test1_lock_0000001418

4 号人 获取锁:/locks/test1_lock_0000001419 抢到了第35 件商品;

释放锁 /locks/test1_lock_0000001419

46 号人 获取锁:/locks/test1_lock_0000001420 抢到了第36 件商品;

释放锁 /locks/test1_lock_0000001420

5 号人 获取锁:/locks/test1_lock_0000001421 抢到了第37 件商品;

释放锁 /locks/test1_lock_0000001421

54 号人 获取锁:/locks/test1_lock_0000001422 抢到了第38 件商品;

释放锁 /locks/test1_lock_0000001422

24 号人 获取锁:/locks/test1_lock_0000001423 抢到了第39 件商品;

释放锁 /locks/test1_lock_0000001423

48 号人 获取锁:/locks/test1_lock_0000001424 抢到了第40 件商品;

释放锁 /locks/test1_lock_0000001424

50 号人 获取锁:/locks/test1_lock_0000001425 抢到了第41 件商品;

释放锁 /locks/test1_lock_0000001425

21 号人 获取锁:/locks/test1_lock_0000001426 抢到了第42 件商品;

释放锁 /locks/test1_lock_0000001426

49 号人 获取锁:/locks/test1_lock_0000001427 抢到了第43 件商品;

释放锁 /locks/test1_lock_0000001427

36 号人 获取锁:/locks/test1_lock_0000001428 抢到了第44 件商品;

释放锁 /locks/test1_lock_0000001428

43 号人 获取锁:/locks/test1_lock_0000001429 抢到了第45 件商品;

释放锁 /locks/test1_lock_0000001429

44 号人 获取锁:/locks/test1_lock_0000001430 抢到了第46 件商品;

释放锁 /locks/test1_lock_0000001430

30 号人 获取锁:/locks/test1_lock_0000001431 抢到了第47 件商品;

释放锁 /locks/test1_lock_0000001431

47 号人 获取锁:/locks/test1_lock_0000001432 抢到了第48 件商品;

释放锁 /locks/test1_lock_0000001432

10 号人 获取锁:/locks/test1_lock_0000001433 抢到了第49 件商品;

释放锁 /locks/test1_lock_0000001433

9 号人 获取锁:/locks/test1_lock_0000001434 抢到了第50 件商品;

释放锁 /locks/test1_lock_0000001434

51 号人 获取锁:/locks/test1_lock_0000001435 抢到了第51 件商品;

释放锁 /locks/test1_lock_0000001435

31 号人 获取锁:/locks/test1_lock_0000001436 抢到了第52 件商品;

释放锁 /locks/test1_lock_0000001436

16 号人 获取锁:/locks/test1_lock_0000001437 抢到了第53 件商品;

释放锁 /locks/test1_lock_0000001437

45 号人 获取锁:/locks/test1_lock_0000001438 抢到了第54 件商品;

释放锁 /locks/test1_lock_0000001438

55 号人 获取锁:/locks/test1_lock_0000001439 抢到了第55 件商品;

释放锁 /locks/test1_lock_0000001439

53 号人 获取锁:/locks/test1_lock_0000001440 抢到了第56 件商品;

释放锁 /locks/test1_lock_0000001440

61 号人 获取锁:/locks/test1_lock_0000001441 抢到了第57 件商品;

释放锁 /locks/test1_lock_0000001441

59 号人 获取锁:/locks/test1_lock_0000001442 抢到了第58 件商品;

释放锁 /locks/test1_lock_0000001442

58 号人 获取锁:/locks/test1_lock_0000001443 抢到了第59 件商品;

释放锁 /locks/test1_lock_0000001443

57 号人 获取锁:/locks/test1_lock_0000001444 抢到了第60 件商品;

释放锁 /locks/test1_lock_0000001444

60 号人 获取锁:/locks/test1_lock_0000001445 抢到了第61 件商品;

释放锁 /locks/test1_lock_0000001445

92 号人 获取锁:/locks/test1_lock_0000001446 抢到了第62 件商品;

释放锁 /locks/test1_lock_0000001446

95 号人 获取锁:/locks/test1_lock_0000001447 抢到了第63 件商品;

释放锁 /locks/test1_lock_0000001447

94 号人 获取锁:/locks/test1_lock_0000001448 抢到了第64 件商品;

释放锁 /locks/test1_lock_0000001448

96 号人 获取锁:/locks/test1_lock_0000001449 抢到了第65 件商品;

释放锁 /locks/test1_lock_0000001449

98 号人 获取锁:/locks/test1_lock_0000001450 抢到了第66 件商品;

释放锁 /locks/test1_lock_0000001450

100 号人 获取锁:/locks/test1_lock_0000001451 抢到了第67 件商品;

释放锁 /locks/test1_lock_0000001451

101 号人 获取锁:/locks/test1_lock_0000001452 抢到了第68 件商品;

释放锁 /locks/test1_lock_0000001452

102 号人 获取锁:/locks/test1_lock_0000001453 抢到了第69 件商品;

释放锁 /locks/test1_lock_0000001453

104 号人 获取锁:/locks/test1_lock_0000001454 抢到了第70 件商品;

释放锁 /locks/test1_lock_0000001454

117 号人 获取锁:/locks/test1_lock_0000001455 抢到了第71 件商品;

释放锁 /locks/test1_lock_0000001455

116 号人 获取锁:/locks/test1_lock_0000001456 抢到了第72 件商品;

释放锁 /locks/test1_lock_0000001456

113 号人 获取锁:/locks/test1_lock_0000001457 抢到了第73 件商品;

释放锁 /locks/test1_lock_0000001457

119 号人 获取锁:/locks/test1_lock_0000001458 抢到了第74 件商品;

释放锁 /locks/test1_lock_0000001458

120 号人 获取锁:/locks/test1_lock_0000001459 抢到了第75 件商品;

释放锁 /locks/test1_lock_0000001459

111 号人 获取锁:/locks/test1_lock_0000001460 抢到了第76 件商品;

释放锁 /locks/test1_lock_0000001460

115 号人 获取锁:/locks/test1_lock_0000001461 抢到了第77 件商品;

释放锁 /locks/test1_lock_0000001461

107 号人 获取锁:/locks/test1_lock_0000001462 抢到了第78 件商品;

释放锁 /locks/test1_lock_0000001462

114 号人 获取锁:/locks/test1_lock_0000001463 抢到了第79 件商品;

释放锁 /locks/test1_lock_0000001463

109 号人 获取锁:/locks/test1_lock_0000001464 抢到了第80 件商品;

释放锁 /locks/test1_lock_0000001464

63 号人 获取锁:/locks/test1_lock_0000001465 抢到了第81 件商品;

释放锁 /locks/test1_lock_0000001465

62 号人 获取锁:/locks/test1_lock_0000001466 抢到了第82 件商品;

释放锁 /locks/test1_lock_0000001466

88 号人 获取锁:/locks/test1_lock_0000001467 抢到了第83 件商品;

释放锁 /locks/test1_lock_0000001467

81 号人 获取锁:/locks/test1_lock_0000001468 抢到了第84 件商品;

释放锁 /locks/test1_lock_0000001468

82 号人 获取锁:/locks/test1_lock_0000001469 抢到了第85 件商品;

释放锁 /locks/test1_lock_0000001469

87 号人 获取锁:/locks/test1_lock_0000001470 抢到了第86 件商品;

释放锁 /locks/test1_lock_0000001470

64 号人 获取锁:/locks/test1_lock_0000001471 抢到了第87 件商品;

释放锁 /locks/test1_lock_0000001471

84 号人 获取锁:/locks/test1_lock_0000001472 抢到了第88 件商品;

释放锁 /locks/test1_lock_0000001472

68 号人 获取锁:/locks/test1_lock_0000001473 抢到了第89 件商品;

释放锁 /locks/test1_lock_0000001473

67 号人 获取锁:/locks/test1_lock_0000001474 抢到了第90 件商品;

释放锁 /locks/test1_lock_0000001474

99 号人 获取锁:/locks/test1_lock_0000001475 抢到了第91 件商品;

释放锁 /locks/test1_lock_0000001475

78 号人 获取锁:/locks/test1_lock_0000001476 抢到了第92 件商品;

释放锁 /locks/test1_lock_0000001476

73 号人 获取锁:/locks/test1_lock_0000001477 抢到了第93 件商品;

释放锁 /locks/test1_lock_0000001477

71 号人 获取锁:/locks/test1_lock_0000001478 抢到了第94 件商品;

释放锁 /locks/test1_lock_0000001478

83 号人 获取锁:/locks/test1_lock_0000001479 抢到了第95 件商品;

释放锁 /locks/test1_lock_0000001479

91 号人 获取锁:/locks/test1_lock_0000001480 抢到了第96 件商品;

释放锁 /locks/test1_lock_0000001480

90 号人 获取锁:/locks/test1_lock_0000001481 抢到了第97 件商品;

释放锁 /locks/test1_lock_0000001481

65 号人 获取锁:/locks/test1_lock_0000001482 抢到了第98 件商品;

释放锁 /locks/test1_lock_0000001482

77 号人 获取锁:/locks/test1_lock_0000001483 抢到了第99 件商品;

释放锁 /locks/test1_lock_0000001483

76 号人 获取锁:/locks/test1_lock_0000001484 抢到了第100 件商品;

释放锁 /locks/test1_lock_0000001484

释放锁 /locks/test1_lock_0000001485

75 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001486

69 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001487

89 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001488

105 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001489

85 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001490

74 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001491

66 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001492

112 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001493

释放锁 /locks/test1_lock_0000001494

释放锁 /locks/test1_lock_0000001495

70 号人 太悲剧了,没抢到商品...

108 号人 太悲剧了,没抢到商品...

79 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001496

释放锁 /locks/test1_lock_0000001497

93 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001498

106 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001499

103 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001500

释放锁 /locks/test1_lock_0000001501

72 号人 太悲剧了,没抢到商品...

80 号人 太悲剧了,没抢到商品...

118 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001502

86 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001503

97 号人 太悲剧了,没抢到商品...

释放锁 /locks/test1_lock_0000001504

110 号人 太悲剧了,没抢到商品...

总体来说,如果了解到整个实现流程,使用zookeeper实现分布式锁并不是很困难,不过这也只是一个简单的实现,与前面实现Redis实现相比,本实现的稳定性更强,这是因为zookeeper的特性所致,在外界看来,zookeeper集群中每一个节点都是一致的。

在此我向大家推荐一个架构学习交流群。交流学习群号:897889510 加群获得以上学习视频,群里面还会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

t_goods表结构:

CREATE TABLE `d_sunjs_test`.`t_goods`  (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

  `num` int(11) NULL DEFAULT NULL,

  `version` int(11) NULL DEFAULT 1 COMMENT '乐观锁',

  PRIMARY KEY (`id`) USING BTREE

) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

测试的时候记得启动zookeeper服务!

推荐阅读更多精彩内容