一、Curator基本API操作
为了更好的实现Java操作zookeeper服务器,后来出现Curator框架,非常的强大,目前已经是Apache顶级项目,里面提供了更多丰富的操作,例如session超时重连、主从选举、分布式计数器、分布式锁等等适用于各种复杂的zookeeper场景的API封装。
在pom.xml中引入下述依赖:
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.3.0</version>
</dependency>
Curator框架中使用链式编程风格,易读性更强,使用工程方法创建连接对象。
1、使用CuratorFrameworkFactory的两个静态工厂方法(参数不同)来实现
参数1:connectString,连接串
参数2:retryPolicy,重试连接策略。有四种实现分别为:ExponentialBackoffRetry、RetryNTimes、RetryOne Times、RetryUntilElapsed
参数3:sessionTimeoutMs会话超时时间默认为60000ms
参数4:connectionTimeoutMs连接超时时间,默认为15 000ms
注意:对于retryPolicy策略通过一个接口来让用户自定义实现。
2、创建节点create方法,可选链式项:
creatingParentslfNeeded、withMode、forPath、withACL等。
3、删除节点delete方法,可选链式项:
deletingChildrenIfNeeded、guaranteed、withVersion、forPath等
4、读取和修改数据getData、setData方法
5、异步绑定回调方法。比如创建节点时绑定一个回调函数,该回调函数可以输出服务器的状态码以及服务器事件类型,还可以加入一个线程池进行优化操作。
6、读取子节点方法getChildren
7、判断节点是否存在方法checkExists
package com.bhz.curator.base;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooKeeper.States;
import org.apache.zookeeper.data.Stat;
public class CuratorBase {
// zookeeper地址
static final String CONNECT_ADDR = "192.168.1.105:2181,192.168.1.106:2181,192.168.1.107:2181";
// session超时时间,单位是毫秒
static final int SESSION_OUTTIME = 100000;
public static void main(String[] args) throws Exception {
//1 重试策略:初试时间为1s,重试10次
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
//2 通过工厂创建连接
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR)
.sessionTimeoutMs(SESSION_OUTTIME)
.retryPolicy(retryPolicy)
// .namespace("super")
.build();
//3 开启连接
cf.start();
// 新加、删除
// 4.建立节点 指定节点类型(不加withMode默认为持久类型节点)、路径、数据内容
//cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes());
// 5.删除节点,会进行递归删除
//cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super");
// 读取、修改
//6.创建节点
cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes());
cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c2","c2内容".getBytes());
//7.读取节点
//String ret1 = new String(cf.getData().forPath("/super/c2"));
//System.out.println(ret1);
//8.修改节点
//cf.setData().forPath("/super/c2", "修改c2内容".getBytes());
//String ret2 = new String(cf.getData().forPath("/super/c2"));
//System.out.println(ret2);
// 绑定回调函数
ExecutorService pool = Executors.newCachedThreadPool();
cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT)
.inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework cf, CuratorEvent ce) throws Exception {
System.out.println("code:" + ce.getResultCode());
System.out.println("type:" + ce.getType());
System.out.println("线程为:" + Thread.currentThread().getName());
}
}, pool)
.forPath("/super/c3","c3内容".getBytes());
Thread.sleep(Integer.MAX_VALUE);
// 读取子节点getChildren方法 和
//List<String> list = cf.getChildren().forPath("/super");
//for(String p : list){
// System.out.println(p);
//}
// checkExists方法判断节点是否存在
//Stat stat = cf.checkExists().forPath("/super/c3");
//System.out.println(stat);
cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super");
}
}
执行上述代码,其输出结果为:
code:0
type:CREATE
线程为:pool-4-thread-1
二、Curator的监听
如果要使用类似Wather的监听功能Curator必须依赖一个jar包,Maven依赖:
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.3.0</version>
</dependency>
有了这个依赖包,我们使用NodeCache的方式去客户端实例中注册一个监听缓存,然后实现对应的监听方法即可,这里我们主要有俩种监听方式:
NodeCacheListener:监听节点的新增、修改操作。
PathChildrenCacheListener:监听子节点的新增、修改、删除操作。
以下为监听节点的新增、修改操作的示例:
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
public class CuratorWatcher1 {
/** zookeeper地址 */
static final String CONNECT_ADDR = "192.168.1.105:2181,192.168.1.106:2181,192.168.1.107:2181";
/** session超时时间 */
static final int SESSION_OUTTIME = 100000;//ms
public static void main(String[] args) throws Exception {
//1 重试策略:初试时间为1s 重试10次
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
//2 通过工厂创建连接
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR)
.sessionTimeoutMs(SESSION_OUTTIME)
.retryPolicy(retryPolicy)
.build();
//3 建立连接
cf.start();
//4 建立一个cache缓存
final NodeCache cache = new NodeCache(cf, "/super", false);
cache.start(true);
cache.getListenable().addListener(new NodeCacheListener() {
// 触发事件为创建节点和更新节点,在删除节点的时候并不触发此操作。
@Override
public void nodeChanged() {
System.out.println("路径为:" + cache.getCurrentData().getPath());
System.out.println("数据为:" + new String(cache.getCurrentData().getData()));
System.out.println("状态为:" + cache.getCurrentData().getStat());
System.out.println("---------------------------------------");
}
});
cf.create().forPath("/super", "123".getBytes());
cf.setData().forPath("/super", "456".getBytes());
cf.delete().forPath("/super");
Thread.sleep(Integer.MAX_VALUE);
}
}
以下为监听子节点的新增、修改、删除操作的示例:
package com.bhz.curator.watcher;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;
import org.apache.curator.retry.ExponentialBackoffRetry;
public class CuratorWatcher2 {
/** zookeeper地址 */
static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181";
/** session超时时间 */
static final int SESSION_OUTTIME = 100000;//ms
public static void main(String[] args) throws Exception {
//1 重试策略:初试时间为1s 重试10次
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
//2 通过工厂创建连接
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR)
.sessionTimeoutMs(SESSION_OUTTIME)
.retryPolicy(retryPolicy)
.build();
//3 建立连接
cf.start();
//4 建立一个PathChildrenCache缓存,第三个参数为是否接受节点数据内容 如果为false则不接受
PathChildrenCache cache = new PathChildrenCache(cf, "/super", true);
//5 在初始化的时候就进行缓存监听
cache.start(StartMode.POST_INITIALIZED_EVENT);
cache.getListenable().addListener(new PathChildrenCacheListener() {
// 监听子节点变更,新建、修改、删除
@Override
public void childEvent(CuratorFramework cf, PathChildrenCacheEvent event) throws Exception {
switch (event.getType()) {
case CHILD_ADDED:
System.out.println("CHILD_ADDED :" + event.getData().getPath());
break;
case CHILD_UPDATED:
System.out.println("CHILD_UPDATED :" + event.getData().getPath());
break;
case CHILD_REMOVED:
System.out.println("CHILD_REMOVED :" + event.getData().getPath());
break;
default:
break;
}
}
});
//创建本身节点不发生变化
cf.create().forPath("/super", "init".getBytes());
//添加子节点
Thread.sleep(1000);
cf.create().forPath("/super/c1", "c1内容".getBytes());
Thread.sleep(1000);
cf.create().forPath("/super/c2", "c2内容".getBytes());
//修改子节点
Thread.sleep(1000);
cf.setData().forPath("/super/c1", "c1更新内容".getBytes());
//删除子节点
Thread.sleep(1000);
cf.delete().forPath("/super/c2");
//删除本身节点
Thread.sleep(1000);
cf.delete().deletingChildrenIfNeeded().forPath("/super");
Thread.sleep(Integer.MAX_VALUE);
}
}
三、Curator实现分布式锁
分布式锁功能:在分布式场景中,我们为了保证数据的一致性,经常在程序运行的某一个点需要进行同步操作(java可提供synchronized或者Reentrantlock实现)比如我们看一个小示例,这个示例会出现分布式不同步的问题。因为我们之前所说的是在高并发下访问一个程序,现在我们则是在高并发下访问多个服务器节点(分布式)。
我们使用Curator基于zookeeper的特性提供的分布式锁来处理分布式场景的数据一致性,zookeeper本身的分布式是有写问题的,我以前在实现的时候遇到过,这里强烈推荐使用Curator的分布式锁!
单机环境下的锁:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;
public class Lock1 {
static ReentrantLock reentrantLock = new ReentrantLock();
static int count = 10;
public static void genarNo(){
try {
reentrantLock.lock();
count--;
} finally {
reentrantLock.unlock();
}
}
public static void main(String[] args) throws Exception{
final CountDownLatch countdown = new CountDownLatch(1);
for(int i = 0; i < 10; i++){
new Thread(new Runnable() {
@Override
public void run() {
try {
countdown.await();
genarNo();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss|SSS");
System.out.println(sdf.format(new Date()));
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
},"t" + i).start();
}
Thread.sleep(50);
countdown.countDown();
}
}
使用Curator实现分布式锁(使用Zookeeper实现分布式锁推荐使用这种方案):
import java.util.concurrent.CountDownLatch;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
public class Lock2 {
/** zookeeper地址 */
static final String CONNECT_ADDR = "192.168.1.105:2181,192.168.1.106:2181,192.168.1.107:2181";
/** session超时时间 */
static final int SESSION_OUTTIME = 100000;//ms
public static CuratorFramework createCuratorFramework(){
//1 重试策略:初试时间为1s 重试10次
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
//2 通过工厂创建连接
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR)
.sessionTimeoutMs(SESSION_OUTTIME)
.retryPolicy(retryPolicy)
.build();
return cf;
}
public static void main(String[] args) throws Exception {
final CountDownLatch coutdown = new CountDownLatch(1);
for(int i = 0; i < 10; i++){
new Thread(new Runnable() {
@Override
public void run() {
CuratorFramework cf = createCuratorFramework();
cf.start();
final InterProcessMutex lock = new InterProcessMutex(cf, "/super");
try {
coutdown.await();
lock.acquire();
System.out.println(Thread.currentThread().getName() + "执行业务逻辑..");
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//释放
lock.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
},"t" + i).start();
}
Thread.sleep(2000);
coutdown.countDown();
}
}
四、Curator实现分布式计数器
分布式计数器功能:一说到分布式计数器,你可能脑海里想到了Atomiclnteger这种经典的方式,如果针对于一个jvm的场景当然没有问题,但是我们现在是分布式场景下,就需要利用curator框架的DistributedAtomiclnteger了。
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.atomic.AtomicValue;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryNTimes;
public class CuratorAtomicInteger {
/** zookeeper地址 */
static final String CONNECT_ADDR = "192.168.1.105:2181,192.168.1.106:2181,192.168.1.107:2181";
/** session超时时间 */
static final int SESSION_OUTTIME = 5000;//ms
public static void main(String[] args) throws Exception {
//1 重试策略:初试时间为1s 重试10次
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
//2 通过工厂创建连接
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR)
.sessionTimeoutMs(SESSION_OUTTIME)
.retryPolicy(retryPolicy)
.build();
//3 开启连接
cf.start();
//cf.delete().forPath("/super");
//4 使用DistributedAtomicInteger
DistributedAtomicInteger atomicIntger =
new DistributedAtomicInteger(cf, "/super", new RetryNTimes(3, 1000));
AtomicValue<Integer> value = atomicIntger.add(1);
System.out.println(value.succeeded());
System.out.println(value.postValue()); //最新值
System.out.println(value.preValue()); //原始值
}
}
执行上述代码,其输出结果为:
true
1
0
五、Curator实现栅栏(barrier)
该部分仅需了解即可,和并发工具类CycleBarrier类似
import java.util.Random;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.barriers.DistributedDoubleBarrier;
import org.apache.curator.retry.ExponentialBackoffRetry;
public class CuratorBarrier1 {
// zookeeper地址
static final String CONNECT_ADDR = "192.168.1.105:2181,192.168.1.106:2181,192.168.1.107:2181";
public static void main(String[] args){
for(int i = 0; i < 5; i++){
new Thread(new Runnable() {
@Override
public void run() {
try {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR)
.retryPolicy(retryPolicy)
.build();
cf.start();
DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(cf, "/super", 5);
Thread.sleep(1000 * (new Random()).nextInt(3));
System.out.println(Thread.currentThread().getName() + "已经准备");
barrier.enter();
System.out.println("同时开始运行...");
Thread.sleep(1000 * (new Random()).nextInt(3));
System.out.println(Thread.currentThread().getName() + "运行完毕");
barrier.leave();
System.out.println("同时退出运行...");
} catch (Exception e) {
e.printStackTrace();
}
}
},"t" + i).start();
}
}
}
六、Curator实现多个Watcher集群操作
定义一个Watcher:
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
public class CuratorWatcher {
/** 父节点path */
static final String PARENT_PATH = "/super";
/** zookeeper服务器地址 */
public static final String CONNECT_ADDR = "192.168.1.105:2181,192.168.1.106:2181,192.168.1.107:2181";
/** 定义session失效时间 */
public static final int SESSION_TIMEOUT = 30000;
public CuratorWatcher() throws Exception{
//1 重试策略:初试时间为1s 重试10次
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
//2 通过工厂创建连接
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR)
.sessionTimeoutMs(SESSION_TIMEOUT)
.retryPolicy(retryPolicy)
.build();
//3 建立连接
cf.start();
//4 创建跟节点
if(cf.checkExists().forPath(PARENT_PATH) == null){
cf.create().withMode(CreateMode.PERSISTENT).forPath(PARENT_PATH,"super init".getBytes());
}
//4 建立一个PathChildrenCache缓存,第三个参数为是否接受节点数据内容 如果为false则不接受
PathChildrenCache cache = new PathChildrenCache(cf, PARENT_PATH, true);
//5 在初始化的时候就进行缓存监听
cache.start(StartMode.POST_INITIALIZED_EVENT);
cache.getListenable().addListener(new PathChildrenCacheListener() {
/**
* <B>方法名称:</B>监听子节点变更<BR>
* <B>概要说明:</B>新建、修改、删除<BR>
* @see org.apache.curator.framework.recipes.cache.PathChildrenCacheListener#childEvent(org.apache.curator.framework.CuratorFramework, org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)
*/
@Override
public void childEvent(CuratorFramework cf, PathChildrenCacheEvent event) throws Exception {
switch (event.getType()) {
case CHILD_ADDED:
System.out.println("CHILD_ADDED :" + event.getData().getPath());
System.out.println("CHILD_ADDED :" + new String(event.getData().getData()));
break;
case CHILD_UPDATED:
System.out.println("CHILD_UPDATED :" + event.getData().getPath());
System.out.println("CHILD_UPDATED :" + new String(event.getData().getData()));
break;
case CHILD_REMOVED:
System.out.println("CHILD_REMOVED :" + event.getData().getPath());
System.out.println("CHILD_REMOVED :" + new String(event.getData().getData()));
break;
default:
break;
}
}
});
}
}
定义两个客户端:
public class Client1 {
public static void main(String[] args) throws Exception{
CuratorWatcher watcher = new CuratorWatcher();
Thread.sleep(100000000);
}
}
public class Client2 {
public static void main(String[] args) throws Exception{
CuratorWatcher watcher = new CuratorWatcher();
Thread.sleep(100000000);
}
}
构建相关的测试类:
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.apache.zookeeper.CreateMode;
public class Test {
/** zookeeper地址 */
static final String CONNECT_ADDR = "192.168.1.105:2181,192.168.1.106:2181,192.168.1.107:2181";
/** session超时时间 */
static final int SESSION_OUTTIME = 5000;//ms
public static void main(String[] args) throws Exception {
//1 重试策略:初试时间为1s 重试10次
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
//2 通过工厂创建连接
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR)
.sessionTimeoutMs(SESSION_OUTTIME)
.retryPolicy(retryPolicy)
.build();
//3 开启连接
cf.start();
Thread.sleep(3000);
System.out.println(cf.getChildren().forPath("/super").get(0));
//4 创建节点
// Thread.sleep(1000);
cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes());
Thread.sleep(1000);
// cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c2","c2内容".getBytes());
// Thread.sleep(1000);
//
//
//
// //5 读取节点
// Thread.sleep(1000);
// String ret1 = new String(cf.getData().forPath("/super/c1"));
// System.out.println(ret1);
//
//
// //6 修改节点
Thread.sleep(1000);
cf.setData().forPath("/super/c2", "修改的新c2内容".getBytes());
String ret2 = new String(cf.getData().forPath("/super/c2"));
System.out.println(ret2);
//
//
//
// //7 删除节点
// Thread.sleep(1000);
// cf.delete().forPath("/super/c1");
}
}
本文详细介绍了Apache的Curator框架,包括基本API操作,如创建、删除节点,读取和修改数据,以及监听功能。重点讲解了Curator实现的分布式锁、分布式计数器,展示了如何使用Curator解决分布式场景下的数据一致性问题。此外,还简要提到了栅栏(barrier)和多Watcher集群操作。
1515

被折叠的 条评论
为什么被折叠?



