Curator高度封装zookeeper客户端,对比之前的原生就非常nice。
码来!~~~
首先还是pom文件下载jar包
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
增、删、改如下 查就是stat,namespace也会自动创建出来。
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
/**
* @Title: Spring-distributed
* @Description:
* @author: liaryank
* @Date: 2020/5/6 11:26 上午
* @Version: 1.0
*/
public class Curator {
public static void main(String[] args) throws Exception {
CuratorFramework curatorFramework = CuratorFrameworkFactory.builder().connectString("192.168.3.32,192.168.3.33,192.168.3.34")
.sessionTimeoutMs(4000).
//重试机制
retryPolicy(new ExponentialBackoffRetry(1000,3)).namespace("Curator").build();
curatorFramework.start();
//创建 会把上面的namespace和下面的父子节点都创建出来
curatorFramework.create().creatingParentsIfNeeded().
withMode(CreateMode.PERSISTENT).
forPath("/liaryank/node","1".getBytes());
//修改
Stat stat = new Stat();
curatorFramework.getData().storingStatIn(stat).forPath("/liaryank/node");
curatorFramework.setData().withVersion(stat.getVersion()).forPath("/liaryank/node","abc".getBytes());
//删除 删除就写入你要删除的节点路径就好
curatorFramework.delete().deletingChildrenIfNeeded().
forPath("/liaryank/node");
curatorFramework.close();
}
}
自己创建、删除、修改注释下多试试然后用客户端检查一下操作结果
相比与之前的原生就舒服多了,简洁明了,而且多层级快速构建。
下面我们来演示一下监听机制:
首先引入一下jar包
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
下面有三个方法
调用时逐个注释观察,你就会发现其中的奥妙。
package com.test.demo1;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
/**
* @Title: Spring-distributed
* @Description:增删改和事务监听
* @author: liaryank
* @Date: 2020/5/6 11:26 上午
* @Version: 1.0
*/
public class Curator {
public static void main(String[] args) throws Exception {
CuratorFramework curatorFramework = CuratorFrameworkFactory.builder().connectString("192.168.3.32,192.168.3.33,192.168.3.34")
.sessionTimeoutMs(4000).
//重试机制
retryPolicy(new ExponentialBackoffRetry(1000,3)).namespace("Curator").build();
curatorFramework.start();
//监听当前这个节点的增删改
//ListenerNodeCache(curatorFramework,"/liaryank");
//监听当前这个节点的子节点的增删改
//ListenerPathChildCache(curatorFramework,"/liaryank");
//监听当前节点及子节点的增删改
TreeNode(curatorFramework,"/liaryank");
System.in.read();
}
/***
* PatchChildCache 监听一个节点下的子节点创建、更新、删除
* NodeCache 监听一个节点的更新和创建事件
* TreeNode 综合PatchChildCache 、NodeCache特性
*/
public static void ListenerPathChildCache(CuratorFramework curatorFramework, String path) throws Exception {
PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework,path,true);
PathChildrenCacheListener pathChildrenCacheListener = new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
System.out.println("Receive Event:"+ pathChildrenCacheEvent.getType());
}
};
pathChildrenCache.getListenable().addListener(pathChildrenCacheListener);
pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);
}
public static void ListenerNodeCache(CuratorFramework curatorFramework, String path) throws Exception {
final NodeCache nodeCache = new NodeCache(curatorFramework,path,false);
NodeCacheListener nodeCacheListener = new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("Receive Event:"+nodeCache.getCurrentData().getPath());
}
};
nodeCache.getListenable().addListener(nodeCacheListener);
nodeCache.start();
}
public static void TreeNode(CuratorFramework curatorFramework, String path) throws Exception {
TreeCache treeCache = new TreeCache(curatorFramework, path);
TreeCacheListener treeCacheListener = new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
System.out.println(treeCacheEvent.getType()+"==>"+treeCacheEvent.getData().getPath());
}
};
treeCache.getListenable().addListener(treeCacheListener);
treeCache.start();
}
}
操作截图
这种事件监听是不是比原生的舒服多了,就很nice。