zookeeper+Springboot實(shí)現(xiàn)服務(wù)器動(dòng)態(tài)上下線監(jiān)聽(tīng)教程詳解
zookeeper+Springboot實(shí)現(xiàn)服務(wù)器動(dòng)態(tài)上下線監(jiān)聽(tīng)教程
一.什么是服務(wù)器動(dòng)態(tài)上下線監(jiān)聽(tīng)
客戶(hù)端能夠?qū)崟r(shí)洞察到服務(wù)器上下線的變化,現(xiàn)在我們看看下面三個(gè)變化給集群、服務(wù)器、客戶(hù)端三者的變化
初始情況

服務(wù)器3啟動(dòng)

服務(wù)器2下線

從上面的圖我們可以知道,在集群中,每當(dāng)一臺(tái)服務(wù)器上線時(shí),都會(huì)在集群中注冊(cè)一個(gè)有序且臨時(shí)的節(jié)點(diǎn),并通知客戶(hù)端;在服務(wù)器下線的時(shí)候,服務(wù)器所注冊(cè)的節(jié)點(diǎn)也會(huì)被刪除,并通知客戶(hù)端。在這樣的結(jié)構(gòu)下,客戶(hù)端便能夠通過(guò)集群實(shí)時(shí)監(jiān)聽(tīng)服務(wù)器的上下線。
二.為什么要實(shí)現(xiàn)對(duì)服務(wù)器上下線的監(jiān)聽(tīng)
在開(kāi)發(fā)中,這種結(jié)構(gòu)應(yīng)用的非常廣泛,核心用處如下:
- 用于監(jiān)聽(tīng)節(jié)點(diǎn)數(shù)據(jù)產(chǎn)生的變化,在zk中可以配置集群的通用配置,當(dāng)配置數(shù)據(jù)發(fā)生了變化之后通知所有訂閱該節(jié)點(diǎn)的Watcher,該節(jié)點(diǎn)發(fā)生事件類(lèi)型
- 用于監(jiān)聽(tīng)節(jié)點(diǎn)狀態(tài)的變化,比如創(chuàng)建一個(gè)節(jié)點(diǎn)、刪除一個(gè)節(jié)點(diǎn)等對(duì)節(jié)點(diǎn)的操作
- 管理客戶(hù)端與服務(wù)端連接的生命周期
三.編碼實(shí)現(xiàn)
建議大家在實(shí)現(xiàn)之前可以先對(duì)zookeeper的一些API操作有一些了解,這些我在我的另外一篇文章做了詳細(xì)的介紹,有需要的小伙伴可以移步去看看喔。
基于Springboot整合zookeeper實(shí)現(xiàn)對(duì)節(jié)點(diǎn)的創(chuàng)建、監(jiān)聽(tīng)與判斷
http://www.dbjr.com.cn/article/252816.htm
1.在集1.在zookeeper集群中創(chuàng)建一個(gè)servers節(jié)點(diǎn)
create /servers "servers"

2.創(chuàng)建一個(gè)簡(jiǎn)單的springboot工程并引入zookeeper

