您好,欢迎访问一九零五行业门户网

php的zookeeper扩展怎么安装

php安装 zookeeper扩展的方法:首先下载zookeeper;然后指定一下安装目录;最后通过“make && make install”安装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('@/$@', $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='') { $parts = explode('/', $path); $parts = array_filter($parts);//过滤空值 $subpath = ''; while (count($parts) > 1) { $subpath .= '/' . array_shift($parts);//数组第一个元素弹出数组 if (!$this->zookeeper->exists($subpath)) { $this->makenode($subpath, $value); } } } /* * 创建节点 * */ private function makenode($path, $value, array $params = array()) { if (empty($params)) { $params = [ [ 'perms' => zookeeper::perm_all, 'scheme' => 'world', 'id' => 'anyone' ] ]; } 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('localhost:2181'); //var_dump($zk->get('/zookeeper')); var_dump($zk->getchildren('/foo')); //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'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 = '') { $parts = explode('/', $path); $parts = array_filter($parts); $subpath = ''; while (count($parts) > 1) { $subpath .= '/' . 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( 'perms' => zookeeper::perm_all, 'scheme' => 'world', 'id' => 'anyone', ) ); } 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('@/$@', $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, 'watchcallback')); } } } /** * 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, 'watchcallback')); 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('localhost:2181'); // var_dump($zk->get('/')); // var_dump($zk->getchildren('/')); // var_dump($zk->set('/test', 'abc')); // var_dump($zk->get('/test')); // var_dump($zk->getchildren('/')); // var_dump($zk->set('/foo/001', 'bar1')); // var_dump($zk->set('/foo/002', 'bar2')); // var_dump($zk->get('/')); // var_dump($zk->getchildren('/')); // var_dump($zk->getchildren('/foo')); //watch example 一旦/test节点的值被改变,就会调用一次callback function callback() { echo "in watch callback\n"; } //$zk->set('/test', 1); $ret = $zk->watch('/test', 'callback'); //$zk->set('/test', 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( '/test', array($this, 'watcher' ) ); } }$zoo = new zookeeperdemo('127.0.0.1:2181');$zoo->get( '/test', array($zoo, 'watcher' ) );while( true ) { echo '.'; sleep(2);}*//*// $zc = new zookeeper(); // $zc->connect('127.0.0.1:2181');// var_dump($zc->get('/zookeeper'));// exit;*/ ?>
代码参考链接:
https://github.com/php-zookeeper/php-zookeeper/blob/master/examples/zookeeper_example.php
以上就是php的zookeeper扩展怎么安装的详细内容。
其它类似信息

推荐信息