0

0

php的zookeeper扩展怎么安装

藏色散人

藏色散人

发布时间:2021-03-08 10:31:04

|

2673人浏览过

|

来源于php中文网

原创

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

立即学习PHP免费学习笔记(深入)”;

在这里面下载最新版的稳定版

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

好买卖商城
好买卖商城

好买卖商城开源商城 是基于Opencart网店系统,针对中文用户而改进的本地化分支,是真正的开源PHP中文网店系统,兼容Opencart的插件。该系统具有易于操作的可视化安装界面、完善的前台商品展示和户在线购物车功能、强大的后台管理和维护功能模块简单易用,灵活的插件机制,更易于扩展。另外,好买卖商城开源商城 还集成集成了支付宝等支付和物流插件,更适合中文用户使用。 好买卖商城2.0开源商城流程进行

下载

【推荐:《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代码测试

测试代码

  1. 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、
     
    
     
     * @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 
     
     * @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

 

keep
keep

Keep是一款健身安排,无论是想减肥塑形或增肌,还是寻找健身跑步瑜伽计步等训练计划,你可以随时随地选择课程进行训练!权威教练视频教学,健身干货自由分享!有需要的小伙伴快来保存下载体验吧!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
java
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

799

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

722

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

727

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

394

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

398

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

445

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

428

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16860

2023.08.03

JavaScript 性能优化与前端调优
JavaScript 性能优化与前端调优

本专题系统讲解 JavaScript 性能优化的核心技术,涵盖页面加载优化、异步编程、内存管理、事件代理、代码分割、懒加载、浏览器缓存机制等。通过多个实际项目示例,帮助开发者掌握 如何通过前端调优提升网站性能,减少加载时间,提高用户体验与页面响应速度。

3

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Apache Storm教程手册
Apache Storm教程手册

共11课时 | 5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号