前言
Soul 網(wǎng)關(guān)支持兩種注冊(cè)中心的同步,一種是前文中提到的 nacos,還有一種是今天要講 Zookeeper。
zookeeper 的概要
ZooKeeper 是用于維護(hù)配置信息,命名,提供分布式同步以及提供組服務(wù)的集中式服務(wù)。官網(wǎng)上說(shuō)了很多設(shè)計(jì)目標(biāo),很多都是在高可用性,和易用性這塊;其實(shí)對(duì)于我們來(lái)說(shuō)有一塊內(nèi)容是很重要的,那就是 watch 機(jī)制
watch 機(jī)制
ZooKeeper supports the concept of watches. Clients can set a watch on a znode. A watch will be triggered and removed when the znode changes. When a watch is triggered, the client receives a packet saying that the znode has changed. If the connection between the client and one of the ZooKeeper servers is broken, the client will receive a local notification.
New in 3.6.0: Clients can also set permanent, recursive watches on a znode that are not removed when triggered and that trigger for changes on the registered znode as well as any children znodes recursively.
以上是官網(wǎng)的原文,在這里我大致翻譯一下:Zookeeper 支持 watches 機(jī)制, 客戶(hù)端在 znode 上設(shè)置一個(gè)監(jiān)聽(tīng)器,當(dāng) znode 變化時(shí),監(jiān)聽(tīng)器會(huì)被觸發(fā)或者刪除。 當(dāng)監(jiān)聽(tīng)器觸發(fā)時(shí),客戶(hù)端會(huì)接受到到一個(gè) znode 變化的包。 當(dāng)客戶(hù)端和某個(gè) Zookeeper 服務(wù)端失去連接時(shí), 客戶(hù)端將會(huì)接受一個(gè)本地通知。
啟動(dòng) Zookeeper
大概了解 Zookeeper 的基本概念之后,我們就可以簡(jiǎn)單的使用 Zookeeper 了。這里為了演示方式,直接從 zookeeper docker hub 中起了一個(gè)容器。

插句題外話,這個(gè)界面展示是因?yàn)楸镜匮b了 portainer, 有興趣的可以移到它官網(wǎng) 進(jìn)行了解。
啟動(dòng)之后,我們可以使用命令行的方式進(jìn)行節(jié)點(diǎn)數(shù)據(jù)的查看,還可以通過(guò) ZooInspecter 工具查看。
可以在 zooInspector下載
Soul zookeeper 的配置以及啟動(dòng)
admin 端配置
soul:
sync:
zookeeper:
url: localhost:2181
sessionTimeout: 5000
connectionTimeout: 2000
bootstrap 端配置
- 檢查 pom 文件是否引入包
<!--soul data sync start use zookeeper-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-sync-data-zookeeper</artifactId>
<version>${project.version}</version>
</dependency>
- yml 配置
soul :
sync:
zookeeper:
url: localhost:2181
sessionTimeout: 5000
connectionTimeout: 2000
- 啟動(dòng) admin,啟動(dòng) bootstrap
admin 顯示
Started SoulAdminBootstrap in 19.743 seconds
bootstrap 顯示
Started SoulBootstrapApplication in 7.27 seconds
即為顯示成功
總結(jié)
- 今天講了 Zookeeper 的概念,和 watch 機(jī)制。
- 怎么配置 Soul 中怎么配置 Zookeeper。