文章目錄
- 一、Zookeeper JavaAPI操作
- 1、Curator介紹
- 2、創建、查詢、修改、刪除節點
- 3、Watch事件監聽
- 二、Zookeeper分布式鎖原理
一、Zookeeper JavaAPI操作
1、Curator介紹
- Curator是Apache Zookeeper的Java客戶端。
- 常見的Zookeeper Java API:
- 原生Java API。
- ZkClient。
- Curator。
- Curator項目目標是簡化Zookeeper客戶端的使用。
- Curator最初是Netfix研發的,后來捐獻了Apache基金會,目前是Apache的頂級項目。
- 官網:https://curator.apache.org/docs/about
2、創建、查詢、修改、刪除節點
public class CuratorTest {private CuratorFramework client;@Beforepublic void init() {// 1、方式一RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("localhost:2181", 60 * 1000, 15 * 1000, retryPolicy);// 2、方式二CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181").sessionTimeoutMs(60 * 1000).connectionTimeoutMs(15 * 1000).retryPolicy(retryPolicy).namespace("test").build();// 開啟連接client.start();this.client = client;}/*** 1、基本創建:client.create().forPath("/app1")* 2、創建節點,帶有數據:client.create().forPath("/app1", data)* 3、設置節點的類型: client.create().withMode(CreateMode.EPHEMERAL).forPath("/app1")* 4、創建多級節點: client.create().creatingParentsIfNeeded().forPath("/app1/app2")*/@Testpublic void testCreate() {// 1、基本創建// 如果創建節點,沒有指定數據,則默認將當前客戶端的ip作為數據存儲try {String path = client.create().forPath("/app1");System.out.println(path);} catch (Exception e) {throw new RuntimeException(e);}}/*** 查詢節點:* 1、查詢數據:get* 2、查詢子節點: ls* 3、查詢節點狀態信息: ls -s*/@Testpublic void testQueryData() {// 1、查詢數據: gettry {byte[] data = client.getData().forPath("/app1");System.out.println(new String(data));} catch (Exception e) {throw new RuntimeException(e);}}@Testpublic void testQueryChildren() {// 查詢子節點: lstry {List<String> stringList = client.getChildren().forPath("/");System.out.println(stringList);} catch (Exception e) {throw new RuntimeException(e);}}@Testpublic void testQueryState() {// 查詢節點狀態信息: ls -sStat status = new Stat();try {client.getData().storingStatIn(status).forPath("/app1");System.out.println(status);} catch (Exception e) {throw new RuntimeException(e);}}/*** 修改數據:* 1、修改數據。* 2、根據版本修改*/@Testpublic void testSet() {// 修改數據try {client.setData().forPath("/app1", "haha".getBytes());} catch (Exception e) {throw new RuntimeException(e);}}@Testpublic void testSetForVersion() throws Exception {// 根據版本修改Stat stat = new Stat();// 查詢節點狀態信息: ls -sclient.getData().storingStatIn(stat).forPath("/app1");int version = stat.getVersion();client.setData().withVersion(version).forPath("/app1", "hehe".getBytes());}/*** 刪除節點:delete、deleteall* 1、刪除單個節點* 2、刪除帶有子節點的節點* 3、必須成功的刪除: 為了防止網絡抖動。本質就是重試。* 4、回調*/@Testpublic void testSingleDelete() throws Exception {// 1、刪除單個節點client.delete().forPath("/app1");// 2、刪除帶有子節點的節點client.delete().deletingChildrenIfNeeded().forPath("/app1");// 3、必須成功刪除client.delete().guaranteed().forPath("/app1");//4、回調client.delete().guaranteed().inBackground((client, event) -> {System.out.println("我被刪除了");System.out.println(event);}).forPath("/app1");}@Afterpublic void close() {if (client != null) {client.close();}}
}
3、Watch事件監聽
- Zookeeper允許用戶在指定節點上注冊一些Watcher,并且在一些特定事件觸發的時候,Zookeeper服務端會將事件通知到感興趣的客戶端上去,該機制是Zookeeper實現分布式協調服務的重要性。
- Zookeeper中引入了Watcher機制來實現了發布/訂閱功能,能夠讓多個訂閱者同時監聽某一個對象,當一個對象自身狀態變化時,會通知所有訂閱者。
- Zookeeper原生支持通過注冊Watcher來進行事件監聽,但是其使用并不是特別方便需要開發人員自己反復注冊Watcher,比較繁瑣。
- Curator引入了Cache來實現對Zookeeper服務端事件的監聽。
- Zookeeper提供了三種Watcher:
- NodeCache:只是監聽某一個特定的節點。
- PathChildrenCache:監控一個ZNode的子節點。
- TreeCache:可以監控整個樹上所有節點,類似于PathChildrenCache和NodeCache的組合。
二、Zookeeper分布式鎖原理
