首頁 後端開發 PHP問題 php的zookeeper擴充功能怎麼安裝

php的zookeeper擴充功能怎麼安裝

Mar 08, 2021 am 10:31 AM
zookeeper

php安裝 zookeeper擴充功能的方法:先下載zookeeper;然後指定安裝目錄;最後透過「make && make install」安裝zookeeper即可。

php的zookeeper擴充功能怎麼安裝

本文操作環境:Windows7系統、PHP7.1、Dell G3電腦。

ZooKeeper是一個分散式的,開放原始碼的分散式應用程式協調服務,是Google的Chubby一個開源的實現,是Hadoop和Hbase的重要元件。它是一個為分散式應用提供一致性服務的軟體,提供的功能包括:配置維護、網域服務、分散式同步、群組服務等。

ZooKeeper的目標就是封裝好複雜易出錯的關鍵服務,將簡單易用的介面和效能高效、功能穩定的系統提供給使用者。

要在php中使用zookeeper,先要安裝php zookeeper擴展,要安裝php zookeeper擴展,得先安裝zookeeper

1、安裝zookeeper

在這裡面下載最新版的穩定版

http://mirror.bit.edu.cn/apache/zookeeper/stable/

cd /download

wget http://mirror .bit.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz //這個是已經安裝好的工具,下面我們還需要自己編譯安裝一下,因為後面安裝php的擴充時用得到

tar -zxvf zookeeper-3.4.12.tar.gz

cd zookeeper-3.4.12/src/c/

#./configure --prefix=/usr /local/zookeeper  //指定一下安裝目錄

make && make install

就這樣安裝完了

2、安裝php zookeeper的擴充  在 http://pecl. php.net/package/zookeeper找找

cd /download

wget http://pecl.php.net/get/zookeeper-0.6.2.tgz

tar -zxvf zookeeper-0.6.2.tgz

 cd zookeeper-0.6.2

./configure --with-libzookeeper-dir=/usr/local/zookeeper //要指定依賴

make && make install

配置php.ini

extension="/usr/local/Cellar/php/7.2.6/pecl/20170718/zookeeper.so"

重啟php-fpm即可。

【推薦:《PHP影片教學》】

3、啟動zookeeper前要安裝jdk  已經安裝的可以忽略

在http:/ /www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html裡面下載

然後傻瓜式安裝,這裡不說了

4、啟動zookeeper

cd /download/zookeeper-3.4.12/bin

 ./zkServer.sh start

#./zkCli.sh -server 127.0.0.1:2181

cli方式開啟

注意:

如果報錯:

cd ../conf

cp zoo_sample.cfg zoo.cfg

複製一下檔案

5、php程式碼測試

