php的zookeeper擴展安裝及使用


 

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即可

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代碼測試

測試代碼1、

  1.  
    <?php
  2.  
    /**
  3.  
    *
  4.  
    */
  5.  
    class zookeeperDemo
  6.  
    {
  7.  
    private $zookeeper;
  8.  
    function __construct($address)
  9.  
    {
  10.  
    $this->zookeeper = new Zookeeper($address);
  11.  
    }
  12.  
    /*
  13.  
    * get
  14.  
    */
  15.  
    public function get($path)
  16.  
    {
  17.  
    if (!$this->zookeeper->exists($path)) {
  18.  
    return null;
  19.  
    }
  20.  
    return $this->zookeeper->get($path);
  21.  
    }
  22.  
     
  23.  
    public function getChildren($path) {
  24.  
    if (strlen($path) > 1 && preg_match('@/$@', $path)) {
  25.  
    // remove trailing /
  26.  
    $path = substr($path, 0, -1);
  27.  
    }
  28.  
    return $this->zookeeper->getChildren($path);
  29.  
    }
  30.  
    /*
  31.  
    * set 值
  32.  
    *
  33.  
    *
  34.  
    */
  35.  
    public function set($path, $value)
  36.  
    {
  37.  
    if (!$this->zookeeper->exists($path)) {
  38.  
    //創建節點
  39.  
    $this->makePath($path);
  40.  
    } else {
  41.  
    $this->zookeeper->set($path,$value);
  42.  
    }
  43.  
     
  44.  
    }
  45.  
    /*
  46.  
    * 創建路徑
  47.  
    */
  48.  
    private function makePath($path, $value='')
  49.  
    {
  50.  
    $parts = explode( '/', $path);
  51.  
    $parts = array_filter($parts); //過濾空值
  52.  
    $subPath = '';
  53.  
    while (count($parts) > 1) {
  54.  
    $subPath .= '/' . array_shift($parts);//數組第一個元素彈出數組
  55.  
    if (!$this->zookeeper->exists($subpath)) {
  56.  
    $this->makeNode($subPath, $value);
  57.  
    }
  58.  
    }
  59.  
    }
  60.  
    /*
  61.  
    * 創建節點
  62.  
    *
  63.  
    */
  64.  
    private function makeNode($path, $value, array $params = array())
  65.  
    {
  66.  
    if (empty($params)) {
  67.  
    $params = [
  68.  
    [
  69.  
    'perms' => Zookeeper::PERM_ALL,
  70.  
    'scheme' => 'world',
  71.  
    'id' => 'anyone'
  72.  
    ]
  73.  
    ];
  74.  
    }
  75.  
    return $this->zookeeper->create($path, $value, $params);
  76.  
    }
  77.  
    /*
  78.  
    * 刪除
  79.  
    **/
  80.  
    public function deleteNode($path)
  81.  
    {
  82.  
    if (!$this->zookeeper->exists($path)) {
  83.  
    return null;
  84.  
    } else {
  85.  
    return $this->zookeeper->delete($path);
  86.  
    }
  87.  
    }
  88.  
     
  89.  
    }
  90.  
    $zk = new zookeeperDemo('localhost:2181');
  91.  
    //var_dump($zk->get('/zookeeper'));
  92.  
    var_dump($zk->getChildren( '/foo'));
  93.  
    //var_dump($zk->deleteNode("/foo"));
  94.  
     
  95.  
    ?>

