摘要:簡單介紹是開發和維護開源服務器的服務,它能夠實現高度可靠的分布式協調。同步地獲取與節點關聯的。此值可能在服務器重新連接后發生更改。
Zookeeper 簡單介紹
Apache Zookeeper是開發和維護開源服務器的服務,它能夠實現高度可靠的分布式協調。安裝Zookeeper(無需安裝)
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz tar zxvf zookeeper-3.4.10.tar.gz安裝Zookeeper C擴展支持
cd zookeeper-3.4.8/src/c ./configure --prefix=/usr/ make make install安裝php的zookeeper擴展
wget https://pecl.php.net/get/zookeeper-0.6.2.tgz tar zxvf zookeeper-0.6.2.tgz cd zookeeper-0.6.2 phpize ./configure --with-libzookeeper-dir=/usr/ $ make $ make install啟動zookeeper server
# {zookeeperserver}代表你zookeeper解壓的目錄 mkdir -p /project/zookeeper/demo/data #zoo.conf 是默認啟動所加載的配置文件 cp /{zookeeperserver}/conf/zoo_sample.cfg /{zookeeperserver}/conf/zoo.cfg /{zookeeperserver}/bin/zkServer startzookeeper client 測試
/{zookeeperserver}/bin/zkCli.sh -server 127.0.0.1:2181 # [zk: 127.0.0.1:2181(CONNECTED) x] 客戶端連接信息shell [zk: 127.0.0.1:2181(CONNECTED) 3] ls / [zookeeper] [zk: 127.0.0.1:2181(CONNECTED) 4] create /test qkl001 Created /test [zk: 127.0.0.1:2181(CONNECTED) 5] create /zgq zgq002 Created /zgq [zk: 127.0.0.1:2181(CONNECTED) 6] ls / [zookeeper, test, zgq] [zk: 127.0.0.1:2181(CONNECTED) 5] delete /zgq Created /zgq [zk: 127.0.0.1:2181(CONNECTED) 7] quitPHP下實踐 zookeeper類信息
Zookeeper — Zookeeper類 -- Zookeeper::addAuth — 指定應用程序憑據 -- Zookeeper::connect — 創建與Zookeeper溝通的句柄 -- Zookeeper::__construct — 創建與Zookeeper溝通的句柄 -- Zookeeper::create — 同步創建節點 -- Zookeeper::delete — 同步刪除Zookeeper中的一個節點 -- Zookeeper::exists — 同步檢查Zookeeper節點的存在性 -- Zookeeper::get — 同步獲取與節點關聯的數據。 -- Zookeeper::getAcl — 同步地獲取與節點關聯的ACL。 -- Zookeeper::getChildren — 同步列出節點的子節點 -- Zookeeper::getClientId — 返回客戶端會話ID,僅在連接當前連接時才有效(即最后觀察者狀態為ZooOnCeleDelphi狀態) -- Zookeeper::getRecvTimeout — 返回此會話的超時,如果連接當前連接(只有上次觀察者狀態為ZooOnCeleTytStand狀態)才有效。此值可能在服務器重新連接后發生更改。 -- Zookeeper::getState — 獲取Zookeeper連接的狀態 -- Zookeeper::isRecoverable — 檢查當前的Zookeeper連接狀態是否可以恢復 -- Zookeeper::set — 設置與節點關聯的數據 -- Zookeeper::setAcl — 同步設置與節點關聯的ACL -- Zookeeper::setDebugLevel — 設置庫的調試級別 -- Zookeeper::setDeterministicConnOrder — 啟用/禁用仲裁端點順序隨機化 -- Zookeeper::setLogStream — 設置庫用于日志記錄的流 -- Zookeeper::setWatcher — 設置觀察函數客戶端操作
zkCli.cmd # output 表示連接成功 WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0] # 以下是操作命令 ls / create /ztest 1獲取節點信息(Zookeeper::get)
# get.php $zoo = new Zookeeper("192.168.1.45:2181"); $r = $zoo->get( "/ztest"); var_dump($r);獲取節點信息(Zookeeper::get)并設置watcher
# get.php class ZookeeperDemo extends Zookeeper { public function watcher($type, $state, $key) { var_dump($type); var_dump($state); var_dump($key); if ($type == 3) { var_dump($this->get("/zgetw")); // Watcher gets consumed so we need to set a new one $this->get("/zgetw", array($this, "watcher")); } } } $zoo = new ZookeeperDemo("192.168.1.45:2181"); $zoo->get("/zgetw", [$zoo, "watcher"]); while (true) { echo "."; sleep(1); }客戶端操作
set /ztest 2 set /ztest 3 set /ztest 5獲取節點信息(Zookeeper::get) 結果
php get.php #output ......int(3) int(3) string(6) "/ztest" string(1) "2" ..int(3) int(3) string(6) "/ztest" string(1) "3" ..int(3) int(3) string(6) "/ztest" string(1) "4" .int(3) int(3) string(6) "/ztest" string(1) "5"創建子節點
create /ztest/child1 c1 # output: Created /ztest/child1 create /ztest/child2 c2 # output: Created /ztest/child2 ls /ztest # output: [child2, child1]php下獲取子節點
# getChild.php $zookeeper = new Zookeeper("192.168.1.45:2181"); $path = "/ztest"; $r = $zookeeper->getchildren($path); if (!empty($r)) { foreach ($r as $c) { var_dump($c); } } # php getChild.php string(6) "child2" string(6) "child1"創建順序節點
[zk: localhost:2181(CONNECTED) 22] create /stest 1 Created /stest [zk: localhost:2181(CONNECTED) 23] create -s /stest/seq 1 Created /stest/seq0000000000 [zk: localhost:2181(CONNECTED) 24] create -s /stest/seq 1 Created /stest/seq0000000001 [zk: localhost:2181(CONNECTED) 25] create -s /stest/seq 1 Created /stest/seq0000000002 [zk: localhost:2181(CONNECTED) 26] create -s /stest/seq 1 Created /stest/seq0000000003 [zk: localhost:2181(CONNECTED) 27] create -s /stest/seq 1 Created /stest/seq0000000004 [zk: localhost:2181(CONNECTED) 28] create -s /stest/seq 1 Created /stest/seq0000000005 [zk: localhost:2181(CONNECTED) 29] create -s /stest/seq 1 Created /stest/seq0000000006 [zk: localhost:2181(CONNECTED) 30] create -s /stest/seq 1 Created /stest/seq0000000007 [zk: localhost:2181(CONNECTED) 31] create -s /stest/seq 2 Created /stest/seq0000000008 [zk: localhost:2181(CONNECTED) 32] create -s /stest/seq 2 Created /stest/seq0000000009 [zk: localhost:2181(CONNECTED) 33] create -s /stest/seq 2 Created /stest/seq0000000010 [zk: localhost:2181(CONNECTED) 34] create -s /stest/seq 2 Created /stest/seq0000000011 [zk: localhost:2181(CONNECTED) 35] create -s /stest/seq 2 Created /stest/seq0000000012 [zk: localhost:2181(CONNECTED) 36] create -s /stest/seq 2 Created /stest/seq0000000013 [zk: localhost:2181(CONNECTED) 37] create -s /stest/seq 2 Created /stest/seq0000000014代碼
$zookeeper = new Zookeeper("192.168.1.45:2181"); $aclArray = array( array( "perms" => Zookeeper::PERM_ALL, "scheme" => "world", "id" => "anyone", ) ); $path = "/t3"; //ZOO_EPHEMERAL = 1 //ZOO_SEQUENCE = 2 //這里這里的flag=NULL,flag=0 表示創建永久節點,=1創建臨時節點,=2創建seq 順序節點 $realPath = $zookeeper->create($path, null, $aclArray, 1); var_dump($realPath);worker
一個利用順序臨時節點的leader遷移實現
# worker.php Zookeeper::PERM_ALL, "scheme" => "world", "id" => "anyone" ] ]; private $isLeader = false; private $znode = ""; private $prevNode = ""; public function __construct($host = "", $watcher_cb = null, $recv_timeout = 10000) { $this->zk = new Zookeeper($host, $watcher_cb, $recv_timeout); } public function register() { if (!$this->zk->exists(self::CONTAINER)) { $this->zk->create(self::CONTAINER, null, $this->acl); } # 創建一個臨時的順序節點 $this->znode = $this->zk->create(self::CONTAINER . "/w-", null, $this->acl, Zookeeper::EPHEMERAL | Zookeeper::SEQUENCE); //獲取順序節點名,去除父路徑 $this->znode = str_replace(self::CONTAINER . "/", "", $this->znode); printf("我創建了節點: %s ", self::CONTAINER . "/" . $this->znode); $this->prevNode = $this->getPrev(); if (is_null($this->prevNode)) { $this->setLeader(true); } else { $this->zk->get(self::CONTAINER . "/" . $this->prevNode, [$this, "watcher"]); } } public function getPrev() { $workers = $this->zk->getChildren(self::CONTAINER); sort($workers); $size = count($workers); for ($i = 0; $i < $size; $i++) { if ($this->znode == $workers[$i] && $i > 0) { return $workers[$i - 1]; } } return null; } public function watcher($type, $state, $key) { $prevNode = $this->prevNode; printf("watchNode-getPrevious:%s ", self::CONTAINER . "/" . $prevNode); if (!is_null($prevNode)) { if ($type == 2) { printf($prevNode . " had deleted "); } if ($type == 3) { printf("我重新監控節點:%s ", self::CONTAINER . "/" . $prevNode); $this->zk->get(self::CONTAINER . "/" . $prevNode, [$this, "watcher"]); } } } public function isLeader() { return $this->isLeader; } public function setLeader($flag) { $this->isLeader = $flag; } public function run() { $this->register(); while (true) { if ($this->isLeader()) { $this->leaderJob(); } else { $this->watcherJob(); } sleep(2); } } public function leaderJob() { echo "現在我是Leader "; } public function watcherJob() { echo "我在監控:".(self::CONTAINER . "/" . $this->prevNode)." "; } } $worker = new Worker("192.168.1.45:2181"); $worker->run();嘗試
# 終端1 php worker.php # 終端2 php worker.php # 終端3 php worker.php # 此處運行不會被節點變化不會被監控到再次嘗試
# zkCli create /worker_test/w- /worker_test/0000000020 php worker.php # output 我創建了節點: /worker_test/w-0000000022 我在監控:/worker_test/w-0000000020 我在監控:/worker_test/w-0000000020 watchNode-getPrevious:/worker_test/w-0000000020 我重新監控節點:/worker_test/w-0000000020 我在監控:/worker_test/w-0000000020 我在監控:/worker_test/w-0000000020 我在監控:/worker_test/w-0000000020 我在監控:/worker_test/w-0000000020 我在監控:/worker_test/w-0000000020 watchNode-getPrevious:/worker_test/w-0000000020 我重新監控節點:/worker_test/w-0000000020 2018-08-28 02:11:46,684:2486(0x7f28ed0a1700):ZOO_WARN@zookeeper_interest@1570: Exceeded deadline by 15ms 我在監控:/worker_test/w-0000000020 我在監控:/worker_test/w-0000000020 watchNode-getPrevious:/worker_test/w-0000000020 w-0000000020 had deleted 我在監控:/worker_test/w-0000000020這邊發現一個類似bug問題
如果我們直接運行worker.php 在worker.php運行創建的臨時順序節點是不會被watcher到的 我們必須先首先創建好相關的節點再啟動監控,不知道這里是不是php版本的zookeeper的bug 有了解的小伙伴可以告之下Watcher通知狀態與事件類型一覽
ZOO_CREATED_EVENT(value=1):節點創建事件,需要watch一個不存在的節點,當節點被創建時觸發,此watch通過zoo_exists()設置
ZOO_DELETED_EVENT(value=2):節點刪除事件,此watch通過zoo_exists()或zoo_get()設置
ZOO_CHANGED_EVENT(value=3):節點數據改變事件,此watch通過zoo_exists()或zoo_get()設置
ZOO_CHILD_EVENT(value=4):子節點列表改變事件,此watch通過zoo_get_children()或zoo_get_children2()設置
ZOO_SESSION_EVENT(value=-1):會話事件,客戶端與服務端斷開或重連時觸發
ZOO_NOTWATCHING_EVENT(value=-2):watch移除事件,服務端出于某些原因不再為客戶端watch節點時觸發
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/29341.html
摘要:今天來跟大家分享的是奇虎開源的配置中心。容錯當進程死掉,網絡終端,機器重啟等異常情況發生時,我們希望能盡可能的提供可靠的配置獲取服務。配置更新及時可以秒級同步到所有客戶端機器。本身是沒有的恭喜你,你已經構建完自己的配置中心了。 今天來跟大家分享的是奇虎360開源的 QConf 配置中心。 為什么我們需要做這么一件事情? 因為遇到了,當業務分布較廣,配置分布較廣的時候,就會很容易地出現一...
摘要:消息以為類別記錄將消息種子分類每一類的消息稱之為一個主題。這意味著生產者不等待來自同步完成的確認繼續發送下一條批消息。這意味著在已成功收到的數據并得到確認后發送下一條。三種機制,性能依次遞減吞吐量降低,數據健壯性則依次遞增。 kafka 簡介 Kafka 是一種高吞吐量的分布式發布訂閱消息系統 kafka角色必知 producer:生產者。 consumer:消費者。 topic: 消...
摘要:閱讀本教程前最好先嘗試閱讀下的實踐自帶命令實踐嘗試實踐的知識創建話題生產消息消費消息話題信息獲取消費組獲取消費組的自帶的命令安裝目錄的目錄下代表我們會使用的腳本 閱讀本教程前最好先嘗試閱讀:PHP下kafka的實踐 自帶命令實踐 嘗試實踐的kafka知識: 創建話題 生產消息 消費消息 話題信息 獲取消費組 獲取消費組的offset 自帶的命令 # kafka安裝目錄的bin目錄下...
閱讀 894·2021-09-03 10:42
閱讀 1511·2019-08-30 15:56
閱讀 1444·2019-08-29 17:27
閱讀 870·2019-08-29 15:25
閱讀 3156·2019-08-26 18:27
閱讀 2480·2019-08-26 13:41
閱讀 1888·2019-08-26 10:39
閱讀 1570·2019-08-23 18:36