zookeeper.jar使用(一)

package com.test.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class BaseZookeeper implements Watcher {

    public ZooKeeper zooKeeper;

    private static final int SESSION_TIME_OUT = 6000;

    private CountDownLatch countDownLatch = new CountDownLatch(1);

    /**
     * 鏈接zookeeper
     *
     * @param host
     * @throws IOException
     * @throws InterruptedException
     */
    public void connectZookeeper(String host) throws IOException, InterruptedException {
        zooKeeper = new ZooKeeper(host, SESSION_TIME_OUT, this);
        countDownLatch.await();
        System.out.println("zookeeper connect ok");

    }

    /**
     * 實現watcher的接口方法,當鏈接zookeeper成功後,zookeeper會經過此方法通知watcher
     * 此處爲若是接受到鏈接成功的event,則countDown,讓當前線程繼續其餘事情。
     */
    @Override
    public void process(WatchedEvent event) {
        if (event.getState() == Event.KeeperState.SyncConnected) {
            System.out.println("watcher received event");
            countDownLatch.countDown();
        }
    }

    /**
     * 根據路徑建立節點,而且設置節點數據
     *
     * @param path
     * @param data
     * @return
     * @throws KeeperException
     * @throws InterruptedException
     */
    public String createNode(String path, byte[] data) throws KeeperException, InterruptedException {
        return this.zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    /**
     * 根據路徑獲取全部孩子節點
     *
     * @param path
     * @return
     * @throws KeeperException
     * @throws InterruptedException
     */
    public List<String> getChildren(String path) throws KeeperException, InterruptedException {
        return this.zooKeeper.getChildren(path, false);
    }

    public Stat setData(String path, byte[] data, int version) throws KeeperException, InterruptedException {
        return this.zooKeeper.setData(path, data, version);
    }

    /**
     * 根據路徑獲取節點數據
     *
     * @param path
     * @return
     * @throws KeeperException
     * @throws InterruptedException
     */
    public byte[] getData(String path) throws KeeperException, InterruptedException {
        return this.zooKeeper.getData(path, false, null);
    }

    /**
     * 刪除節點
     *
     * @param path
     * @param version
     * @throws InterruptedException
     * @throws KeeperException
     */
    public void deletNode(String path, int version) throws InterruptedException, KeeperException {
        this.zooKeeper.delete(path, version);
    }

    /**
     * 關閉zookeeper鏈接
     *
     * @throws InterruptedException
     */
    public void closeConnect() throws InterruptedException {
        if (null != zooKeeper) {
            zooKeeper.close();
        }
    }

}
package com.test.zk;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class ZookeeperTest {
    private static Logger logger = LoggerFactory.getLogger(ZookeeperTest.class);
    public static void main(String[] args) {
        logger.info("main begin....");
        try {
            BaseZookeeper baseZookeeper = new BaseZookeeper();

            String host = "127.0.0.1:8080";//本地zookeeper端口號改成8080了

            // 鏈接zookeeper
            baseZookeeper.connectZookeeper(host);
            System.out.println("--------connect zookeeper ok-----------");

            // 建立節點
            /*byte [] data = {1, 2, 3, 4, 5};
            String result = baseZookeeper.createNode("/test", data);
            System.out.println(result);
            System.out.println("--------create node ok-----------");*/


            // 獲取某路徑下全部節點
            List<String> children = baseZookeeper.getChildren("/");
            for (String child : children)
            {
                logger.info(child);
            }
            logger.info("--------get children ok-----------");

            // 獲取節點數據
           /* byte [] nodeData = baseZookeeper.getData("/test");
            logger.info(new String(nodeData,"UTF-8"));
            logger.info("--------get node data ok-----------");*/

            // 更新節點數據
            byte[] data = "測試".getBytes("UTF-8");
            baseZookeeper.setData("/test", data, 2);//版本號爲當前的版本
            System.out.println("--------set node data ok-----------");

            byte[] nodeData = baseZookeeper.getData("/test");
            System.out.println(new String(nodeData,"UTF-8"));
            System.out.println("--------get node new data ok-----------");

            baseZookeeper.closeConnect();
            System.out.println("--------close zookeeper ok-----------");

        } catch (Exception e) {
            logger.error("...zookeeper操做出錯....",e);
        }
    }

}

pom.xmlhtml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>test-demo</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>test-demo Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <junit.version>3.8.1</junit.version>
        <zkclient.version>2.1.1</zkclient.version>
        <zookeeper.version>3.4.6</zookeeper.version>
        <slf4j-log4j12.version>1.7.12</slf4j-log4j12.version>
    </properties>
    <dependencies>
        <!--
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        -->
        <!--
        <dependency>
            <groupId>com.github.adyliu</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>compile</scope>
        </dependency>
        -->

        <!--zookeeper java客戶端-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>jline</groupId>
                    <artifactId>jline</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--日誌相關-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j-log4j12.version}</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>test-demo</finalName>
    </build>
</project>

1、ZooKeeper 節點java

是有生命週期的,這取決於節點的類型。在 ZooKeeper 中,節點類型能夠分爲持久節點(PERSISTENT )、臨時節點(EPHEMERAL),以及時序節點(SEQUENTIAL ),具體在節點建立過程當中,通常是組合使用,能夠生成如下 4 種節點類型。node

持久節點(PERSISTENT)git

所謂持久節點,是指在節點建立後,就一直存在,直到有刪除操做來主動清除這個節點——不會由於建立該節點的客戶端會話失效而消失。github

持久順序節點(PERSISTENT_SEQUENTIAL)apache

這類節點的基本特性和上面的節點類型是一致的。額外的特性是,在ZK中,每一個父節點會爲他的第一級子節點維護一份時序,會記錄每一個子節點建立的前後順序。 基於這個特性,在建立子節點的時候,能夠設置這個屬性,那麼在建立節點過程當中,ZK會自動爲給定節點名加上一個數字後綴,做爲新的節點名。這個數字後綴的 範圍是整型的最大值。api

臨時節點(EPHEMERAL)app

和持久節點不一樣的是,臨時節點的生命週期和客戶端會話綁定。也就是說,若是客戶端會話失效,那麼這個節點就會自動被清除掉。注意,這裏提到的是會話失效,而非鏈接斷開。另外,在臨時節點下面不能建立子節點。maven

臨時順序節點(EPHEMERAL_SEQUENTIAL)分佈式

能夠用來實現分佈式鎖

客戶端調用create()方法建立名爲「_locknode_/guid-lock-」的節點,須要注意的是,這裏節點的建立類型須要設置爲EPHEMERAL_SEQUENTIAL。客戶端調用getChildren(「_locknode_」)方法來獲取全部已經建立的子節點,注意,這裏不註冊任何Watcher。客戶端獲取到全部子節點path以後,若是發現本身在步驟1中建立的節點序號最小,那麼就認爲這個客戶端得到了鎖。若是在步驟3中發現本身並不是全部子節點中最小的,說明本身尚未獲取到鎖。此時客戶端須要找到比本身小的那個節點,而後對其調用exist()方法,同時註冊事件監聽。以後當這個被關注的節點被移除了,客戶端會收到相應的通知。這個時候客戶端須要再次調用getChildren(「_locknode_」)方法來獲取全部已經建立的子節點,確保本身確實是最小的節點了,而後進入步驟3。

相關文章
相關標籤/搜索