測試代碼2、

  1.  
    <?php
  2.  
    /**
  3.  
    * PHP Zookeeper
  4.  
    *
  5.  
    * PHP Version 5.3
  6.  
    *
  7.  
    * The PHP License, version 3.01
  8.  
    *
  9.  
    * @category Libraries
  10.  
    * @package PHP-Zookeeper
  11.  
    * @author Lorenzo Alberton <l.alberton@quipo.it>
  12.  
    * @copyright 2012 PHP Group
  13.  
    * @license http://www.php.net/license The PHP License, version 3.01
  14.  
    * @link https://github.com/andreiz/php-zookeeper
  15.  
    */
  16.  
    /**
  17.  
    * Example interaction with the PHP Zookeeper extension
  18.  
    *
  19.  
    * @category Libraries
  20.  
    * @package PHP-Zookeeper
  21.  
    * @author Lorenzo Alberton <l.alberton@quipo.it>
  22.  
    * @copyright 2012 PHP Group
  23.  
    * @license http://www.php.net/license The PHP License, version 3.01
  24.  
    * @link https://github.com/andreiz/php-zookeeper
  25.  
    */
  26.  
    class Zookeeper_Example
  27.  
    {
  28.  
    /**
  29.  
    * @var Zookeeper
  30.  
    */
  31.  
    private $zookeeper;
  32.  
    /**
  33.  
    * @var Callback container
  34.  
    */
  35.  
    private $callback = array();
  36.  
    /**
  37.  
    * Constructor
  38.  
    *
  39.  
    * @param string $address CSV list of host:port values (e.g. "host1:2181,host2:2181")
  40.  
    */
  41.  
    public function __construct($address) {
  42.  
    $this->zookeeper = new Zookeeper($address);
  43.  
    }
  44.  
    /**
  45.  
    * Set a node to a value. If the node doesn't exist yet, it is created.
  46.  
    * Existing values of the node are overwritten
  47.  
    *
  48.  
    * @param string $path The path to the node
  49.  
    * @param mixed $value The new value for the node
  50.  
    *
  51.  
    * @return mixed previous value if set, or null
  52.  
    */
  53.  
    public function set($path, $value) {
  54.  
    if (!$this->zookeeper->exists($path)) {
  55.  
    $this->makePath($path);
  56.  
    $this->makeNode($path, $value);
  57.  
    } else {
  58.  
    $this->zookeeper->set($path, $value);
  59.  
    }
  60.  
    }
  61.  
    /**
  62.  
    * Equivalent of "mkdir -p" on ZooKeeper
  63.  
    *
  64.  
    * @param string $path The path to the node
  65.  
    * @param string $value The value to assign to each new node along the path
  66.  
    *
  67.  
    * @return bool
  68.  
    */
  69.  
    public function makePath($path, $value = '') {
  70.  
    $parts = explode( '/', $path);
  71.  
    $parts = array_filter($parts);
  72.  
    $subpath = '';
  73.  
    while (count($parts) > 1) {
  74.  
    $subpath .= '/' . array_shift($parts);
  75.  
    if (!$this->zookeeper->exists($subpath)) {
  76.  
    $this->makeNode($subpath, $value);
  77.  
    }
  78.  
    }
  79.  
    }
  80.  
    /**
  81.  
    * Create a node on ZooKeeper at the given path
  82.  
    *
  83.  
    * @param string $path The path to the node
  84.  
    * @param string $value The value to assign to the new node
  85.  
    * @param array $params Optional parameters for the Zookeeper node.
  86.  
    * By default, a public node is created
  87.  
    *
  88.  
    * @return string the path to the newly created node or null on failure
  89.  
    */
  90.  
    public function makeNode($path, $value, array $params = array()) {
  91.  
    if (empty($params)) {
  92.  
    $params = array(
  93.  
    array(
  94.  
    'perms' => Zookeeper::PERM_ALL,
  95.  
    'scheme' => 'world',
  96.  
    'id' => 'anyone',
  97.  
    )
  98.  
    );
  99.  
    }
  100.  
    return $this->zookeeper->create($path, $value, $params);
  101.  
    }
  102.  
    /**
  103.  
    * Get the value for the node
  104.  
    *
  105.  
    * @param string $path the path to the node
  106.  
    *
  107.  
    * @return string|null
  108.  
    */
  109.  
    public function get($path) {
  110.  
    if (!$this->zookeeper->exists($path)) {
  111.  
    return null;
  112.  
    }
  113.  
    return $this->zookeeper->get($path);
  114.  
    }
  115.  
    /**
  116.  
    * List the children of the given path, i.e. the name of the directories
  117.  
    * within the current node, if any
  118.  
    *
  119.  
    * @param string $path the path to the node
  120.  
    *
  121.  
    * @return array the subpaths within the given node
  122.  
    */
  123.  
    public function getChildren($path) {
  124.  
    if (strlen($path) > 1 && preg_match('@/$@', $path)) {
  125.  
    // remove trailing /
  126.  
    $path = substr($path, 0, -1);
  127.  
    }
  128.  
    return $this->zookeeper->getChildren($path);
  129.  
    }
  130.  
     
  131.  
    /**
  132.  
    * Delete the node if it does not have any children
  133.  
    *
  134.  
    * @param string $path the path to the node
  135.  
    *
  136.  
    * @return true if node is deleted else null
  137.  
    */
  138.  
     
  139.  
    public function deleteNode($path)
  140.  
    {
  141.  
    if(!$this->zookeeper->exists($path))
  142.  
    {
  143.  
    return null;
  144.  
    }
  145.  
    else
  146.  
    {
  147.  
    return $this->zookeeper->delete($path);
  148.  
    }
  149.  
    }
  150.  
     
  151.  
    /**
  152.  
    * Wath a given path
  153.  
    * @param string $path the path to node
  154.  
    * @param callable $callback callback function
  155.  
    * @return string|null
  156.  
    */
  157.  
    public function watch($path, $callback)
  158.  
    {
  159.  
    if (!is_callable($callback)) {
  160.  
    return null;
  161.  
    }
  162.  
     
  163.  
    if ($this->zookeeper->exists($path)) {
  164.  
    if (!isset($this->callback[$path])) {
  165.  
    $this->callback[$path] = array();
  166.  
    }
  167.  
    if (!in_array($callback, $this->callback[$path])) {
  168.  
    $this->callback[$path][] = $callback;
  169.  
    return $this->zookeeper->get($path, array($this, 'watchCallback'));
  170.  
    }
  171.  
    }
  172.  
    }
  173.  
     
  174.  
    /**
  175.  
    * Wath event callback warper
  176.  
    * @param int $event_type
  177.  
    * @param int $stat
  178.  
    * @param string $path
  179.  
    * @return the return of the callback or null
  180.  
    */
  181.  
    public function watchCallback($event_type, $stat, $path)
  182.  
    {
  183.  
    if (!isset($this->callback[$path])) {
  184.  
    return null;
  185.  
    }
  186.  
     
  187.  
    foreach ($this->callback[$path] as $callback) {
  188.  
    $this->zookeeper->get($path, array($this, 'watchCallback'));
  189.  
    return call_user_func($callback);
  190.  
    }
  191.  
    }
  192.  
     
  193.  
    /**
  194.  
    * Delete watch callback on a node, delete all callback when $callback is null
  195.  
    * @param string $path
  196.  
    * @param callable $callback
  197.  
    * @return boolean|NULL
  198.  
    */
  199.  
    public function cancelWatch($path, $callback = null)
  200.  
    {
  201.  
    if (isset($this->callback[$path])) {
  202.  
    if (empty($callback)) {
  203.  
    unset($this->callback[$path]);
  204.  
    $this->zookeeper->get($path); //reset the callback
  205.  
    return true;
  206.  
    } else {
  207.  
    $key = array_search($callback, $this->callback[$path]);
  208.  
    if ($key !== false) {
  209.  
    unset($this->callback[$path][$key]);
  210.  
    return true;
  211.  
    } else {
  212.  
    return null;
  213.  
    }
  214.  
    }
  215.  
    } else {
  216.  
    return null;
  217.  
    }
  218.  
    }
  219.  
    }
  220.  
    $zk = new Zookeeper_Example('localhost:2181');
  221.  
    // var_dump($zk->get('/'));
  222.  
    // var_dump($zk->getChildren('/'));
  223.  
    // var_dump($zk->set('/test', 'abc'));
  224.  
    // var_dump($zk->get('/test'));
  225.  
    // var_dump($zk->getChildren('/'));
  226.  
    // var_dump($zk->set('/foo/001', 'bar1'));
  227.  
    // var_dump($zk->set('/foo/002', 'bar2'));
  228.  
    // var_dump($zk->get('/'));
  229.  
    // var_dump($zk->getChildren('/'));
  230.  
    // var_dump($zk->getChildren('/foo'));
  231.  
     
  232.  
    //watch example 一旦/test節點的值被改變,就會調用一次callback
  233.  
    function callback() {
  234.  
    echo "in watch callback\n";
  235.  
    }
  236.  
    //$zk->set('/test', 1);
  237.  
    $ret = $zk->watch( '/test', 'callback');
  238.  
    //$zk->set('/test', 2);//在終端執行
  239.  
    while (true) {
  240.  
    sleep( 1);
  241.  
    }
  242.  
     
  243.  
    /*
  244.  
    class ZookeeperDemo extends Zookeeper {
  245.  
     
  246.  
    public function watcher( $i, $type, $key ) {
  247.  
    echo "Insider Watcher\n";
  248.  
     
  249.  
    // Watcher gets consumed so we need to set a new one
  250.  
     
  251.  
    //ZooKeeper提供了可以綁定在znode的監視器。如果監視器發現znode發生變化,該service會立即通知所有相關的客戶端。這就是PH//P腳本如何知道變化的。Zookeeper::get方法的第二個參數是回調函數。當觸發事件時,監視器會被消費掉,所以我們需要在回調函
  252.  
    //數中再次設置監視器。
  253.  
     
  254.  
    $this->get( '/test', array($this, 'watcher' ) );
  255.  
     
  256.  
    }
  257.  
     
  258.  
    }
  259.  
     
  260.  
    $zoo = new ZookeeperDemo('127.0.0.1:2181');
  261.  
    $zoo->get( '/test', array($zoo, 'watcher' ) );
  262.  
     
  263.  
    while( true ) {
  264.  
    echo '.';
  265.  
    sleep(2);
  266.  
    }
  267.  
    */
  268.  
    /*
  269.  
     
  270.  
    // $zc = new Zookeeper();
  271.  
     
  272.  
    // $zc->connect('127.0.0.1:2181');
  273.  
     
  274.  
    // var_dump($zc->get('/zookeeper'));
  275.  
     
  276.  
    // exit;
  277.  
    */
  278.  
     
  279.  
    ?>

代碼參考鏈接:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM