1. 程式人生 > >zookeeper 偽分布式安裝

zookeeper 偽分布式安裝

jre gradle apache cde .sh org 不同的 exceptio zookeep

1 下載zookeeper安裝包

下載地址 http://apache.fayea.com/zookeeper/

我下載的是zookeeper-3.4.6.tar.gz

2 解壓縮

將zookeeper-3.4.6.tar.gz復制到/usr/local文件夾下

運行命令 tar -zxvf zookeeper-3.4.6.tar.gz

解壓後的文件夾為 zookeeper-3.4.6 ,為了方便,將文件夾改為zookeeper。mv zookeeper-3.4.6 zookeeper

3 配置

為模擬3個分布式節點,所以須要創建三個節點的配置文件 zoo1.cfg、zoo2.cfg、zoo3.cfg

cd /usr/local/zookeeper/conf

將zoo_sample.cfg改名為zoo1.cfg

mv zoo_sample.cfg zoo1.cfg

cp zoo1.cfg zoo2.cfg

cp zoo1.cfg zoo3.cfg

技術分享圖片

zoo1.cfg配置例如以下 (註意以下標紅的地方)

技術分享圖片

註:上面的hadoop3是我的機器名,能夠寫成127.0.0.1

zoo2.cfg配置例如以下

技術分享圖片

zoo3.cfg配置例如以下

技術分享圖片

創建Zookeeper 保存數據的文件夾

mkdir -p /usr/local/zookeeper/data/zk1

mkdir -p /usr/local/zookeeper/data/zk2

mkdir -p /usr/local/zookeeper/data/zk3

4 配置說明

  • tickTime:這個時間是作為 Zookeeper server之間或client與server之間維持心跳的時間間隔,也就是每一個 tickTime 時間就會發送一個心跳。
  • initLimit:這個配置項是用來配置 Zookeeper 接受client(這裏所說的client不是用戶連接 Zookeeper server的client,而是 Zookeeper server集群中連接到 Leader 的 Follower server)初始化連接時最長能忍受多少個心跳時間間隔數。

    當已經超過 10個心跳的時間(也就是 tickTime)長度後 Zookeeper server還沒有收到client的返回信息,那麽表明這個client連接失敗。總的時間長度就是 10*2000=20 秒

  • syncLimit:這個配置項標識 Leader 與 Follower 之間發送消息。請求和應答時間長度,最長不能超過多少個 tickTime 的時間長度。總的時間長度就是 5*2000=10秒
  • dataDir:顧名思義就是 Zookeeper 保存數據的文件夾,默認情況下,Zookeeper 將寫數據的日誌文件也保存在這個文件夾裏。
  • clientPort:這個port就是client連接 Zookeeper server的port。Zookeeper 會監聽這個port,接受client的訪問請求。
  • server.A=B:C:D:當中 A 是一個數字。表示這個是第幾號server;B 是這個server的 ip 地址;C 表示的是這個server與集群中的 Leader server交換信息的port;D 表示的是萬一集群中的 Leader server掛了。須要一個port來又一次進行選舉。選出一個新的 Leader。而這個port就是用來運行選舉時server相互通信的port。假設是偽集群的配置方式,因為 B 都是一樣,所以不同的 Zookeeper 實例通信port號不能一樣,所以要給它們分配不同的port號。
5 創建節點標識 在/usr/local/zookeeper/data/zk1 、 /usr/local/zookeeper/data/zk3 、/usr/local/zookeeper/data/zk3 三個文件夾下分別創建myid文件 裏面分別寫1、2、3用於標識第幾號server vi /usr/local/zookeeper/data/zk1 1 vi /usr/local/zookeeper/data/zk1 2 vi /usr/local/zookeeper/data/zk1 3 6 啟動zookeeper cd /usr/local/zookeeper/ 技術分享圖片 7 查看啟動狀態 技術分享圖片 能夠看到管理節點在3號server上 8 假設想停止server可使用下面命令 ./bin/zkServer.sh stop conf/zoo1.cfg

./bin/zkServer.sh stop conf/zoo2.cfg

./bin/zkServer.sh stop conf/zoo3.cfg

9 驗證zookeeper安裝的正確性

./bin/zkCli.sh -server 127.0.0.1:2183

註:因為上面的leader在3號server上,3號server的client連接port是2183

運行成功後:

WATCHER::
WatchedEvent state:SyncConnected type:None path:null

[zk: 127.0.0.1:2183(CONNECTED) 0]

查看幫助:

技術分享圖片

測試

技術分享圖片

至此,Zookeeper 安裝完畢。

zookeeper命令行操作

我們通過客戶端連接ZooKeeper的集群,我們可以任意的zookeeper是進行連接。


./bin/zkCli.sh -server 127.0.0.1:2183

