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。