怎么安装php的zookeeper扩展
这篇文章主要介绍“怎么安装php的zookeeper扩展”,在日常操作中,相信很多人在怎么安装php的zookeeper扩展问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么安装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
安装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/class="lazy" data-src/c/
./configure --prefix=/usr/local/zookeeper //指定一下安装目录
make && make install
就这样安装完了
安装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即可。
【】
启动zookeeper前要安装jdk 已经安装的可以忽略
在http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html里面下载
然后傻瓜式安装,这里不说了
启动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
复制一下文件
php代码测试
测试代码
<?php class zookeeperDemo { private $zookeeper; function __construct($address) { $this->zookeeper = new Zookeeper($address); } 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); } 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 class Zookeeper_Example { private $zookeeper; private $callback = array(); public function __construct($address) { $this->zookeeper = new Zookeeper($address); } public function set($path, $value) { if (!$this->zookeeper->exists($path)) { $this->makePath($path); $this->makeNode($path, $value); } else { $this->zookeeper->set($path, $value); } } 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); } } } 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); } 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); } public function deleteNode($path) { if(!$this->zookeeper->exists($path)) { return null; } else { return $this->zookeeper->delete($path); } } 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')); } } } 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); } } 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); } ?>
到此,关于“怎么安装php的zookeeper扩展”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341