最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成Curator實(shí)現(xiàn)Zookeeper基本操作的代碼示例

 更新時(shí)間:2024年05月14日 10:47:38   作者:Java畢設(shè)王  
Zookeeper是一個(gè)Apache開源的分布式的應(yīng)用,為系統(tǒng)架構(gòu)提供協(xié)調(diào)服務(wù),ZooKeeper的目標(biāo)就是封裝好復(fù)雜易出錯(cuò)的關(guān)鍵服務(wù),將簡單易用的接口和性能高效、功能穩(wěn)定的系統(tǒng)提供給用戶,本文給大家介紹了SpringBoot集成Curator實(shí)現(xiàn)Zookeeper基本操作,需要的朋友可以參考下

Zookeeper是一個(gè)Apache開源的分布式的應(yīng)用,為系統(tǒng)架構(gòu)提供協(xié)調(diào)服務(wù)。從設(shè)計(jì)模式角度來審視:該組件是一個(gè)基于觀察者模式設(shè)計(jì)的框架,負(fù)責(zé)存儲(chǔ)和管理數(shù)據(jù),接受觀察者的注冊,一旦數(shù)據(jù)的狀態(tài)發(fā)生變化,Zookeeper就將負(fù)責(zé)通知已經(jīng)在Zookeeper上注冊的觀察者做出相應(yīng)的反應(yīng),從而實(shí)現(xiàn)集群中類似Master/Slave管理模式。ZooKeeper的目標(biāo)就是封裝好復(fù)雜易出錯(cuò)的關(guān)鍵服務(wù),將簡單易用的接口和性能高效、功能穩(wěn)定的系統(tǒng)提供給用戶。

zookeeper安裝單機(jī)模式

http://www.javacui.com/opensource/445.html

官網(wǎng)

https://curator.apache.org/releases.html#Current_Release

POM引入

<!--                      curator                      -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>5.2.0</version>
</dependency>
<!--                      fastjson                      -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.79</version>
</dependency>

application.yml定義連接屬性

server:
  port: 80
curator:
  connectString: 192.168.3.22:2181 # zookeeper 地址
  retryCount: 1 # 重試次數(shù)
  elapsedTimeMs: 2000 # 重試間隔時(shí)間
  sessionTimeoutMs: 60000 # session超時(shí)時(shí)間
  connectionTimeoutMs: 10000 # 連接超時(shí)時(shí)間

使用Springboot配置讀取

package com.example.springboot.config;
 
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
/**
 * @Auther: Java小強(qiáng)
 * @Date: 2022/2/4 - 19:37
 * @Decsription: com.example.springboot.config
 * @Version: 1.0
 */
@Data
@Component
@ConfigurationProperties(prefix = "curator")
public class CuratorConf {
    private int retryCount;
    private int elapsedTimeMs;
    private String connectString;
    private int sessionTimeoutMs;
    private int connectionTimeoutMs;
}

公用連接創(chuàng)建對(duì)象

package com.example.springboot.tool;
 
import com.example.springboot.config.CuratorConf;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @Auther: Java小強(qiáng)
 * @Date: 2022/2/4 - 19:37
 * @Decsription: com.example.springboot.tool
 * @Version: 1.0
 */
@Configuration
public class ZkConfiguration {
 
    @Autowired
    private CuratorConf curatorConf;
 
    /**
     * 這里會(huì)自動(dòng)調(diào)用一次start,請(qǐng)勿重復(fù)調(diào)用
     */
    @Bean(initMethod = "start")
    public CuratorFramework curatorFramework() {
        return CuratorFrameworkFactory.newClient(
                curatorConf.getConnectString(),
                curatorConf.getSessionTimeoutMs(),
                curatorConf.getConnectionTimeoutMs(),
                new RetryNTimes(curatorConf.getRetryCount(), curatorConf.getElapsedTimeMs()));
    }
 
}

