+ Curator入門

https://www.jianshu.com/p/db65b64f38aa簡介Curator是Netflix公司開源的一套zookeeper客戶端框架,解決了很多Zookeeper客戶端非常底層的細節開發工作,包括連接重連、反覆註冊Watcher和NodeExistsException異常等等。Patrixck Hunt(Zookeeper)以一句“Guava is to Java that Curator to Zookeeper”給Curator予高度評價。
Curator的maven依賴

[AppleScript] 純文本查看 複製代碼

?

01

02

03

04

05

06

07

08

09

10

11

12

<!-- 對zookeeper的底層api的一些封裝 -->

        <dependency>

            <groupId>org.apache.curator</groupId>

            <artifactId>curator-framework</artifactId>

            <version>2.12.0</version>

        </dependency>

        <!-- 封裝了一些高級特性,如:Cache事件監聽、選舉、分佈式鎖、分佈式Barrier -->

        <dependency>

            <groupId>org.apache.curator</groupId>

            <artifactId>curator-recipes</artifactId>

            <version>2.12.0</version>

        </dependency>

基本API創建會話使用靜態工程方法創建

[AppleScript] 純文本查看 複製代碼

?

1

2

3

RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);

CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.128.129:2181",

        5000, 5000, retryPolicy);



其中RetryPolicy爲重試策略,第一個參數爲baseSleepTimeMs初始的sleep時間,用於計算之後的每次重試的sleep時間。第二個參數爲maxRetries,最大重試次數。
使用Fluent風格api創建

[AppleScript] 純文本查看 複製代碼

?

1

2

3

4

5

6

7

8

9

RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);

CuratorFramework client = CuratorFrameworkFactory.builder()

        .connectString("192.168.128.129:2181")

        .sessionTimeoutMs(5000)  // 會話超時時間

        .connectionTimeoutMs(5000) // 連接超時時間

        .retryPolicy(retryPolicy)

        .namespace("base") // 包含隔離名稱

        .build();

client.start();



創建數據節點

[AppleScript] 純文本查看 複製代碼

?

1

2

3

client.create().creatingParentContainersIfNeeded() // 遞歸創建所需父節點

                .withMode(CreateMode.PERSISTENT) // 創建類型爲持久節點

                .forPath("/nodeA", "init".getBytes()); // 目錄及內容



刪除數據節點

[AppleScript] 純文本查看 複製代碼

?

1

2

3

4

5

client.delete()

      .guaranteed()  // 強制保證刪除

      .deletingChildrenIfNeeded() // 遞歸刪除子節點

      .withVersion(10086) // 指定刪除的版本號

      .forPath("/nodeA");



讀取數據節點讀數據,返回值爲byte[]

[AppleScript] 純文本查看 複製代碼

?

1

2

byte[] bytes = client.getData().forPath("/nodeA");

        System.out.println(new String(bytes));



讀stat

[AppleScript] 純文本查看 複製代碼

?

1

2

3

4

Stat stat = new Stat();

        client.getData()

                .storingStatIn(stat)

                .forPath("/nodeA");



修改數據節點

[AppleScript] 純文本查看 複製代碼

?

1

2

3

client.setData()

                .withVersion(10086) // 指定版本修改

                .forPath("/nodeA", "data".getBytes());



事務

[AppleScript] 純文本查看 複製代碼

?

1

2

3

4

5

6

7

client.inTransaction().check().forPath("/nodeA")

                .and()

                .create().withMode(CreateMode.EPHEMERAL).forPath("/nodeB", "init".getBytes())

                .and()

                .create().withMode(CreateMode.EPHEMERAL).forPath("/nodeC", "init".getBytes())

                .and()

                .commit();



其他

[AppleScript] 純文本查看 複製代碼

?

1

2

3

client.checkExists() // 檢查是否存在

          .forPath("/nodeA");

  client.getChildren().forPath("/nodeA"); // 獲取子節點的路徑



異步回調異步創建節點

[AppleScript] 純文本查看 複製代碼

?

1

2

3

4

5

6

7

8

Executor executor = Executors.newFixedThreadPool(2);

        client.create()

                .creatingParentsIfNeeded()

                .withMode(CreateMode.EPHEMERAL)

                .inBackground((curatorFramework, curatorEvent) -> {

                    System.out.println(String.format("eventType:%s,resultCode:%s",curatorEvent.getType(),curatorEvent.getResultCode()));

                },executor)

                .forPath("path");



注:本文參考了博客
https://zjcscut.github.io/2018/12/16/zookeeper-curator-usage/#Zookeeper%E5%AE%A2%E6%88%B7%E7%AB%AFCurator%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章