php zookeeper api介绍


这里写图片描述

简介

Apache Zookeeper 是由 Apache Hadoop 的 Zookeeper 子项目发展而来,现在已经成为了 Apache 的顶级项目。Zookeeper 为分布式系统提供了高效可靠且易于使用的协同服务,它可以为分布式应用提供相当多的服务,诸如统一命名服务,配置管理,状态同步和组服务等。 Zookeeper 接口简单,开发人员不必过多地纠结在分布式系统编程难于处理的同步和一致性问题上,你可以使用 Zookeeper 提供的现成(off-the-shelf)服务来实现分布式系统的配置管理,组管理,Leader 选举等功能。

Zookeeper 维护了大规模分布式系统中的常用对象,比如配置信息,层次化命名空间等,本文将从开发者的角度详细介绍 Zookeeper 的配置信息的意义以及 Zookeeper 的典型应用场景(配置文件的管理、集群管理、分布式队列、同步锁、Leader 选举、队列管理等)。

上一篇博客主要讲了Apache Zookeeper的安装与配置,本文主要介绍Zookeeper PHP API

Zookeeper PHP API

安装php-zookeeper扩展,Mac机子上本人习惯用homebrew来管理自己的环境,下面介绍怎样用homebrew安装这个扩展

不知道怎么安装homebrew的童鞋们看这里https://brew.sh/

$brew install php56-zookeeper

安装完之后,通过下面的命令查看是否安装成功

$php -m
[PHP Modules]
apc
apcu
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
ereg
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
igbinary
json
ldap
libxml
mbstring
mcrypt
memcache
mhash
mongodb
mysqli
mysqlnd
odbc
openssl
pcntl
pcre
PDO
pdo_mysql
PDO_ODBC
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
swoole
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib
zookeeper

[Zend Modules]
Xdebug

下面给出PHP API基本操作

<?php
// 创建一个与服务器的连接
$zookeeper = new Zookeeper('127.0.0.1:2181');
$aclArray = array(
array(
'perms'  => Zookeeper::PERM_ALL,
'scheme' => 'world',
'id'     => 'anyone',
)
);
// 创建一个目录节点
$path = '/path';

if ( ! $zookeeper->exists($path))
$zookeeper->create($path, "parent", $aclArray);

$childPath = '/path/child';

if ( ! $zookeeper->exists($childPath))
$zookeeper->create($childPath,"child",$aclArray);


function callback(){
    echo "callback01";
}

// 判断目录是否存在
if ($zookeeper->exists($childPath)){
    // 取出子目录节点内容
    var_dump($zookeeper->get($childPath,call_user_func("callback")));
}

$zookeeper->set($childPath,'child01');

// 判断目录是否存在
if ($zookeeper->exists($childPath)){
    // 取出子目录节点内容
    var_dump($zookeeper->get($childPath));
}

// 删除目录节点
$zookeeper->delete($childPath);

// 判断目录是否存在
if ($zookeeper->exists($childPath)){
    var_dump("true");
} else {
    var_dump("false");
}

上面只介绍了一些常用的方法,要想了解更多的方法,请查看https://secure.php.net/manual/en/class.zookeeper.php

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值