SpringBoot3集成Zookeeper的代碼詳解
一、簡(jiǎn)介
ZooKeeper是一個(gè)集中的服務(wù),用于維護(hù)配置信息、命名、提供分布式同步、提供組服務(wù)。分布式應(yīng)用程序以某種形式使用所有這些類型的服務(wù)。
二、環(huán)境搭建
1、修改配置文件
# 1、拷貝一份樣本配置文件 cp zookeeper-3.8.3/conf/zoo_sample.cfg zookeeper-3.8.3/conf/zoo.cfg # 2、修改數(shù)據(jù)文件地址,注意這里用本地路徑 dataDir=/local-path/zookeeper-3.8.3/data # 3、添加一個(gè)配置,處理啟動(dòng)日志的提示:ZooKeeper audit is disabled. audit.enable=true
2、服務(wù)啟動(dòng)
# 1、啟動(dòng)服務(wù)端 zookeeper-3.8.3/bin/zkServer.sh start # 2、停止服務(wù)端 zookeeper-3.8.3/bin/zkServer.sh stop # 3、啟動(dòng)客戶端 zookeeper-3.8.3/bin/zkCli.sh
3、客戶端測(cè)幾個(gè)增刪查的命令
[zk: localhost:2181(CONNECTED) 0] create /cicada smile1 Created /cicada [zk: localhost:2181(CONNECTED) 1] get /cicada smile1 [zk: localhost:2181(CONNECTED) 2] ls / [cicada, zookeeper] [zk: localhost:2181(CONNECTED) 3] delete /cicada
三、工程搭建
1、工程結(jié)構(gòu)
2、依賴管理
Curator是一組Java庫(kù),它讓ZooKeeper的使用變得更加容易,這里的依賴實(shí)際是查詢匹配版本的時(shí)候走了個(gè)捷徑,也可以參考integration-redis
包,熟悉下Spring的封裝策略。
<!-- Zookeeper組件 --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <!-- 包含Curator組件 --> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-zookeeper</artifactId> <version>${spring-integration.version}</version> </dependency>
3、配置文件
配置腳本
zookeeper: #服務(wù)器地址 connectString: 127.0.0.1:2181 #會(huì)話超時(shí)時(shí)間 sessionTimeoutMs: 3000 #連接超時(shí)時(shí)間 connectionTimeoutMs: 60000 #最大重試次數(shù) maxRetries: 3 #初始休眠時(shí)間 baseSleepTimeMs: 1000
配置類
@Configuration public class ZookeeperConfig { @Value("${zookeeper.connectString}") private String connectString; @Value("${zookeeper.baseSleepTimeMs}") private int baseSleepTimeMs; @Value("${zookeeper.maxRetries}") private int maxRetries ; @Value("${zookeeper.connectionTimeoutMs}") int connectionTimeoutMs ; @Value("${zookeeper.sessionTimeoutMs}") int sessionTimeoutMs ; private static CuratorFramework client = null ; /** * 初始化 */ @PostConstruct public void init (){ // 重試策略 RetryPolicy policy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries); // 創(chuàng)建Curator client = CuratorFrameworkFactory.builder() .connectString(connectString) .connectionTimeoutMs(connectionTimeoutMs) .sessionTimeoutMs(sessionTimeoutMs) .retryPolicy(policy).build(); //開(kāi)啟連接 client.start(); } @Bean public CuratorFramework getClient (){ return client ; } }
四、ZooKeeper用法
測(cè)試幾個(gè)API方法,節(jié)點(diǎn)創(chuàng)建和添加數(shù)據(jù),以及判斷和查詢數(shù)據(jù),還有就是基于ZooKeeper提供的讀寫鎖能力。
public class ConfigTest { @Autowired private CuratorFramework client ; @Test public void testCreate () throws Exception { // 創(chuàng)建一個(gè)持久化節(jié)點(diǎn),斷開(kāi)連接時(shí)不會(huì)自動(dòng)刪除 client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/path1"); } @Test public void testExists () throws Exception { // 判斷節(jié)點(diǎn)是否存在,path2不存在所以stat2是null Stat stat1 = client.checkExists().forPath("/path1"); System.out.println(stat1); Stat stat2 = client.checkExists().forPath("/path2"); System.out.println(stat2); } @Test public void testSetData () throws Exception { // 設(shè)置節(jié)點(diǎn)數(shù)據(jù) client.setData().forPath("/path1", "data1".getBytes(StandardCharsets.UTF_8)); } @Test public void testCreateAndSet () throws Exception { // 創(chuàng)建一個(gè)持久化節(jié)點(diǎn)并設(shè)置節(jié)點(diǎn)數(shù)據(jù) client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT) .forPath("/path3","data3".getBytes(StandardCharsets.UTF_8)); } @Test public void testGetData () throws Exception { // 查詢節(jié)點(diǎn)數(shù)據(jù) byte[] data = client.getData().forPath("/path3"); System.out.println(new String(data,StandardCharsets.UTF_8)); } @Test public void testDelete () throws Exception { // 刪除節(jié)點(diǎn) client.delete().guaranteed().deletingChildrenIfNeeded().forPath("/path3"); } @Test public void testReadLock () throws Exception { // 讀寫鎖-讀 InterProcessReadWriteLock lock = new InterProcessReadWriteLock(client,"/lock-read"); lock.readLock().acquire(); System.out.println("獲取-ReadLock"); lock.readLock().release(); } @Test public void testWriteLock () throws Exception { // 讀寫鎖-寫 InterProcessReadWriteLock lock = new InterProcessReadWriteLock(client,"/lock-write"); lock.writeLock().acquire(); System.out.println("獲取-WriteLock"); lock.writeLock().release(); } }
五、參考源碼
文檔倉(cāng)庫(kù):
https://gitee.com/cicadasmile/butte-java-note
源碼倉(cāng)庫(kù):
https://gitee.com/cicadasmile/butte-spring-parent
以上就是SpringBoot3集成Zookeeper的代碼詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot3集成Zookeeper的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot項(xiàng)目使用內(nèi)置的單機(jī)任務(wù)調(diào)度功能詳解
這篇文章主要介紹了SpringBoot項(xiàng)目使用內(nèi)置的單機(jī)任務(wù)調(diào)度功能詳解,SpringBoot框架中提供了2個(gè)注解來(lái)讓開(kāi)發(fā)者快速配置來(lái)實(shí)現(xiàn)單機(jī)定時(shí)任務(wù)調(diào)度的功能,分別是@EnableScheduling和 @Scheduled,需要的朋友可以參考下2024-01-01RocketMQ中消費(fèi)者的消費(fèi)進(jìn)度管理
這篇文章主要介紹了RocketMQ中消費(fèi)者的消費(fèi)進(jìn)度管理,業(yè)務(wù)實(shí)現(xiàn)消費(fèi)回調(diào)的時(shí)候,當(dāng)且僅當(dāng)此回調(diào)函數(shù)返回ConsumeConcurrentlyStatus.CONSUME_SUCCESS ,RocketMQ才會(huì)認(rèn)為這批消息(默認(rèn)是1條)是消費(fèi)完成的,需要的朋友可以參考下2023-10-10springboot自帶的緩存@EnableCaching用法
這篇文章主要介紹了springboot自帶的緩存@EnableCaching用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)詳細(xì)教程
使用SSM(Spring、SpringMVC和Mybatis)已經(jīng)有段時(shí)間了,項(xiàng)目在技術(shù)上已經(jīng)沒(méi)有什么難點(diǎn)了,基于現(xiàn)有的技術(shù)就可以實(shí)現(xiàn)想要的功能,下面這篇文章主要給大家介紹了關(guān)于整合SSM框架:Spring MVC + Spring + MyBatis的相關(guān)資料,需要的朋友可以參考下。2017-07-07Java NIO.2 使用Path接口來(lái)監(jiān)聽(tīng)文件、文件夾變化
Java7對(duì)NIO進(jìn)行了大的改進(jìn),新增了許多功能,接下來(lái)通過(guò)本文給大家介紹Java NIO.2 使用Path接口來(lái)監(jiān)聽(tīng)文件、文件夾變化 ,需要的朋友可以參考下2019-05-05SpringBoot同一個(gè)方法操作多個(gè)數(shù)據(jù)源保證事務(wù)一致性
本文探討了在Spring Boot應(yīng)用中,如何在同一個(gè)方法中操作多個(gè)數(shù)據(jù)源并保證事務(wù)的一致性,由于聲明式事務(wù)的限制,直接使用@Transactional注解無(wú)法滿足需求,文章介紹了解決方案:編程式事務(wù),它允許在代碼級(jí)別更靈活地管理事務(wù),確保多數(shù)據(jù)源操作的事務(wù)一致性2024-11-11Java多線程編程之CountDownLatch同步工具使用實(shí)例
這篇文章主要介紹了Java多線程編程之CountDownLatch同步工具使用實(shí)例,需要的朋友可以參考下2015-05-05Spring Boot 在啟動(dòng)時(shí)進(jìn)行配置文件加解密的方法詳解
這篇文章主要介紹了Spring Boot 在啟動(dòng)時(shí)進(jìn)行配置文件加解密的方法,本文通過(guò)實(shí)例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06