步骤:

1- pom.xml

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>

2- yml配置:

zk:
  url: 127.0.0.1:2181
  localPath: /newlock
  timeout: 3000

3- 配置类

package com.test.domi.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class ZookeeperConf { @Value("${zk.url}")
private String zkUrl; @Bean
public CuratorFramework getCuratorFramework(){
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
CuratorFramework client = CuratorFrameworkFactory.newClient(zkUrl,retryPolicy);
client.start();
return client;
}
}

4- 使用

package com.test.domi.common.utils.lock;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.zookeeper.CreateMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
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; @Component("zklock")
public class ZKlock implements Lock { @Autowired
private CuratorFramework zkClient;
@Value("${zk.localPath}")
private String lockPath;
private String currentPath;
private String beforePath; @Override
public boolean tryLock() {
try {
//根节点的初始化放在构造函数里面不生效
if (zkClient.checkExists().forPath(lockPath) == null) {
System.out.println("初始化根节点==========>" + lockPath);
zkClient.create().creatingParentsIfNeeded().forPath(lockPath);
}
System.out.println("当前线程" + Thread.currentThread().getName() + "初始化根节点" + lockPath);
} catch (Exception e) {
} if (currentPath == null) {
try {
currentPath = this.zkClient.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
.forPath(lockPath + "/");
} catch (Exception e) {
return false;
}
}
try {
//此处该如何获取所有的临时节点呢?如locks00004.而不是获取/locks/order中的order作为子节点??
List<String> childrens = this.zkClient.getChildren().forPath(lockPath);
Collections.sort(childrens);
if (currentPath.equals(lockPath + "/" + childrens.get(0))) {
System.out.println("当前线程获得锁" + currentPath);
return true;
}else{
//取前一个节点
int curIndex = childrens.indexOf(currentPath.substring(lockPath.length() + 1));
//如果是-1表示children里面没有该节点
beforePath = lockPath + "/" + childrens.get(curIndex - 1);
}
} catch (Exception e) {
return false;
}
return false;
} @Override
public void lock() {
if (!tryLock()) {
waiForLock();
lock();
}
} @Override
public void unlock() {
try {
zkClient.delete().guaranteed().deletingChildrenIfNeeded().forPath(currentPath);
} catch (Exception e) {
//guaranteed()保障机制,若未删除成功,只要会话有效会在后台一直尝试删除
}
} private void waiForLock(){
CountDownLatch cdl = new CountDownLatch(1);
//创建监听器watch
NodeCache nodeCache = new NodeCache(zkClient,beforePath);
try {
nodeCache.start(true);
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
cdl.countDown();
System.out.println(beforePath + "节点监听事件触发,重新获得节点内容为:" + new String(nodeCache.getCurrentData().getData()));
}
});
} catch (Exception e) {
}
//如果前一个节点还存在,则阻塞自己
try {
if (zkClient.checkExists().forPath(beforePath) == null) {
cdl.await();
}
} catch (Exception e) {
}finally {
//阻塞结束,说明自己是最小的节点,则取消watch,开始获取锁
try {
nodeCache.close();
} catch (IOException e) {
}
}
} @Override
public void lockInterruptibly() throws InterruptedException {
} @Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
return false;
} @Override
public Condition newCondition() {
return null;
} }

5- 调用demo

package com.test.domi.controller;

import com.test.domi.common.utils.ZkUtil;
import com.test.domi.common.utils.lock.ZKlock;
import org.I0Itec.zkclient.ZkClient;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/zk")
public class ZKController { @Autowired
private CuratorFramework zkClient;
// @Autowired
// private ZkClient zkClient; private String url = "127.0.0.1:2181";
private int timeout = 3000;
private String lockPath = "/testl";
@Autowired
private ZKlock zklock;
private int k = 1; @GetMapping("/lock")
public Boolean getLock() throws Exception{ for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
zklock.lock();           zklock.unlock();
} }).start(); }
return true; } }

