1. 程式人生 > >zookeeper 基本命令和java api

zookeeper 基本命令和java api

1.Zookeeper命令工具

啟動zookeeper服務後,連線到zookeeper服務:

    zkServer -server localhost:2181

執行結果如下:

Connecting to localhost:2181
2018-06-10 03:52:52,540 [myid:] - INFO  [main:[email protected]] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2018-06-10 03:52:52,562 [myid:] - INFO  [main:
[email protected]
] - Client environment:host.name=192.168.15.201 2018-06-10 03:52:52,563 [myid:] - INFO [main:[email protected]] - Client environment:java.version=1.8.0_171 2018-06-10 03:52:52,578 [myid:] - INFO [main:[email protected]] - Client environment:java.vendor=Oracle Corporation 2018-06-10 03:52:52,579 [myid:] - INFO [main:
[email protected]
] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el6_9.x86_64/jre 2018-06-10 03:52:52,579 [myid:] - INFO [main:[email protected]] - Client environment:java.class.path=/usr/local/zookeeper/zookeeper-3.4.10_1/bin/../build/classes:/usr/local/zookeeper/zookeeper-3.4.10_1/bin/../build/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.10_1/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/zookeeper-3.4.10_1/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/zookeeper-3.4.10_1/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper/zookeeper-3.4.10_1/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/zookeeper-3.4.10_1/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/zookeeper-3.4.10_1/bin/../zookeeper-3.4.10.jar:/usr/local/zookeeper/zookeeper-3.4.10_1/bin/../src/java/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.10_1/bin/../conf:.:/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.171-8.b10.el6_9.x86_64/lib/dt.jar:/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.171-8.b10.el6_9.x86_64/lib/tools.jar 2018-06-10 03:52:52,579 [myid:] - INFO [main:
[email protected]
] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2018-06-10 03:52:52,580 [myid:] - INFO [main:[email protected]] - Client environment:java.io.tmpdir=/tmp 2018-06-10 03:52:52,580 [myid:] - INFO [main:[email protected]] - Client environment:java.compiler=<NA> 2018-06-10 03:52:52,581 [myid:] - INFO [main:[email protected]] - Client environment:os.name=Linux 2018-06-10 03:52:52,581 [myid:] - INFO [main:[email protected]] - Client environment:os.arch=amd64 2018-06-10 03:52:52,582 [myid:] - INFO [main:[email protected]] - Client environment:os.version=2.6.32-431.el6.x86_64 2018-06-10 03:52:52,582 [myid:] - INFO [main:[email protected]] - Client environment:user.name=root 2018-06-10 03:52:52,582 [myid:] - INFO [main:[email protected]] - Client environment:user.home=/root 2018-06-10 03:52:52,583 [myid:] - INFO [main:[email protected]] - Client environment:user.dir=/usr/local/zookeeper/zookeeper-3.4.10/bin 2018-06-10 03:52:52,589 [myid:] - INFO [main:[email protected]] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 [email protected] Welcome to ZooKeeper! 2018-06-10 03:52:52,716 [myid:] - INFO [main-SendThread(localhost:2181):[email protected]] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error) JLine support is enabled 2018-06-10 03:52:53,237 [myid:] - INFO [main-SendThread(localhost:2181):[email protected]] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session 2018-06-10 03:52:53,507 [myid:] - INFO [main-SendThread(localhost:2181):[email protected]] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x163e94ed0530000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null

控制檯會輸出一些配置資訊和“welcome to Zookeeper”,檢視zookeeper的相關操作,可以輸入 help 命令

[zk: localhost:2181(CONNECTED) 4] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history 
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit 
getAcl path
close 
connect host:port

演示下常見的zookeeper基本操作

1.建立一個節點

節點的名字是zoo,節點的相關資訊是myData

[zk: localhost:2181(CONNECTED) 4] create /zoo myData
Created /zoo

再檢視下現在zookeeper包含的節點

[zk: localhost:2181(CONNECTED) 6] ls /
[zoo, zookeeper]

這時,可以看到zoo節點已經被建立

2.獲取指定節點的資訊

[zk: localhost:2181(CONNECTED) 7] get /zoo
myData
cZxid = 0x100000006
ctime = Sun Jun 10 10:35:55 PDT 2018
mZxid = 0x100000006
mtime = Sun Jun 10 10:35:55 PDT 2018
pZxid = 0x100000006
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

3.修改指定節點的資訊

[zk: localhost:2181(CONNECTED) 8] set /zoo animais
cZxid = 0x100000006
ctime = Sun Jun 10 10:35:55 PDT 2018
mZxid = 0x100000007
mtime = Sun Jun 10 10:39:06 PDT 2018
pZxid = 0x100000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0

再檢視修改後內容

