赞
踩
在分布式系统中,存在着许多进程同时运转,而为了防止分布式系统中的多个进程之间相互干扰,我们需要一种分布式协调技术来对这些进程进行调度。而这个分布式协调技术的核心就是来实现分布式锁。
分布式锁:控制分布式系统有序的去对共享资源进行操作,通过互斥来保持一致性。
setnx
命令实现。
Curator是Netflix开源的一套ZooKeeper客户端框架,用于解决以下这三类问题:
1.创建一个简单的springboot工程,在pom.xml文件导入zookeeper依赖和Curator相关依赖
- <!--引入对应的zookeeper -->
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.7.1</version>
- </dependency>
- <!--添加对应的curator框架依赖-->
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>5.2.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>5.2.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-client</artifactId>
- <version>5.2.0</version>
- </dependency>
2.开启zookeeper集群中的客户端 (至少保证集群能够正常启动)
[root@zookeeper3 ~]# zkCli.sh
3.编写CuratorLockTest(测试分布式锁)
- package com.canrioyuan.zookeepertest.curator;
-
- import org.apache.curator.framework.CuratorFramework;
- import org.apache.curator.framework.CuratorFrameworkFactory;
- import org.apache.curator.framework.recipes.locks.InterProcessLock;
- import org.apache.curator.framework.recipes.locks.InterProcessMutex;
- import org.apache.curator.retry.ExponentialBackoffRetry;
-
- //使用Curator框架实现分布式锁
- public class CuratorLockTest {
- //定义锁节点的路径
- private String node = "/locks";
- //设置zookeeper连接
- private final String connectString = "zookeeper1:2181,zookeeper2:2181,zookeeper3:2181";
- //设置超时时间
- private final int sessionTimeout = 2000;
- //设置超时时间
- private final int connectionTimeout = 3000;
-
- public static void main(String[] args) {
- new CuratorLockTest().test();
- }
-
- private void test() {
- //创建分布式锁1
- final InterProcessLock lock1 = new InterProcessMutex(getCuratorFramework(), node);
-
- //创建分布式锁2
- final InterProcessLock lock2 = new InterProcessMutex(getCuratorFramework(), node);
-
- //创建线程
- //线程1
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- //线程加锁
- lock1.acquire();
- System.out.println("线程1获取锁");
-
- //线程沉睡
- Thread.sleep(5*1000);
-
- //线程解锁
- lock1.release();
- System.out.println("线程1释放了锁");
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- }).start();
-
- //线程2
- new Thread(new Runnable() {
- @Override
- public void run() {
- //线程加锁
- try {
- lock2.acquire();
- System.out.println("线程2获取到锁");
-
- //线程沉睡
- Thread.sleep(5*1000);
-
- lock2.release();
- System.out.println("线程2释放锁");
-
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- }).start();
- }
-
- //对分布式锁进行初始化
- private CuratorFramework getCuratorFramework() {
- //重试策略,定义初试时间3s,重试3次
- ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(3000, 3);
-
- //初始化客户端
- CuratorFramework client = CuratorFrameworkFactory.builder()
- .connectString(connectString)
- .sessionTimeoutMs(sessionTimeout)
- .connectionTimeoutMs(connectionTimeout)
- .retryPolicy(exponentialBackoffRetry)
- .build();
-
- //开启连接
- client.start();
- System.out.println("zookeeper 初始化完成...");
- return client;
-
- }
-
-
-
-
- }
线程1获取了锁
线程1释放了锁
等到线程1释放锁之后线程2才能获取到锁
线程2释放锁
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。