Springboot整合zookeeper實現(xiàn)對節(jié)點的創(chuàng)建、監(jiān)聽與判斷的案例詳解
Springboot整合zookeeper教程
1.環(huán)境準備
- zookeeper集群環(huán)境
- 一個簡單的springboot項目環(huán)境
不懂如何搭建zookeeper集群的小伙伴可以移步到我的另一篇文章喔,里面有詳細的zookeeper集群搭建教程~
zookeeper集群搭建步驟(超詳細)
http://www.dbjr.com.cn/article/252826.htm
2.代碼編寫
2.1.在pom.xml文件中增加zookeeper依賴(記得跟自己的zookeeper版本對應)
除此之外,我們也引入junit和log4j方便我們后面的測試
<!--引入對應的zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.1</version>
</dependency>
<!--引入日志-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.17.2</version>
</dependency>
<!--引入junit測試-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>2.2.API測試
我們創(chuàng)建zkClient類用于實現(xiàn)對zookeeper中節(jié)點的操作:
1.客戶端初始化
package com.canrioyuan.zookeepertest;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
//創(chuàng)建對應的zookeeper客戶端
public class ZkClient {
//對應的zookeeper客戶端連接,連接之間不能存在空格
private String connectString = "192.168.154.133:2181,192.168.154.134:2181,192.168.154.135:2181";
//即超時時間,我們設置成2s
private int sessionTimeout = 2000;
//聲明zookeeper客戶端
private ZooKeeper zkClient;
//初始化
@Test
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
}
});
}
}運行測試,成功后如會出現(xiàn)下圖所示結果:

2.創(chuàng)建節(jié)點
//創(chuàng)建節(jié)點
@Test
public void create() throws InterruptedException, KeeperException {
//public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode)
//上圖中從左到右的參數(shù)分別對應著:你所要創(chuàng)建的節(jié)點的名字,節(jié)點的值,節(jié)點的權限,節(jié)點的類型(持久有序,持久無序,臨時有序,臨時無序)
zkClient.create("/class", "LiHua".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}運行測試,你會發(fā)現(xiàn),程序報了這樣的錯誤;

這是因為我們測試方法是互相獨立的,此時運行測試,客戶端處于未初始化的狀態(tài)。因此,我們在客戶端類的init方法中將@Test注解改成@Before注解,表示在每個測試執(zhí)行之前都必須執(zhí)行初始化方法。
//初始化
@Before
// @Test
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
}
});
}這時候我們再運行,會提示運行成功

我們來到任意一臺zookeeper客戶端中查看節(jié)點,會發(fā)現(xiàn)已經(jīng)創(chuàng)建成功

3.監(jiān)聽節(jié)點的狀態(tài)
一次監(jiān)聽只能生效一次,而為了能夠持續(xù)監(jiān)聽,我們對初始化方法再做出調(diào)整,保證每次初始化都有一次監(jiān)聽生效。
@Before
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override //process中即為監(jiān)聽后做出的處理
public void process(WatchedEvent watchedEvent) {
List<String> children = null;
try {
children = zkClient.getChildren("/", true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("-------------------------");
for (String child : children) {
System.out.println(child);
}
System.out.println("-------------------------");
}
});
}然后在監(jiān)聽節(jié)點代碼中進行延時操作
//監(jiān)聽節(jié)點
@Test
public void getChildren() throws InterruptedException, KeeperException {
//正常一次監(jiān)聽只會生效一次,因此我們設置延時監(jiān)聽,確保能夠持續(xù)監(jiān)聽節(jié)點的變化。
Thread.sleep(Long.MAX_VALUE);
}此時我們可以看到控制臺輸出了zookeeper中的節(jié)點:

我們進入zookeeper客戶端對節(jié)點進行操作
增加一個節(jié)點school

返回控制臺,此時已經(jīng)監(jiān)聽到了節(jié)點的變化:

我們再對school這個節(jié)點進行刪除操作

此時控制臺同樣監(jiān)聽到了節(jié)點的變化:

4.判斷節(jié)點是否存在
//判斷節(jié)點是否存在
@Test
public void status() throws InterruptedException, KeeperException {
Stat status = zkClient.exists("/class", false);
System.out.println(status == null ? "no exist" : "exist");
}此時zookeeper中的節(jié)點;

測試結果:

3.全部代碼
package com.canrioyuan.zookeepertest;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
//創(chuàng)建對應的zookeeper客戶端
public class ZkClient {
//對應的zookeeper客戶端連接,連接之間不能存在空格
private String connectString = "192.168.154.133:2181,192.168.154.134:2181,192.168.154.135:2181";
//即超時時間,我們設置成2s
private int sessionTimeout = 2000;
//聲明zookeeper客戶端
private ZooKeeper zkClient;
//初始化
// @Test
@Before
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override //process中即為監(jiān)聽后做出的處理
public void process(WatchedEvent watchedEvent) {
List<String> children = null;
try {
children = zkClient.getChildren("/", true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("-------------------------");
for (String child : children) {
System.out.println(child);
}
System.out.println("-------------------------");
}
});
}
//創(chuàng)建節(jié)點
@Test
public void create() throws InterruptedException, KeeperException {
//public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode)
//上圖中從左到右的參數(shù)分別對應著:你所要創(chuàng)建的節(jié)點的名字,節(jié)點的值,節(jié)點的權限,節(jié)點的類型(持久有序,持久無序,臨時有序,臨時無序)
zkClient.create("/class", "LiHua".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
//監(jiān)聽節(jié)點
@Test
public void getChildren() throws InterruptedException, KeeperException {
//正常一次監(jiān)聽只會生效一次,因此我們設置延時監(jiān)聽,確保能夠持續(xù)監(jiān)聽節(jié)點的變化。
Thread.sleep(Long.MAX_VALUE);
}
//判斷節(jié)點是否存在
@Test
public void status() throws InterruptedException, KeeperException {
Stat status = zkClient.exists("/class", false);
System.out.println(status == null ? "no exist" : "exist");
}
}至此,我們基于Springboot整合zookeeper實現(xiàn)對節(jié)點的創(chuàng)建、監(jiān)聽與判斷教程就結束啦~
到此這篇關于基于Springboot整合zookeeper實現(xiàn)對節(jié)點的創(chuàng)建、監(jiān)聽與判斷的文章就介紹到這了,更多相關Springboot整合zookeeper實現(xiàn)節(jié)點監(jiān)聽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot中實現(xiàn)加載遠程配置的代碼示例
本文章將通過結合consul config來講解在springboot中如何加載遠程配置:通過consul config加載consul server中存儲的配置,需要的朋友可以參考下2023-06-06
springboot之redis cache TTL選項的使用
這篇文章主要介紹了springboot之redis cache TTL選項的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
mybatis教程之動態(tài)sql語句_動力節(jié)點Java學院整理
這篇文章主要介紹了mybatis教程之動態(tài)sql語句,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

