Curator實(shí)現(xiàn)zookeeper的節(jié)點(diǎn)監(jiān)聽詳解
Curator實(shí)現(xiàn)zookeeper的節(jié)點(diǎn)監(jiān)聽
Curtor框架中一共有三個實(shí)現(xiàn)監(jiān)聽的方式 一種是NodeCache監(jiān)聽指定節(jié)點(diǎn) 一種是pathChildrenCache監(jiān)聽子節(jié)點(diǎn) 一種是TreeCache可以監(jiān)控所有節(jié)點(diǎn) 相當(dāng)于以上兩種的合集
引入依賴
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.3.0</version>
</dependency>創(chuàng)建一個測試類 連接好客戶端
public class CuratorTest {
private CuratorFramework curatorFramework;
@Before
public void testCreate() {
ExponentialBackoffRetry retry = new ExponentialBackoffRetry(2, 10);
curatorFramework = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.connectionTimeoutMs(60 * 1000)
.sessionTimeoutMs(15 * 10000)
.retryPolicy(retry).build();
curatorFramework.start();
}
@After
public void close() {
if (curatorFramework != null) {
curatorFramework.close();
}
}
}Watch監(jiān)聽之NodeCache
監(jiān)聽一個指定節(jié)點(diǎn)
@Test
public void testUpdate() throws Exception {
//監(jiān)聽一個節(jié)點(diǎn)
NodeCache nodeCache = new NodeCache(curatorFramework,"/dongwuyuan");
//注冊監(jiān)聽
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("節(jié)點(diǎn)變化了?。。?!");
}
});
//開啟監(jiān)聽 參數(shù) 如果設(shè)置為true 則開啟監(jiān)聽時加載緩存數(shù)據(jù)
nodeCache.start(true);
while (true){
}
}set /dongwuyuan "laohu"

Watch監(jiān)聽之PathChildrenCache
監(jiān)聽子節(jié)點(diǎn)的變化
@Test
public void testPathChildrenCache() throws Exception {
// 參數(shù) 客戶端,路徑 ,緩存數(shù)據(jù),是否壓縮,線程池
PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework,"/dongwuyuan",true);
//綁定監(jiān)聽器
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
System.out.println("子節(jié)點(diǎn)變化了");
System.out.println(pathChildrenCacheEvent);
//監(jiān)聽子節(jié)點(diǎn)的變更,并且拿到變更后的數(shù)據(jù)
PathChildrenCacheEvent.Type type = pathChildrenCacheEvent.getType();
//判斷類型是否是update
if(type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
//拿到數(shù)據(jù)
byte[] data = pathChildrenCacheEvent.getData().getData();
System.out.println(data);
}
}
});
//開啟監(jiān)聽
pathChildrenCache.start();
while (true){
}
}watch監(jiān)聽之TreeCache
/**
* TreeCache:監(jiān)聽節(jié)點(diǎn)自己和所有子節(jié)點(diǎn)們
*/
@Test
public void testTreeCache(){
//1.創(chuàng)建監(jiān)聽器
TreeCache treeCache = new TreeCache(curatorFramework, "/dongwuyuan");
//2.注冊監(jiān)聽
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
System.out.println("節(jié)點(diǎn)變化了");
System.out.println(event);
}
});
//開啟監(jiān)聽
try {
treeCache.start();
while (true){
}
} catch (Exception e) {
e.printStackTrace();
}
}[zk: localhost:2181(CONNECTED) 13] delete /dongwuyuan/node1

[zk: localhost:2181(CONNECTED) 15] set /dongwuyuan/node2 "shizi"