<!--引入對(duì)應(yīng)的zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.1</version>
</dependency>3.編寫(xiě)DistributeServer(分布式服務(wù)端)
package com.canrioyuan.zookeepertest.zkcase1;
import org.apache.zookeeper.*;
import java.io.IOException;
//分布式服務(wù)器端
public class DistributeServer {
//對(duì)應(yīng)的zookeeper客戶(hù)端連接,連接之間不能存在空格
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服務(wù)器端
private ZooKeeper zkServer;
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
//1.獲取zookeeper連接
DistributeServer distributeServer = new DistributeServer();
distributeServer.getConnect();
//2.注冊(cè)服務(wù)器
distributeServer.register(args[0]);
//3.啟動(dòng)
distributeServer.business(args[0]);
}
private void business(String url) throws InterruptedException {
System.out.println("url為" + url + "的服務(wù)器正在工作中");
Thread.sleep(Long.MAX_VALUE);
}
private void register(String url) throws InterruptedException, KeeperException {
//創(chuàng)建一個(gè)節(jié)點(diǎn)
zkServer.create("/servers/" + url, url.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("url為" + url + "的服務(wù)器已經(jīng)上線");
}
//創(chuàng)建到zookeeper的客戶(hù)端連接
private void getConnect() throws IOException {
zkServer = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
}
});
}
}4.分布式客戶(hù)端
package com.canrioyuan.zookeepertest.zkcase1;
import org.apache.zookeeper.*;
import java.io.IOException;
import java.util.List;
//分布式客戶(hù)端
public class DistributeClient {
//對(duì)應(yīng)的zookeeper客戶(hù)端連接,連接之間不能存在空格
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客戶(hù)端
private static ZooKeeper zkClient;
private final String PARENT_NODE = "/servers";
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
//1.獲取zookeeper連接
DistributeClient distributeClient = new DistributeClient();
distributeClient.getConnect();
//2.獲取servers中的子節(jié)點(diǎn)信息
distributeClient.getServerList();
//3.業(yè)務(wù)邏輯
distributeClient.business();
}
private void business() throws InterruptedException {
System.out.println("客戶(hù)端正在工作中......");
Thread.sleep(Long.MAX_VALUE);
}
private void getServerList() throws InterruptedException, KeeperException {
List<String> children = zkClient.getChildren(PARENT_NODE, true);
for (String child : children) {
System.out.println(child);
}
}
private void getConnect() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
getServerList();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
}
}
});
}
}四.測(cè)試
1.啟動(dòng)客戶(hù)端,開(kāi)啟監(jiān)聽(tīng)

2.按照下面的流程啟動(dòng)服務(wù)器端

輸入對(duì)應(yīng)的服務(wù)器名,如我輸入的就是:server1

啟動(dòng)成功后結(jié)果如下所示:

關(guān)注客戶(hù)端的監(jiān)聽(tīng)狀況,發(fā)現(xiàn)已經(jīng)監(jiān)聽(tīng)到集群中已經(jīng)出現(xiàn)了一個(gè)節(jié)點(diǎn)

為了使輸出更明顯,我們?cè)趜ookeeper中的servers節(jié)點(diǎn)下創(chuàng)建一個(gè)server2的持久節(jié)點(diǎn)

此時(shí)客戶(hù)端也監(jiān)聽(tīng)到了集群中節(jié)點(diǎn)的變化

4.此時(shí)我們關(guān)閉服務(wù)器端,再來(lái)觀察一下客戶(hù)端的監(jiān)聽(tīng)情況
此時(shí)我們發(fā)現(xiàn)客戶(hù)端中只輸出了server2這個(gè)節(jié)點(diǎn)

至此,我們便成功實(shí)現(xiàn)了簡(jiǎn)單的zookeeper集群對(duì)服務(wù)器端上下線的監(jiān)聽(tīng)。
至此,我們zookeeper+Springboot實(shí)現(xiàn)服務(wù)器動(dòng)態(tài)上下線監(jiān)聽(tīng)教程就結(jié)束啦~
到此這篇關(guān)于zookeeper+Springboot實(shí)現(xiàn)服務(wù)器動(dòng)態(tài)上下線監(jiān)聽(tīng)的文章就介紹到這了,更多相關(guān)zookeeper動(dòng)態(tài)上下線監(jiān)聽(tīng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中帶參數(shù)的try(){}語(yǔ)法含義詳解
這篇文章主要介紹了java中帶參數(shù)的try(){}語(yǔ)法含義詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java如何在沙箱環(huán)境中測(cè)試支付寶支付接口
這篇文章主要介紹了Java如何在沙箱環(huán)境中測(cè)試支付寶支付接口,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Java中FileOutputStream類(lèi)的使用
java.io.FileOutputStream類(lèi)是文件輸出流,用于將數(shù)據(jù)寫(xiě)出到文件,下面就來(lái)介紹一下Java中FileOutputStream類(lèi)的使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
SpringBoot異步調(diào)用方法實(shí)現(xiàn)場(chǎng)景代碼實(shí)例
這篇文章主要介紹了SpringBoot異步調(diào)用方法實(shí)現(xiàn)場(chǎng)景代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Spring Boot Mysql 數(shù)據(jù)庫(kù)操作示例
本篇文章主要介紹了Spring Boot Mysql 數(shù)據(jù)庫(kù)操作示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Java實(shí)現(xiàn)SHA-1算法實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)SHA-1算法,實(shí)例分析了java實(shí)現(xiàn)SHA-1算法的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
java實(shí)現(xiàn)圖片任意角度旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖片任意角度旋轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04

