Springboot整合zookeeper實(shí)現(xiàn)對(duì)節(jié)點(diǎn)的創(chuàng)建、監(jiān)聽與判斷的案例詳解
Springboot整合zookeeper教程
1.環(huán)境準(zhǔn)備
- zookeeper集群環(huán)境
- 一個(gè)簡(jiǎn)單的springboot項(xiàng)目環(huán)境
不懂如何搭建zookeeper集群的小伙伴可以移步到我的另一篇文章喔,里面有詳細(xì)的zookeeper集群搭建教程~
zookeeper集群搭建步驟(超詳細(xì))
http://m.fzitv.net/article/252826.htm
2.代碼編寫
2.1.在pom.xml文件中增加zookeeper依賴(記得跟自己的zookeeper版本對(duì)應(yīng))
除此之外,我們也引入junit和log4j方便我們后面的測(cè)試
<!--引入對(duì)應(yīng)的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測(cè)試-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>2.2.API測(cè)試
我們創(chuàng)建zkClient類用于實(shí)現(xiàn)對(duì)zookeeper中節(jié)點(diǎn)的操作:
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)建對(duì)應(yīng)的zookeeper客戶端
public class ZkClient {
//對(duì)應(yīng)的zookeeper客戶端連接,連接之間不能存在空格
private String connectString = "192.168.154.133:2181,192.168.154.134:2181,192.168.154.135:2181";
//即超時(shí)時(shí)間,我們?cè)O(shè)置成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) {
}
});
}
}運(yùn)行測(cè)試,成功后如會(huì)出現(xiàn)下圖所示結(jié)果:

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

這是因?yàn)槲覀儨y(cè)試方法是互相獨(dú)立的,此時(shí)運(yùn)行測(cè)試,客戶端處于未初始化的狀態(tài)。因此,我們?cè)诳蛻舳祟惖膇nit方法中將@Test注解改成@Before注解,表示在每個(gè)測(cè)試執(zhí)行之前都必須執(zhí)行初始化方法。
//初始化
@Before
// @Test
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
}
});
}這時(shí)候我們?cè)龠\(yùn)行,會(huì)提示運(yùn)行成功

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

3.監(jiān)聽節(jié)點(diǎn)的狀態(tài)
一次監(jiān)聽只能生效一次,而為了能夠持續(xù)監(jiān)聽,我們對(duì)初始化方法再做出調(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é)點(diǎn)代碼中進(jìn)行延時(shí)操作
//監(jiān)聽節(jié)點(diǎn)
@Test
public void getChildren() throws InterruptedException, KeeperException {
//正常一次監(jiān)聽只會(huì)生效一次,因此我們?cè)O(shè)置延時(shí)監(jiān)聽,確保能夠持續(xù)監(jiān)聽節(jié)點(diǎn)的變化。
Thread.sleep(Long.MAX_VALUE);
}此時(shí)我們可以看到控制臺(tái)輸出了zookeeper中的節(jié)點(diǎn):

我們進(jìn)入zookeeper客戶端對(duì)節(jié)點(diǎn)進(jìn)行操作
增加一個(gè)節(jié)點(diǎn)school

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

我們?cè)賹?duì)school這個(gè)節(jié)點(diǎn)進(jìn)行刪除操作

此時(shí)控制臺(tái)同樣監(jiān)聽到了節(jié)點(diǎn)的變化:

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

測(cè)試結(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)建對(duì)應(yīng)的zookeeper客戶端
public class ZkClient {
//對(duì)應(yīng)的zookeeper客戶端連接,連接之間不能存在空格
private String connectString = "192.168.154.133:2181,192.168.154.134:2181,192.168.154.135:2181";
//即超時(shí)時(shí)間,我們?cè)O(shè)置成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é)點(diǎn)
@Test
public void create() throws InterruptedException, KeeperException {
//public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode)
//上圖中從左到右的參數(shù)分別對(duì)應(yīng)著:你所要?jiǎng)?chuàng)建的節(jié)點(diǎn)的名字,節(jié)點(diǎn)的值,節(jié)點(diǎn)的權(quán)限,節(jié)點(diǎn)的類型(持久有序,持久無序,臨時(shí)有序,臨時(shí)無序)
zkClient.create("/class", "LiHua".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
//監(jiān)聽節(jié)點(diǎn)
@Test
public void getChildren() throws InterruptedException, KeeperException {
//正常一次監(jiān)聽只會(huì)生效一次,因此我們?cè)O(shè)置延時(shí)監(jiān)聽,確保能夠持續(xù)監(jiān)聽節(jié)點(diǎn)的變化。
Thread.sleep(Long.MAX_VALUE);
}
//判斷節(jié)點(diǎn)是否存在
@Test
public void status() throws InterruptedException, KeeperException {
Stat status = zkClient.exists("/class", false);
System.out.println(status == null ? "no exist" : "exist");
}
}至此,我們基于Springboot整合zookeeper實(shí)現(xiàn)對(duì)節(jié)點(diǎn)的創(chuàng)建、監(jiān)聽與判斷教程就結(jié)束啦~
到此這篇關(guān)于基于Springboot整合zookeeper實(shí)現(xiàn)對(duì)節(jié)點(diǎn)的創(chuàng)建、監(jiān)聽與判斷的文章就介紹到這了,更多相關(guān)Springboot整合zookeeper實(shí)現(xiàn)節(jié)點(diǎn)監(jiān)聽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
啟用springboot security后登錄web頁面需要用戶名和密碼的解決方法
這篇文章主要介紹了啟用springboot security后登錄web頁面需要用戶名和密碼的解決方法,也就是使用默認(rèn)用戶和密碼登錄的操作方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
使用spring框架ResponseEntity實(shí)現(xiàn)文件下載
這篇文章主要介紹了使用spring框架ResponseEntity實(shí)現(xiàn)文件下載,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring?Boot?Shiro?auto-configure工作流程詳解
這篇文章主要為大家介紹了Spring?Boot?Shiro?auto-configure工作流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

