java - 有zookeeper相關的大神麼?使用ZKClient建立節點,但是伺服器中並沒有顯示?
仅有的幸福
仅有的幸福 2017-05-17 09:58:50
0
1
740

我按照zk權威指南上的程式碼建立節點:

package com.zkstudy;

import java.io.IOException;
import java.util.Random;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class Master implements Watcher {
    private ZooKeeper zk = null;
    private String host;
    private String serverId = null;
    private boolean isLeader = false;

    public Master(String host) {
        this.host = host;
        Random radom = new Random();
        serverId = Integer.toHexString(radom.nextInt());
    }

    public void start() {

        try {
            this.zk = new ZooKeeper(host, 15000, this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createMaster() {

        while (true) {

            try {
                zk.create("/master", serverId.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
                isLeader = true;
                break;
            } catch (KeeperException e) {
                e.printStackTrace();
                isLeader = false;
                break;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (checkMaster()) {
                break;
            }

        }

    }

    public boolean checkMaster() {

        while (true) {
            Stat stat = new Stat();
            try {
                byte[] data = zk.getData("/master", false, stat);
                isLeader = new String(data).equals(serverId);
                return true;
            } catch (KeeperException e) {
                e.printStackTrace();
                return false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void process(WatchedEvent event) {
        System.out.println("watcher thread:---------->" + Thread.currentThread().getId());
        System.out.println("event:---------->" + event);
    }

    public static void main(String[] args) throws InterruptedException {
        Master master = new Master("123.206.28.37:2181");
        master.start();
        master.createMaster();
        if (master.isLeader) {
            System.out.println("i am  master");
        }
        System.out.println("main thread:---------->" + Thread.currentThread().getId());
        Thread.sleep(5000);
    }
}

然後我跑了兩遍。出現瞭如下問題:

watcher thread:---------->10
event:---------->WatchedEvent state:SyncConnected type:None path:null
org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists for /master
main thread:---------->1
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:119)
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
    at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:783)
    at com.zkstudy.Master.createMaster(Master.java:40)
    at com.zkstudy.Master.main(Master.java:83)

提示master節點已經存在。
但是我在命令列裡面查看卻沒有發現master節點:

[zk: localhost:2181(CONNECTED) 10] ls
[zk: localhost:2181(CONNECTED) 11] ls  /
[zk, zookeeper]
[zk: localhost:2181(CONNECTED) 12] 

請問這是為什麼呢? zk使用的是單節點,不是偽叢集。

仅有的幸福
仅有的幸福

全部回覆(1)
Peter_Zhu

自問自答吧。因為我創建的是臨時節點,臨時節點在繪畫斷開以後會自動刪除,估計這個 ls / 命令應該是列舉的持久節點。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板