2013-08-12 05:25:39,284 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.1.201 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@22ba6c83
Welcome to ZooKeeper!
JLine support is enabled
[zk: 192.168.1.201(CONNECTING) 0] 2013-08-12 05:25:39,336 [myid:] - INFO  [main-SendThread(192.168.1.201:2181):ClientCnxn$SendThread@966] - Opening socket connection to server 192.168.1.201/192.168.1.201:2181. Will not attempt to authenticate using SASL (Unable to locate a login configuration)
2013-08-12 05:25:39,345 [myid:] - INFO  [main-SendThread(192.168.1.201:2181):ClientCnxn$SendThread@849] - Socket connection established to 192.168.1.201/192.168.1.201:2181, initiating session
2013-08-12 05:25:39,384 [myid:] - INFO  [main-SendThread(192.168.1.201:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server 192.168.1.201/192.168.1.201:2181, sessionid = 0x1406f3c1ef90001, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: 192.168.1.201(CONNECTED) 0]

集群已連接,下面我們要使用一下,ZooKeeper的命令行操作。

命令行操作
通過help打印命令行幫助


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

ZooKeeper的結構,很像是目錄結構,我們看到了像ls這樣熟悉的命令。


#ls,查看/目錄內容
[zk: 192.168.1.201(CONNECTED) 1] ls /
[zookeeper]

#create,創建一個znode節點
[zk: 192.168.1.201(CONNECTED) 2] create /node conan
Created /node

#ls,再查看/目錄
[zk: 192.168.1.201(CONNECTED) 3] ls /
[node, zookeeper]

#get,查看/node的數據信息
[zk: 192.168.1.201(CONNECTED) 4] get /node
conan
cZxid = 0x100000006
ctime = Mon Aug 12 05:32:49 CST 2013
mZxid = 0x100000006
mtime = Mon Aug 12 05:32:49 CST 2013
pZxid = 0x100000006
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

#set,修改數據
[zk: 192.168.1.201(CONNECTED) 5] set /node fens.me
cZxid = 0x100000006
ctime = Mon Aug 12 05:32:49 CST 2013
mZxid = 0x100000007
mtime = Mon Aug 12 05:34:32 CST 2013
pZxid = 0x100000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0

#get,再查看/node的數據信息,已改為fens.me
[zk: 192.168.1.201(CONNECTED) 6] get /node
fens.me
cZxid = 0x100000006
ctime = Mon Aug 12 05:32:49 CST 2013
mZxid = 0x100000007
mtime = Mon Aug 12 05:34:32 CST 2013
pZxid = 0x100000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0

#delete,刪除/node
[zk: 192.168.1.201(CONNECTED) 7] delete /node
[zk: 192.168.1.201(CONNECTED) 8] ls /
[zookeeper]

#quit,退出客戶端連接
[zk: 192.168.1.201(CONNECTED) 19] quit
Quitting...
2013-08-12 05:40:29,304 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x1406f3c1ef90002 closed
2013-08-12 05:40:29,305 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@509] - EventThread shut down

5. Java編程現實命令行操作


package org.conan.zookeeper.demo;

import java.io.IOException;

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;

public class BasicDemo1 {

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        // 創建一個與服務器的連接
        ZooKeeper zk = new ZooKeeper("192.168.1.201:2181", 60000, new Watcher() {
            // 監控所有被觸發的事件
            public void process(WatchedEvent event) {
                System.out.println("EVENT:" + event.getType());
            }
        });

        // 查看根節點
        System.out.println("ls / => " + zk.getChildren("/", true));