springboot2整合zookeeper集成curator的更多相关文章

  1. SpringBoot2 整合 Zookeeper组件,管理架构中服务协调

    本文源码:GitHub·点这里 || GitEE·点这里 一.Zookeeper基础简介 1.概念简介 Zookeeper是一个Apache开源的分布式的应用,为系统架构提供协调服务.从设计模式角度来 ...

  2. SpringBoot2整合activiti6环境搭建

    SpringBoot2整合activiti6环境搭建 依赖 <dependencies> <dependency> <groupId>org.springframe ...

  3. SpringBoot2 整合Kafka组件,应用案例和流程详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.搭建Kafka环境 1.下载解压 -- 下载 wget http://mirror.bit.edu.cn/apache/kafka/2.2 ...

  4. SpringBoot2 整合Ehcache组件,轻量级缓存管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.Ehcache缓存简介 1.基础简介 EhCache是一个纯Java的进程内缓存框架,具有快速.上手简单等特点,是Hibernate中默认 ...

  5. (十七)整合 Zookeeper组件,管理架构中服务协调

    整合 Zookeeper组件,管理架构中服务协调 1.Zookeeper基础简介 1.1 基本理论 1.2 应用场景 2.安全管理操作 2.1 操作权限 2.2 认证方式: 2.3 Digest授权流 ...

  6. Zookeeper客户端Curator使用详解

    Zookeeper客户端Curator使用详解 前提 最近刚好用到了zookeeper,做了一个基于SpringBoot.Curator.Bootstrap写了一个可视化的Web应用: zookeep ...

  7. zookeeper(六):Zookeeper客户端Curator的API使用详解

    简介 Curator是Netflix公司开源的一套zookeeper客户端框架,解决了很多Zookeeper客户端非常底层的细节开发工作,包括连接重连.反复注册Watcher和NodeExistsEx ...

  8. java springboot整合zookeeper入门教程(增删改查)

    java springboot整合zookeeper增删改查入门教程 zookeeper的安装与集群搭建参考:https://www.cnblogs.com/zwcry/p/10272506.html ...

  9. 转:Zookeeper客户端Curator使用详解

    原文:https://www.jianshu.com/p/70151fc0ef5d Zookeeper客户端Curator使用详解 前提 最近刚好用到了zookeeper,做了一个基于SpringBo ...

随机推荐

  1. sublime tab转4个空格配置

    打开Sublime Text3,选择菜单Preferences->Settings-User,打开用户配置文件 然后在大括号里加上下面两行代码: "tab_size": 4, ...

  2. mybatis延迟加载(assocation)

    一.何为延迟加载? 延迟加载 我们并不需要总是在加载用户信息时就一定要加载他的账户信息.此时就是我们所说的延迟加载. 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据.延迟加载也称懒加载. ...

  3. lamp 环境利用compser安装tp5

    1.安装compser,这个可以去百度命令 2.cd到根目录 3.composer create-project --prefer-dist topthink/think tp(tp可以随意命名) 4 ...

  4. This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}. When it did unmarshal using map[string]interface{}, a number with “int” was changed to “floa

    This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interf ...

  5. koa2环境搭建

    npm install -g koa-generator koa2 ssy-koa2 cd ssy-koa2 npm install

  6. jxbrowser 实现java 和 js互相调用

    https://blog.csdn.net/shuaizai88/article/details/73743626 今天我们使用jxbrowser  实现js直接调用java代码. 调用javaTes ...

  7. vue类似tab切换的效果,显示和隐藏的判断。

    两者切换,动态显示对应的列表详情. 通过v-show的判断 数据驱动

  8. 数据库开源框架之ormlite

    主页: http://ormlite.com/ 配置: 添加以下依赖 * compile 'com.j256.ormlite:ormlite-android:4.48' * compile 'com. ...

  9. 搭建Git服务器及本机克隆提交

    前文 Git是什么? Git是目前世界上最先进的分布式版本控制系统. SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首 ...

  10. Setup Python 开发环境和IPython的基本使用

    目录 目录 前言 软件准备 Python交互式Shell 为Python Shell添加补全功能 IPython-400 IPython功能和特性基础 可直接使用部分的Bash指令 别名alias语法 ...