1. 程式人生 > 實用技巧 >zookeeper的javaAPI操作(基於Curator的CRUD)

zookeeper的javaAPI操作(基於Curator的CRUD)

Curator介紹

•Curator 是 Apache ZooKeeper 的Java客戶端庫,目標是簡化 ZooKeeper 客戶端的使用。

•Curator 最初是 Netfix 研發的,後來捐獻了 Apache 基金會,目前是 Apache 的頂級專案。

•官網:http://curator.apache.org/

獲得對zookeeper服務端的連線物件:

在使用javaAPI的Curator進行增刪改查的操作之前我們需要獲得對zookeeper服務端的連線物件:

    @Before
    public void ceshiConnect() {
//定義重試策略
        RetryPolicy retryPolicy = new
ExponentialBackoffRetry(3000, 10); client = CuratorFrameworkFactory.builder() .connectString("192.168.31.81:2181") //zookeeper的地址 .sessionTimeoutMs(60 * 1000) //會話超時時間 .connectionTimeoutMs(15 * 1000) //連線超時時間 .retryPolicy(retryPolicy) //
重試策略 .namespace("hui") //名稱空間 .build(); client.start(); }

基於javaAPI的Curator的增加結點:


@Test//建立不帶有資料的結點但是,預設資料是當前客戶端的ip public void testCreate() throws Exception { String forPath = client.create().forPath("/app1"); System.out.println(forPath); } @Test
//建立帶有資料的結點 public void testNodetext() throws Exception { String forPath = client.create().forPath("/app4", "yfsn".getBytes()); System.out.println(forPath); } @Test//建立結點的同時設定結點的型別 public void testMadeTheTypeNode() throws Exception { String forPath = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app6", "yfsn".getBytes()); System.out.println(forPath); while (true) { } } @Test//建立多級結點,建立的多級結點中客戶機的ip是隻存在於葉子結點中的,建立的父節點的資料是空不是null public void testMadeManeyNode() throws Exception { String forPath = client.create().creatingParentsIfNeeded().forPath("/app9/bpp1/bpp2"); System.out.println(forPath); }

基於javaAPI的Curator的查詢結點:

    @Test//查詢資料 get
    public void testGETData() throws Exception {

        byte[] bytes = client.getData().forPath("/app9/bpp1");
        System.out.println(new String(bytes));


    }

    @Test//查詢子節點
    public void testFindChild() throws Exception {
        List<String> list = client.getChildren().forPath("/");//這裡的/其實是對應的/hui
        System.out.println(list);

    }

    @Test//查詢結點的資訊
    public void getStatusforNode() throws Exception {

        Stat stat = new Stat();//狀態容器
        byte[] bytes = client.getData().storingStatIn(stat).forPath("/app9");//往容器中裝填、app9的資訊

        System.out.println(stat);//列印狀態資訊


    }

基於javaAPI的Curator的修改結點:

    @Test//修改資料
    public void testChangeData() throws Exception {

        client.setData().forPath("/app9", "yfsn".getBytes());

    }

    @Test//根據版本修改,每一次的修改之後版本都會加1
    public void testSetData() throws Exception {
        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath("/app9");
        int version = stat.getVersion();
        System.out.println(version);

        client.setData().withVersion(version).forPath("/app9", "zyh1".getBytes());
        client.getData().storingStatIn(stat).forPath("/app9");
        System.out.println(stat.getVersion() + "後面的狀態");
    }

基於javaAPI的Curator的刪除結點:

    @Test//刪除沒有子節點的結點
    public void testDeleteNode() throws Exception {

        client.delete().forPath("/app1");

    }

    @Test//刪除帶有子節點的結點
    public void testDeleteNodeWithChilren() throws Exception {
        client.delete().deletingChildrenIfNeeded().forPath("/app9");


    }

    @Test//測試必須刪除成功,防止出現網路抖動不能夠正常刪除
    public void testMustDelete() throws Exception {

        client.delete().guaranteed().forPath("/app2");


    }

@Test//測試回撥刪除,別忘了學習一下lamada表示式
    public void testDeletedCallback() throws Exception {

        client.delete().guaranteed().inBackground(new BackgroundCallback() {
            @Override
            public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
                System.out.println("我被刪除了");
                System.out.println(curatorEvent.getType());
            }
        }).forPath("/app3");




}

操作完畢之後關閉連線:

 @After
    public void closeClient() {


        if (client != null) {
            client.close();

        }
    }