        // 創建一個目錄節點
        if (zk.exists("/node", true) == null) {
            zk.create("/node", "conan".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("create /node conan");
            // 查看/node節點數據
            System.out.println("get /node => " + new String(zk.getData("/node", false, null)));
            // 查看根節點
            System.out.println("ls / => " + zk.getChildren("/", true));
        }

        // 創建一個子目錄節點
        if (zk.exists("/node/sub1", true) == null) {
            zk.create("/node/sub1", "sub1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("create /node/sub1 sub1");
            // 查看node節點
            System.out.println("ls /node => " + zk.getChildren("/node", true));
        }

        // 修改節點數據
        if (zk.exists("/node", true) != null) {
            zk.setData("/node", "changed".getBytes(), -1);
            // 查看/node節點數據
            System.out.println("get /node => " + new String(zk.getData("/node", false, null)));
        }

        // 刪除節點
        if (zk.exists("/node/sub1", true) != null) {
            zk.delete("/node/sub1", -1);
            zk.delete("/node", -1);
            // 查看根節點
            System.out.println("ls / => " + zk.getChildren("/", true));
        }

        // 關閉連接
        zk.close();
    }
}

運行結果:

2013-08-12 15:33:29,699 [myid:] - INFO  [main:Environment@97] - Client environment:zookeeper.version=3.3.1-942149, built on 05/07/2010 17:14 GMT
2013-08-12 15:33:29,702 [myid:] - INFO  [main:Environment@97] - Client environment:host.name=PC201304202140
2013-08-12 15:33:29,702 [myid:] - INFO  [main:Environment@97] - Client environment:java.version=1.6.0_45
2013-08-12 15:33:29,702 [myid:] - INFO  [main:Environment@97] - Client environment:java.vendor=Sun Microsystems Inc.
2013-08-12 15:33:29,702 [myid:] - INFO  [main:Environment@97] - Client environment:java.home=D:\toolkit\java\jdk6\jre
2013-08-12 15:33:29,703 [myid:] - INFO  [main:Environment@97] - Client environment:java.class.path=D:\workspace\java\zkdemo\target\classes;C:\Users\Administrator\.m2\repository\org\apache\hadoop\zookeeper\3.3.1\zookeeper-3.3.1.jar;C:\Users\Administrator\.m2\repository\log4j\log4j\1.2.15\log4j-1.2.15.jar;C:\Users\Administrator\.m2\repository\javax\mail\mail\1.4\mail-1.4.jar;C:\Users\Administrator\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Users\Administrator\.m2\repository\jline\jline\0.9.94\jline-0.9.94.jar;C:\Users\Administrator\.m2\repository\junit\junit\3.8.1\junit-3.8.1.jar
2013-08-12 15:33:29,703 [myid:] - INFO  [main:Environment@97] - Client environment:java.library.path=D:\toolkit\java\jdk6\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\toolkit\Rtools\bin;D:\toolkit\Rtools\gcc-4.6.3\bin;C:\Program Files (x86)\Common Files\NetSarang;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\toolkit\Git\cmd;D:\toolkit\Git\bin;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files (x86)\Common Files\Ulead Systems\MPEG;C:\Program Files (x86)\QuickTime\QTSystem\;D:\toolkit\MiKTex\miktex\bin\x64\;D:\toolkit\sshclient;D:\toolkit\ant19\bin;D:\toolkit\eclipse;D:\toolkit\gradle15\bin;D:\toolkit\java\jdk6\bin;D:\toolkit\maven3\bin;D:\toolkit\mysql56\bin;D:\toolkit\python27;D:\toolkit\putty;C:\Program Files\R\R-3.0.1\bin\x64;D:\toolkit\mongodb243\bin;D:\toolkit\php54;D:\toolkit\nginx140;D:\toolkit\nodejs;D:\toolkit\npm12\bin;D:\toolkit\java\jdk6\jre\bin\server;.
2013-08-12 15:33:29,703 [myid:] - INFO  [main:Environment@97] - Client environment:java.io.tmpdir=C:\Users\ADMINI~1\AppData\Local\Temp2013-08-12 15:33:29,704 [myid:] - INFO  [main:Environment@97] - Client environment:java.compiler=
2013-08-12 15:33:29,704 [myid:] - INFO  [main:Environment@97] - Client environment:os.name=Windows 7
2013-08-12 15:33:29,704 [myid:] - INFO  [main:Environment@97] - Client environment:os.arch=amd64
2013-08-12 15:33:29,705 [myid:] - INFO  [main:Environment@97] - Client environment:os.version=6.1
2013-08-12 15:33:29,705 [myid:] - INFO  [main:Environment@97] - Client environment:user.name=Administrator
2013-08-12 15:33:29,705 [myid:] - INFO  [main:Environment@97] - Client environment:user.home=C:\Users\Administrator
2013-08-12 15:33:29,706 [myid:] - INFO  [main:Environment@97] - Client environment:user.dir=D:\workspace\java\zkdemo
2013-08-12 15:33:29,707 [myid:] - INFO  [main:ZooKeeper@373] - Initiating client connection, connectString=192.168.1.201:2181 sessionTimeout=60000 watcher=org.conan.zookeeper.demo.BasicDemo1$1@3dfeca64
2013-08-12 15:33:29,731 [myid:] - INFO  [main-SendThread():ClientCnxn$SendThread@1000] - Opening socket connection to server /192.168.1.201:2181
2013-08-12 15:33:38,736 [myid:] - INFO  [main-SendThread(192.168.1.201:2181):ClientCnxn$SendThread@908] - Socket connection established to 192.168.1.201/192.168.1.201:2181, initiating session
2013-08-12 15:33:38,804 [myid:] - INFO  [main-SendThread(192.168.1.201:2181):ClientCnxn$SendThread@701] - Session establishment complete on server 192.168.1.201/192.168.1.201:2181, sessionid = 0x1406f3c1ef9000d, negotiated timeout = 60000
EVENT:None
ls / => [zookeeper]
EVENT:NodeCreated
EVENT:NodeChildrenChanged
create /node conan
get /node => conan
ls / => [node, zookeeper]
EVENT:NodeCreated
create /node/sub1 sub1
ls /node => [sub1]
EVENT:NodeDataChanged
get /node => changed
EVENT:NodeDeleted
EVENT:NodeChildrenChanged
EVENT:NodeChildrenChanged
ls / => [zookeeper]
2013-08-12 15:33:38,877 [myid:] - INFO  [main:ZooKeeper@538] - Session: 0x1406f3c1ef9000d closed

zookeeper 偽分布式安裝