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