編寫測試類,實(shí)現(xiàn)各種基礎(chǔ)操作,并挨個(gè)測試

package com.example.springboot;
 
import com.alibaba.fastjson.JSON;
import com.example.springboot.tool.ZkConfiguration;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.zookeeper.data.Stat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
 
import java.nio.charset.StandardCharsets;
import java.util.List;
 
/**
 * @Auther: Java小強(qiáng)
 * @Date: 2022/2/4 - 19:33
 * @Decsription: com.example.springboot
 * @Version: 1.0
 */
@SpringBootTest(classes = Application.class)
public class CuratorTest {
    @Autowired
    private ZkConfiguration zk;
 
    // 測試連接
    @Test
    void contextLoads() {
        CuratorFramework client= zk.curatorFramework();
        System.out.println(client.toString());
    }
 
    // 創(chuàng)建節(jié)點(diǎn)
    @Test
    void createPath() throws Exception{
        CuratorFramework client= zk.curatorFramework();
        // 父節(jié)點(diǎn)不存在則創(chuàng)建
        String path = client.create().creatingParentsIfNeeded().forPath("/javacui/p1" ,
                "Java小強(qiáng)博客".getBytes(StandardCharsets.UTF_8));
        System.out.println(path);
        byte[] data = client.getData().forPath("/javacui/p1");
        System.out.println(new String(data));
    }
 
    // 賦值,修改數(shù)據(jù)
    @Test
    void setData() throws Exception{
        CuratorFramework client = zk.curatorFramework();
 
        int version = 0; // 當(dāng)前節(jié)點(diǎn)的版本信息
        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath("/javacui/p1");
        version = stat.getVersion();
        // 如果版本信息不一致,說明當(dāng)前數(shù)據(jù)被修改過,則修改失敗程序報(bào)錯(cuò)
        client.setData().withVersion(version).forPath("/javacui/p1",
                "Java崔的博客".getBytes(StandardCharsets.UTF_8));
        byte[] data = client.getData().forPath("/javacui/p1");
        System.out.println(new String(data));
    }
 
    // 查詢節(jié)點(diǎn)
    @Test
    void getPath() throws Exception{
        CuratorFramework client= zk.curatorFramework();
        // 查內(nèi)容
        byte[] data = client.getData().forPath("/javacui/p1");
        System.out.println(new String(data));
 
        // 查狀態(tài)
        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath("/javacui/p1");
        System.out.println(JSON.toJSONString(stat, true));
    }
 
    // 刪除節(jié)點(diǎn)
    @Test
    void deletePath() throws Exception{
        CuratorFramework client= zk.curatorFramework();
        // deletingChildrenIfNeeded如果有子節(jié)點(diǎn)一并刪除
        // guaranteed必須成功比如網(wǎng)絡(luò)抖動(dòng)時(shí)造成命令失敗
        client.delete().guaranteed().deletingChildrenIfNeeded().inBackground(new BackgroundCallback() {
            @Override
            public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
                System.out.println("刪除成功");
                // { "path":"/javacui/p1","resultCode":0,"type":"DELETE"}
                System.out.println(JSON.toJSONString(curatorEvent, true));
            }
        }).forPath("/javacui/p1");
    }
 
    // 查詢子節(jié)點(diǎn)
    @Test
    void getPaths() throws Exception{
        CuratorFramework client= zk.curatorFramework();
        List<String> paths = client.getChildren().forPath("/javacui");
        for(String p : paths){
            System.out.println(p);
        }
    }
}