完整代碼
public class CuratorTest {
private CuratorFramework curatorFramework;
@Before
public void testCreate() {
ExponentialBackoffRetry retry = new ExponentialBackoffRetry(2, 10);
curatorFramework = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.connectionTimeoutMs(60 * 1000)
.sessionTimeoutMs(15 * 10000)
.retryPolicy(retry).build();
curatorFramework.start();
}
@Test
public void testUpdate() throws Exception {
//監(jiān)聽一個節(jié)點(diǎn)
NodeCache nodeCache = new NodeCache(curatorFramework,"/dongwuyuan");
//注冊監(jiān)聽
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("節(jié)點(diǎn)變化了!?。?!");
}
});
//開啟監(jiān)聽 參數(shù) 如果設(shè)置為true 則開啟監(jiān)聽時加載緩存數(shù)據(jù)
nodeCache.start(true);
while (true){
}
}
@Test
public void testPathChildrenCache() throws Exception {
// 參數(shù) 客戶端,路徑 ,緩存數(shù)據(jù),是否壓縮,線程池
PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework,"/dongwuyuan",true);
//綁定監(jiān)聽器
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
System.out.println("子節(jié)點(diǎn)變化了");
System.out.println(pathChildrenCacheEvent);
//監(jiān)聽子節(jié)點(diǎn)的變更,并且拿到變更后的數(shù)據(jù)
PathChildrenCacheEvent.Type type = pathChildrenCacheEvent.getType();
//判斷類型是否是update
if(type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
//拿到數(shù)據(jù)
byte[] data = pathChildrenCacheEvent.getData().getData();
System.out.println(data);
}
}
});
//開啟監(jiān)聽
pathChildrenCache.start();
while (true){
}
}
/**
* TreeCache:監(jiān)聽節(jié)點(diǎn)自己和所有子節(jié)點(diǎn)們
*/
@Test
public void testTreeCache(){
//1.創(chuàng)建監(jiān)聽器
TreeCache treeCache = new TreeCache(curatorFramework, "/dongwuyuan");
//2.注冊監(jiān)聽
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
System.out.println("節(jié)點(diǎn)變化了");
System.out.println(event);
}
});
//開啟監(jiān)聽
try {
treeCache.start();
while (true){
}
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void close() {
if (curatorFramework != null) {
curatorFramework.close();
}
}
}到此這篇關(guān)于Curator實(shí)現(xiàn)zookeeper的節(jié)點(diǎn)監(jiān)聽詳解的文章就介紹到這了,更多相關(guān)zookeeper節(jié)點(diǎn)監(jiān)聽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot v2.0.3版本多數(shù)據(jù)源配置方法
這篇文章主要介紹了springboot v2.0.3版本多數(shù)據(jù)源配置方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-11-11
深度解析Java中的國際化底層類ResourceBundle
做項(xiàng)目應(yīng)該都會實(shí)現(xiàn)國際化,那么大家知道Java底層是如何實(shí)現(xiàn)國際化的嗎?這篇文章就來和大家深度解析一下Java中的國際化底層類ResourceBundle,希望對大家有所幫助2023-03-03
在 Spring Boot 中實(shí)現(xiàn)異常處理最佳實(shí)踐
本文介紹如何在Spring Boot中實(shí)現(xiàn)異常處理,涵蓋核心概念、實(shí)現(xiàn)方法、與先前查詢的集成、性能分析、常見問題和最佳實(shí)踐,感興趣的朋友一起看看吧2025-04-04
JavaEE7+Websockets+GlassFish4打造聊天室
Java EE 7已經(jīng)發(fā)布很久了,新增加了很多新的功能和特性,如新增或更新了不少的JSR標(biāo)準(zhǔn)。其中特別受到關(guān)注的是Websockets。它的一個好處之一是減少了不必要的網(wǎng)絡(luò)流量。它主要是用于在客戶機(jī)和服務(wù)器之間建立單一的雙向連接。2015-09-09
全面解析Spring Security 過濾器鏈的機(jī)制和特性
這篇文章主要介紹了Spring Security 過濾器鏈的機(jī)制和特性,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Java線程監(jiān)聽,意外退出線程后自動重啟的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫ava線程監(jiān)聽,意外退出線程后自動重啟的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03

