Springboot整合zookeeper實(shí)現(xiàn)對(duì)節(jié)點(diǎn)的創(chuàng)建、監(jiān)聽與判斷的案例詳解
Springboot整合zookeeper教程
1.環(huán)境準(zhǔn)備
- zookeeper集群環(huán)境
- 一個(gè)簡單的springboot項(xiàng)目環(huán)境
不懂如何搭建zookeeper集群的小伙伴可以移步到我的另一篇文章喔,里面有詳細(xì)的zookeeper集群搭建教程~
zookeeper集群搭建步驟(超詳細(xì))
http://www.dbjr.com.cn/article/252826.htm
2.代碼編寫
2.1.在pom.xml文件中增加zookeeper依賴(記得跟自己的zookeeper版本對(duì)應(yīng))
除此之外,我們也引入junit和log4j方便我們后面的測試
<!--引入對(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測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency>
2.2.API測試
我們創(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í)間,我們設(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)行測試,成功后如會(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)行測試,你會(huì)發(fā)現(xiàn),程序報(bào)了這樣的錯(cuò)誤;
這是因?yàn)槲覀儨y試方法是互相獨(dú)立的,此時(shí)運(yùn)行測試,客戶端處于未初始化的狀態(tài)。因此,我們在客戶端類的init方法中將@Test注解改成@Before注解,表示在每個(gè)測試執(zhí)行之前都必須執(zhí)行初始化方法。
//初始化 @Before // @Test public void init() throws IOException { zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { } }); }
這時(shí)候我們再運(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ì)生效一次,因此我們設(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)的變化:
我們再對(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);
測試結(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í)間,我們設(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ì)生效一次,因此我們設(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)文章
java實(shí)現(xiàn)一個(gè)簡單的Web服務(wù)器實(shí)例解析
這篇文章主要介紹了java實(shí)現(xiàn)一個(gè)簡單的Web服務(wù)器實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02SpringBoot中實(shí)現(xiàn)加載遠(yuǎn)程配置的代碼示例
本文章將通過結(jié)合consul config來講解在springboot中如何加載遠(yuǎn)程配置:通過consul config加載consul server中存儲(chǔ)的配置,需要的朋友可以參考下2023-06-06springboot之redis cache TTL選項(xiàng)的使用
這篇文章主要介紹了springboot之redis cache TTL選項(xiàng)的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07SpringBoot整合Ldap的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot整合Ldap的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11mybatis教程之動(dòng)態(tài)sql語句_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了mybatis教程之動(dòng)態(tài)sql語句,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09springboot啟動(dòng)時(shí)運(yùn)行代碼詳解
在本篇內(nèi)容中我們給大家整理了關(guān)于在springboot啟動(dòng)時(shí)運(yùn)行代碼的詳細(xì)圖文步驟以及需要注意的地方講解,有興趣的朋友們學(xué)習(xí)下。2019-06-06