以上就是SpringBoot集成Curator實(shí)現(xiàn)Zookeeper基本操作的代碼示例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot Zookeeper基本操作的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot整合Java Web三大件的詳細(xì)過程

    SpringBoot整合Java Web三大件的詳細(xì)過程

    這篇文章主要介紹了SpringBoot整合Java Web三大件的詳細(xì)過程,注冊自定義的Servlet、Filter、Listener組件到springboot內(nèi)嵌的Servlet容器,讓它們發(fā)揮自己的作用,需要的朋友可以參考下
    2025-04-04
  • 關(guān)于springboot集成阿里云短信的問題

    關(guān)于springboot集成阿里云短信的問題

    這篇文章主要介紹了springboot集成阿里云短信的方法,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-11-11
  • 詳解Java中的延時(shí)隊(duì)列 DelayQueue

    詳解Java中的延時(shí)隊(duì)列 DelayQueue

    這篇文章主要介紹了Java中延時(shí)隊(duì)列 DelayQueue的相關(guān)資料,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • java:程序包org.apache.ibatis.annotations不存在報(bào)錯(cuò)解決

    java:程序包org.apache.ibatis.annotations不存在報(bào)錯(cuò)解決

    這篇文章主要給大家介紹了關(guān)于java:程序包org.apache.ibatis.annotations不存在報(bào)錯(cuò)的解決方法,這個(gè)錯(cuò)誤是我在直接導(dǎo)入springboot項(xiàng)目的時(shí)候報(bào)錯(cuò)的,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • javamail實(shí)現(xiàn)注冊激活郵件

    javamail實(shí)現(xiàn)注冊激活郵件

    這篇文章主要為大家詳細(xì)介紹了javamail實(shí)現(xiàn)注冊激活郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Java數(shù)據(jù)導(dǎo)出功能之導(dǎo)出Excel文件實(shí)例

    Java數(shù)據(jù)導(dǎo)出功能之導(dǎo)出Excel文件實(shí)例

    這篇文章主要介紹了Java數(shù)據(jù)導(dǎo)出功能之導(dǎo)出Excel文件實(shí)例,本文給出了jar包的下載地址,并給出了導(dǎo)出Excel文件代碼實(shí)例,需要的朋友可以參考下
    2015-06-06
  • 解析Java中PriorityQueue優(yōu)先級(jí)隊(duì)列結(jié)構(gòu)的源碼及用法

    解析Java中PriorityQueue優(yōu)先級(jí)隊(duì)列結(jié)構(gòu)的源碼及用法

    優(yōu)先級(jí)隊(duì)列是一種隊(duì)列結(jié)構(gòu),是0個(gè)或多個(gè)元素的集合,每個(gè)元素都有一個(gè)優(yōu)先權(quán),PriorityQueue被內(nèi)置于JDK中,本文就來解析Java中PriorityQueue優(yōu)先級(jí)隊(duì)列結(jié)構(gòu)的源碼及用法.
    2016-05-05
  • 詳解Java線程中常用操作

    詳解Java線程中常用操作

    這篇文章主要為大家詳細(xì)介紹了一下Java線程中的一些常用操作,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-05-05
  • Java Durid進(jìn)行JDBC連接詳解

    Java Durid進(jìn)行JDBC連接詳解

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章簡單使用Durid進(jìn)行JDBC連接,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-09-09
  • spring.factories文件的解析源碼API機(jī)制詳解

    spring.factories文件的解析源碼API機(jī)制詳解

    通過本文深入探討Spring?Boot的背景歷史、業(yè)務(wù)場景、功能點(diǎn)以及底層原理,使讀者對(duì)Spring?Boot有了更深入的了解,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-11-11

最新評(píng)論

民权县| 柳林县| 龙山县| 定结县| 浙江省| 西青区| 通化县| 安义县| 吉安市| 三台县| 宁津县| 新巴尔虎左旗| 怀柔区| 准格尔旗| 恩施市| 庆云县| 神木县| 呈贡县| 定南县| 三河市| 拉孜县| 天气| 扎兰屯市| 布尔津县| 改则县| 鸡东县| 荔浦县| 社会| 田阳县| 织金县| 琼海市| 三亚市| 广宁县| 叙永县| 荃湾区| 盘锦市| 满城县| 威宁| 米泉市| 独山县| 永靖县|