##測試程式碼

    <?php
     
    /**
     
     * 
     
     */
     
    class zookeeperDemo
     
    {
     
    private $zookeeper;
     
    function __construct($address)
     
    {
     
    $this->zookeeper = new Zookeeper($address);
     
    }
     
    /*
     
     * get 
     
     */
     
    public function get($path)
     
    {
     
    if (!$this->zookeeper->exists($path)) {
     
    return null;
     
    }
     
    return $this->zookeeper->get($path);
     
    }
     
     
     
    public function getChildren($path) {
     
    if (strlen($path) > 1 && preg_match(&#39;@/$@&#39;, $path)) {
     
    // remove trailing /
     
    $path = substr($path, 0, -1);
     
    }
     
    return $this->zookeeper->getChildren($path);
     
    }
     
    /*
     
     * set 值
     
     *
     
     *
     
     */
     
    public function set($path, $value)
     
    {
     
    if (!$this->zookeeper->exists($path)) {
     
    //创建节点
     
    $this->makePath($path);
     
    } else {
     
    $this->zookeeper->set($path,$value);
     
    }
     
     
     
    }
     
    /*
     
     * 创建路径
     
     */
     
    private function makePath($path, $value=&#39;&#39;)
     
    {
     
    $parts = explode(&#39;/&#39;, $path);
     
    $parts = array_filter($parts);//过滤空值
     
    $subPath = &#39;&#39;;
     
    while (count($parts) > 1) {
     
    $subPath .= &#39;/&#39; . array_shift($parts);//数组第一个元素弹出数组
     
    if (!$this->zookeeper->exists($subpath)) {
     
    $this->makeNode($subPath, $value);
     
    }
     
    }
     
    }
     
    /*
     
     * 创建节点
     
     *
     
     */
     
    private function makeNode($path, $value, array $params = array())
     
    {
     
    if (empty($params)) {
     
    $params = [
     
    [
     
    &#39;perms&#39; => Zookeeper::PERM_ALL,
     
    &#39;scheme&#39; => &#39;world&#39;,
     
    &#39;id&#39; => &#39;anyone&#39;
     
    ]
     
    ];
     
    }
     
    return $this->zookeeper->create($path, $value, $params);
     
    }
     
    /*
     
     * 删除
     
     **/
     
    public function deleteNode($path)
     
    {
     
    if (!$this->zookeeper->exists($path)) {
     
    return null;
     
    } else {
     
    return $this->zookeeper->delete($path);
     
    }
     
    }
     
     
     
    }
     
    $zk = new zookeeperDemo(&#39;localhost:2181&#39;);
     
    //var_dump($zk->get(&#39;/zookeeper&#39;));
     
    var_dump($zk->getChildren(&#39;/foo&#39;));
     
    //var_dump($zk->deleteNode("/foo"));
     
     
     
    ?>
    测试代码2、
     
    <?php
     
    /**
     
     * PHP Zookeeper
     
     *
     
     * PHP Version 5.3
     
     *
     
     * The PHP License, version 3.01
     
     *
     
     * @category Libraries
     
     * @package PHP-Zookeeper
     
     * @author Lorenzo Alberton <l.alberton@quipo.it>
     
     * @copyright 2012 PHP Group
     
     * @license http://www.php.net/license The PHP License, version 3.01
     
     * @link https://github.com/andreiz/php-zookeeper
     
     */
     
    /**
     
     * Example interaction with the PHP Zookeeper extension
     
     *
     
     * @category Libraries
     
     * @package PHP-Zookeeper
     
     * @author Lorenzo Alberton <l.alberton@quipo.it>
     
     * @copyright 2012 PHP Group
     
     * @license http://www.php.net/license The PHP License, version 3.01
     
     * @link https://github.com/andreiz/php-zookeeper
     
     */
     
    class Zookeeper_Example
     
    {
     
    /**
     
     * @var Zookeeper
     
     */
     
    private $zookeeper;
     
    /**
     
     * @var Callback container
     
     */
     
    private $callback = array();
     
    /**
     
     * Constructor
     
     *
     
     * @param string $address CSV list of host:port values (e.g. "host1:2181,host2:2181")
     
     */
     
    public function __construct($address) {
     
    $this->zookeeper = new Zookeeper($address);
     
    }
     
    /**
     
     * Set a node to a value. If the node doesn&#39;t exist yet, it is created.
     
     * Existing values of the node are overwritten
     
     *
     
     * @param string $path The path to the node
     
     * @param mixed $value The new value for the node
     
     *
     
     * @return mixed previous value if set, or null
     
     */
     
    public function set($path, $value) {
     
    if (!$this->zookeeper->exists($path)) {
     
    $this->makePath($path);
     
    $this->makeNode($path, $value);
     
    } else {
     
    $this->zookeeper->set($path, $value);
     
    }
     
    }
     
    /**
     
     * Equivalent of "mkdir -p" on ZooKeeper
     
     *
     
     * @param string $path The path to the node
     
     * @param string $value The value to assign to each new node along the path
     
     *
     
     * @return bool
     
     */
     
    public function makePath($path, $value = &#39;&#39;) {
     
    $parts = explode(&#39;/&#39;, $path);
     
    $parts = array_filter($parts);
     
    $subpath = &#39;&#39;;
     
    while (count($parts) > 1) {
     
    $subpath .= &#39;/&#39; . array_shift($parts);
     
    if (!$this->zookeeper->exists($subpath)) {
     
    $this->makeNode($subpath, $value);
     
    }
     
    }
     
    }
     
    /**
     
     * Create a node on ZooKeeper at the given path
     
     *
     
     * @param string $path The path to the node
     
     * @param string $value The value to assign to the new node
     
     * @param array $params Optional parameters for the Zookeeper node.
     
     * By default, a public node is created
     
     *
     
     * @return string the path to the newly created node or null on failure
     
     */
     
    public function makeNode($path, $value, array $params = array()) {
     
    if (empty($params)) {
     
    $params = array(
     
    array(
     
    &#39;perms&#39; => Zookeeper::PERM_ALL,
     
    &#39;scheme&#39; => &#39;world&#39;,
     
    &#39;id&#39; => &#39;anyone&#39;,
     
    )
     
    );
     
    }
     
    return $this->zookeeper->create($path, $value, $params);
     
    }
     
    /**
     
     * Get the value for the node
     
     *
     
     * @param string $path the path to the node
     
     *
     
     * @return string|null
     
     */
     
    public function get($path) {
     
    if (!$this->zookeeper->exists($path)) {
     
    return null;
     
    }
     
    return $this->zookeeper->get($path);
     
    }
     
    /**
     
     * List the children of the given path, i.e. the name of the directories
     
     * within the current node, if any
     
     *
     
     * @param string $path the path to the node
     
     *
     
     * @return array the subpaths within the given node
     
     */
     
    public function getChildren($path) {
     
    if (strlen($path) > 1 && preg_match(&#39;@/$@&#39;, $path)) {
     
    // remove trailing /
     
    $path = substr($path, 0, -1);
     
    }
     
    return $this->zookeeper->getChildren($path);
     
    }
     
     
     
    /**
     
     * Delete the node if it does not have any children
     
     * 
     
     * @param string $path the path to the node
     
     * 
     
     * @return true if node is deleted else null
     
     */
     
     
     
    public function deleteNode($path)
     
    {
     
    if(!$this->zookeeper->exists($path))
     
    {
     
    return null;
     
    }
     
    else
     
    {
     
    return $this->zookeeper->delete($path);
     
    }
     
    }
     
     
     
    /**
     
     * Wath a given path
     
     * @param string $path the path to node
     
     * @param callable $callback callback function
     
     * @return string|null
     
     */
     
    public function watch($path, $callback)
     
    {
     
    if (!is_callable($callback)) {
     
    return null;
     
    }
     
     
     
    if ($this->zookeeper->exists($path)) {
     
    if (!isset($this->callback[$path])) {
     
    $this->callback[$path] = array();
     
    }
     
    if (!in_array($callback, $this->callback[$path])) {
     
    $this->callback[$path][] = $callback;
     
    return $this->zookeeper->get($path, array($this, &#39;watchCallback&#39;));
     
    }
     
    }
     
    }
     
     
     
    /**
     
     * Wath event callback warper
     
     * @param int $event_type
     
     * @param int $stat
     
     * @param string $path
     
     * @return the return of the callback or null
     
     */
     
    public function watchCallback($event_type, $stat, $path)
     
    {
     
    if (!isset($this->callback[$path])) {
     
    return null;
     
    }
     
     
     
    foreach ($this->callback[$path] as $callback) {
     
    $this->zookeeper->get($path, array($this, &#39;watchCallback&#39;));
     
    return call_user_func($callback);
     
    }
     
    }
     
     
     
    /**
     
     * Delete watch callback on a node, delete all callback when $callback is null
     
     * @param string $path
     
     * @param callable $callback
     
     * @return boolean|NULL
     
     */
     
    public function cancelWatch($path, $callback = null)
     
    {
     
    if (isset($this->callback[$path])) {
     
    if (empty($callback)) {
     
    unset($this->callback[$path]);
     
    $this->zookeeper->get($path); //reset the callback
     
    return true;
     
    } else {
     
    $key = array_search($callback, $this->callback[$path]);
     
    if ($key !== false) {
     
    unset($this->callback[$path][$key]);
     
    return true;
     
    } else {
     
    return null;
     
    }
     
    }
     
    } else {
     
    return null;
     
    }
     
    }
     
    }
     
    $zk = new Zookeeper_Example(&#39;localhost:2181&#39;);
     
    // var_dump($zk->get(&#39;/&#39;));
     
    // var_dump($zk->getChildren(&#39;/&#39;));
     
    // var_dump($zk->set(&#39;/test&#39;, &#39;abc&#39;));
     
    // var_dump($zk->get(&#39;/test&#39;));
     
    // var_dump($zk->getChildren(&#39;/&#39;));
     
    // var_dump($zk->set(&#39;/foo/001&#39;, &#39;bar1&#39;));
     
    // var_dump($zk->set(&#39;/foo/002&#39;, &#39;bar2&#39;));
     
    // var_dump($zk->get(&#39;/&#39;));
     
    // var_dump($zk->getChildren(&#39;/&#39;));
     
    // var_dump($zk->getChildren(&#39;/foo&#39;));
     
     
     
    //watch example 一旦/test节点的值被改变,就会调用一次callback
     
    function callback() {
     
    echo "in watch callback\n";
     
    }
     
    //$zk->set(&#39;/test&#39;, 1);
     
    $ret = $zk->watch(&#39;/test&#39;, &#39;callback&#39;); 
     
    //$zk->set(&#39;/test&#39;, 2);//在终端执行
     
    while (true) {
     
    sleep(1);
     
    }
     
     
     
    /*
     
    class ZookeeperDemo extends Zookeeper {
     
     
     
     public function watcher( $i, $type, $key ) {
     
     echo "Insider Watcher\n";
     
     
     
     // Watcher gets consumed so we need to set a new one
     
     
     
    //ZooKeeper提供了可以绑定在znode的监视器。如果监视器发现znode发生变化,该service会立即通知所有相关的客户端。这就是PH//P脚本如何知道变化的。Zookeeper::get方法的第二个参数是回调函数。当触发事件时,监视器会被消费掉,所以我们需要在回调函
     
    //数中再次设置监视器。
     
     
     
     $this->get( &#39;/test&#39;, array($this, &#39;watcher&#39; ) );
     
     
     
     }
     
     
     
    }
    
    $zoo = new ZookeeperDemo(&#39;127.0.0.1:2181&#39;);
    $zoo->get( &#39;/test&#39;, array($zoo, &#39;watcher&#39; ) );
    while( true ) {
     echo &#39;.&#39;;
     sleep(2);
    }
    */
    /*
    
    // $zc = new Zookeeper();
     
    // $zc->connect(&#39;127.0.0.1:2181&#39;);
    // var_dump($zc->get(&#39;/zookeeper&#39;));
    // exit;
    */
     
    ?>
    登入後複製
程式碼參考連結:

https://github.com/php-zookeeper/php-zookeeper/blob/master/ examples/Zookeeper_Example.php

 

以上是php的zookeeper擴充功能怎麼安裝的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Java API 開發中使用 ZooKeeper 進行分散式鎖定處理 Java API 開發中使用 ZooKeeper 進行分散式鎖定處理 Jun 17, 2023 pm 10:36 PM

隨著現代應用程式的不斷發展和對高可用性和並發性的需求日益增長,分散式系統架構變得越來越普遍。在分散式系統中,多個進程或節點同時運作並共同完成任務,進程之間的同步變得特別重要。由於分散式環境下許多節點可以同時存取共享資源,因此,在分散式系統中,如何處理並發和同步問題成為了一項重要的任務。在此方面,ZooKeeper已經成為了一個非常流行的解決方案。 ZooKee

在Beego中使用ZooKeeper和Curator進行分散式協調和管理 在Beego中使用ZooKeeper和Curator進行分散式協調和管理 Jun 22, 2023 pm 09:27 PM

隨著互聯網的迅速發展,分散式系統已經成為了許多企業和組織中的基礎設施之一。而要讓一個分散式系統能夠正常運作,就需要對其進行協調和管理。在這方面,ZooKeeper和Curator是兩個非常值得使用的工具。 ZooKeeper是一個非常受歡迎的分散式協調服務,它可以幫助我們在一個叢集中協調各個節點之間的狀態和資料。 Curator則是對ZooKeeper進行封裝

分散式鎖用 Redis 還是 Zookeeper? 分散式鎖用 Redis 還是 Zookeeper? Aug 22, 2023 pm 03:48 PM

分散式鎖定的實作方式通常有:資料庫、快取(例如:Redis)、Zookeeper、etcd,實際開發中,使用的最多還是Redis和Zookeeper,所以,本文就只聊這兩種。

php如何使用PHP的Zookeeper擴充? php如何使用PHP的Zookeeper擴充? Jun 02, 2023 pm 09:01 PM

PHP是一種非常流行的程式語言,廣泛應用於Web應用程式和伺服器端開發。 Zookeeper是一個分散式的協調服務,用於管理、協調和監控分散式應用程式和服務。在PHP應用程式中使用Zookeeper可以提高應用程式的效能和可靠性。本文將介紹如何使用PHP的Zookeeper擴充。一、安裝Zookeeper擴充功能使用Zookeeper擴充功能需要安裝Zookeeper

在Beego中使用ZooKeeper實現服務註冊和發現 在Beego中使用ZooKeeper實現服務註冊和發現 Jun 22, 2023 am 08:21 AM

在微服務架構中,服務的註冊和發現是一個非常重要的議題。為了解決這個問題,我們可以使用ZooKeeper作為服務註冊中心。在本文中,我們將介紹如何在Beego框架中使用ZooKeeper來實現服務註冊和發現。一、ZooKeeper簡介ZooKeeper是一個分散式的,開源的分散式協調服務,它是ApacheHadoop的子專案之一。 ZooKeeper的主要作用

【建議收藏】靈魂拷問! Zookeeper的31連環砲 【建議收藏】靈魂拷問! Zookeeper的31連環砲 Aug 28, 2023 pm 04:45 PM

ZooKeeper 是一個開源的分散式協調服務。它是一個為分散式應用提供一致性服務的軟體,分散式應用程式可以基於Zookeeper 實現諸如資料發布/訂閱、負載平衡、命名服務、分散式協調/通知、叢集管理、Master 選舉、分散式鎖定和分散式佇列等功能。

Redis實現分散式鎖定的ZooKeeper對比 Redis實現分散式鎖定的ZooKeeper對比 Jun 20, 2023 pm 03:19 PM

隨著互聯網技術的迅速發展,分散式系統在現代應用中已被廣泛應用,特別是在大型互聯網企業中更是不可或缺。但是在分散式系統中,各個節點之間要保持一致性是非常困難的,因此分散式鎖定機製成為了解決這個問題的基礎之一。在分散式鎖定的實作中,Redis和ZooKeeper都是比較流行的工具,本文將對它們進行一些比較和分析。 Redis實作分散式鎖Redis是開源的記憶體資料存

SpringBoot中如何整合Dubbo zookeeper SpringBoot中如何整合Dubbo zookeeper May 17, 2023 pm 02:16 PM

dockerpullzookeeperdockerrun--namezk01-p2181:2181--restartalways-d2e30cac00aca顯示zookeeper已成功啟動Zookeeper和Dubbo•ZooKeeperZooKeeper是一個分散式的,開放原始碼的分散式應用程式協調。它是一個為分散式應用提供一致性服務的軟體,提供的功能包括:配置維護、網域服務、分散式同步、群組服務等。 DubboDubbo是Alibaba開源的分散式服務框架,它最大的特色是按照分層的方式來架構,

See all articles