[zk: localhost:2181(CONNECTED) 9] get /zoo        
animais
cZxid = 0x100000006
ctime = Sun Jun 10 10:35:55 PDT 2018
mZxid = 0x100000007
mtime = Sun Jun 10 10:39:06 PDT 2018
pZxid = 0x100000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0

4.刪除節點

[zk: localhost:2181(CONNECTED) 12] delete /zoo
[zk: localhost:2181(CONNECTED) 13] ls /
[zookeeper]

結驗證,zoo節點已被刪除

2.java API的使用

首先需要在maven新增zookeeper的jar包

    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.10</version>
    </dependency>

具體程式碼如下:

public class ZookeeperDemo {
    /** 服務地址,ip:port,多個地址用逗號隔開 */
    private static final String SERVER_ADDRESS = "192.168.2.201:218,192.168.2.201:2182,192.168.2.201:2183";
    /** 超時時間,毫秒為單位 */
    private static final Integer TIMEOUT_MS = 3000;
    /** 節點名 */
    private static final String NODE_ZOO = "/zoo/mammal";

    public static void main(String[] args) throws Exception {
        ZooKeeper zk = new ZooKeeper(SERVER_ADDRESS, TIMEOUT_MS, new DemoWatcher());
        waitUntilConnected(zk);
        // 判斷節點是否存在
        Stat stat = zk.exists(NODE_ZOO, false);

        if (null == stat) {// 節點不存在
            createNode(zk, NODE_ZOO, "i'm mammal", ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } else {
            getData(zk, NODE_ZOO, stat);
            System.out.println(stat.getVersion());

            updateNode(zk, NODE_ZOO, "i'm mammal haha", stat.getVersion());
        }
    }

    /**
     * 根據指定節點名字,建立節點
     * @param zk               zk客戶端
     * @param node_zoo         節點名
     * @param data             節點資訊
     * @param ACL_LIST         訪問控制列表,這裡使用了完全開放的ACL,允許任何客戶端對znode進行讀寫
     * @param createMode       建立znode的型別:有兩種型別的znode:短暫的和持久的。
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void createNode(ZooKeeper zk, String node_zoo, String data,List<ACL> ACL_LIST,CreateMode createMode)
            throws KeeperException, InterruptedException {
        String createResult = zk.create(node_zoo, data.getBytes(), ACL_LIST ,createMode);
        System.out.println(createResult);
    }

    /**
     * 刪除節點
     * @param zk               zk客戶端
     * @param node_zoo         節點名
     * @param version          指定的版本,-1/無視版本
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void deleteNode(ZooKeeper zk, String node_zoo, Integer version)
            throws KeeperException, InterruptedException {
        zk.delete(node_zoo, version);
    }

    /**
     * 更新節點
     * @param zk               zk客戶端
     * @param node_zoo         節點名
     * @param data             節點資料
     * @param version          指定的版本,-1/無視版本
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void updateNode(ZooKeeper zk, String node_zoo, String data, Integer version)
            throws KeeperException, InterruptedException {
        zk.setData(node_zoo, data.getBytes(), version);
    }

    /**
     * 根據節點名獲取節點資訊
     * @param zk               zk客戶端
     * @param node_zoo         節點名
     * @param stat             節點狀態
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void getData(ZooKeeper zk, String node_zoo, Stat stat) throws KeeperException, InterruptedException {
        byte[] bs = zk.getData(node_zoo, false, stat);
        System.out.println(new String(bs));
    }

    /**
     * 監聽類,接收來自zookeeper的回撥
     * @author Administrator
     *
     */
    static class DemoWatcher implements Watcher {
        public void process(WatchedEvent event) {
            System.out.println("----------->");
            System.out.println("path:" + event.getPath());
            System.out.println("type:" + event.getType());
            System.out.println("stat:" + event.getState());
            System.out.println("<-----------");
        }
    }

    /**
     * 防止java程式未連上zookeeper的伺服器而往下執行,需等待zk的狀態碼
     * @param zooKeeper
     */
    public static void waitUntilConnected(ZooKeeper zooKeeper) {
        CountDownLatch connectedLatch = new CountDownLatch(1);
        Watcher watcher = new ConnectedWatcher(connectedLatch);
        zooKeeper.register(watcher);
        if (States.CONNECTING == zooKeeper.getState()) {
            try {
                connectedLatch.await();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        }
    }

    static class ConnectedWatcher implements Watcher {
        private CountDownLatch connectedLatch;

        ConnectedWatcher(CountDownLatch connectedLatch) {
            this.connectedLatch = connectedLatch;
        }

        public void process(WatchedEvent event) {
            if (event.getState() == KeeperState.SyncConnected) {
                connectedLatch.countDown();
            }
        }
    }
}