PHP中zookeeper的类与方法

__construct( $host = , $watcher_cb = null, $recv_timeout = 10000)

$host:zookeeper的ip和端口,多组信息使用,分隔
$watcher_cb:全局监听函数,任何的监听都会执行该回调函数
$recv_timeout:会话超时,在zookeeper的服务器中可以设置 minSessionTimeout和maxSessionTimeout,限制客户端设置session的超时范围

$zookeeper = new Zookeeper("192.168.80.121:2181,192.168.80.122:2181,
192.168.80.123:2181",'globalWatcher',5000);
$zookeeper->get('/redis','redisWatcher');
$zookeeper->get('/rabbit','rabbitWatcher');
function globalWatcher(){
    echo "global watcher\n";
}
function redisWatcher(){
    echo "redis watcher\n";
    $GLOBALS['zookeeper']->get('/redis','redisWatcher');
}
function rabbitWatcher(){
    echo "rabbit watcher\n";
    $GLOBALS['zookeeper']->get('/rabbit','rabbitWatcher');
}

while (true) {
    echo "\n";
    sleep(2);
}

######################
#在zookeeper的cli中执行 set /rabbit abc 和 set /redis abc
global watcher
rabbit watcher

global watcher
redis watche

connect( $host, $watcher_cb = null, $recv_timeout = 10000)

参考:__construct

create( $path, $value, $acl, $flags = null )

$acl:参考:说说Zookeeper中的ACL

格式:$acl = [['scheme'=>'world', 'id'=>'anyone', 'perms'=>Zookeeper::PERM_ALL]];

#为什么这里是二维数组,暂时没有搞明白,也许是为了设置多组不同的权限吧

scheme和id:参考:说说Zookeeper中的ACL

在PHP中permission中,有以下5种,分别对应文中的读、写、增、删、管理以及全部。

const PERM_READ   = 1;
const PERM_WRITE  = 2;
const PERM_CREATE = 4;
const PERM_DELETE = 8;
const PERM_ADMIN  = 16;
const PERM_ALL    = 31;

$flags:参考:ZooKeeper原理及使用

在PHP中的$flags,有以下两种,这里有个比较特别的写法需要注意

const EPHEMERAL = 1; #临时节点
const SEQUENCE  = 2; #有序节点

Zookeeper::EPHEMERAL | Zookeeper::SEQUENCE #特别注意:临时的且有序的节点
# 距离说明
$acl = [['scheme'=>'world', 'id'=>'anyone', 'perms'=>Zookeeper::PERM_ALL]];

$znode = $zookeeper->create('/redis01','abc',$acl); 
//默认是创建永久节点

$znode = $zookeeper->create('/redis02','abc',$acl,Zookeeper::EPHEMERAL); 
//创建临时节点,但在你脚本结束后,马上删除,在cli中使用ls 看不到该节点

$znode = $zookeeper->create('/redis03','abc',$acl,Zookeeper::SEQUENCE); 
//创建永久的有序节点

$znode = $zookeeper->create('/redis04','abc',$acl,Zookeeper::EPHEMERAL | 
Zookeeper::SEQUENCE); 
//创建临时有序节点,同样在ls看不到

// 如果想要在cli中看到临时节点,可以在脚本sleep(10),10秒内在cli中可以看到临时节点

delete( $path, $version = -1 )

$version:会进行version检查,如下表中节点/wanda的数据版本6,可采用delete(‘/wanda’,6)来删除,如果使用非6的版本,则删除失败。

#使用get获得的节点信息
abc
cZxid = 0x20000014c
ctime = Wed Feb 08 08:49:05 UTC 2017
mZxid = 0x200000152
mtime = Wed Feb 08 08:52:53 UTC 2017
pZxid = 0x20000014c
cversion = 0
dataVersion = 6
aclVersion = 0 //acl版本,在setAcl中会使用到
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0

set( $path, $data, $version = -1, &$stat = null )

$version:参考delete方法
$stat:在设置时,同时返回节点信息

$znode = $zookeeper->set('/redis','abc',-1,$stat);
var_dump($znode);
var_dump($stat);

 get( $path, $watcher_cb = null, &$stat = null, $max_size = 0)

$watcher_cb:get后的回调函数
$stat:参考set方法
$max_size:返回最大字符长度

$znode = $zookeeper->get('/redis',null,$stat); //返回abc
$znode = $zookeeper->get('/redis',null,$stat,1); //返回a
var_dump($znode);
var_dump($stat);

getChildren( $path, $watcher_cb = null )

exists( $path, $watcher_cb = null )

getAcl( $path )

setAcl( $path, $version, $acls )

$version:参考:delete方法

$acl = [['scheme'=>'world', 'id'=>'anyone', 'perms'=>Zookeeper::PERM_ALL]];
$znode = $zookeeper->setAcl('/redis',-1,$acl); //不指定ACL版本
$znode = $zookeeper->setAcl('/redis',4,$acl); //指定ACL版本

getClientId( )

setWatcher( $watcher_cb )

getState( )

返回当前客户端链接zookeeper的状态。成功返回1,失败返回false

getRecvTimeout( )

addAuth( $scheme, $cert, $completion_cb = null )

待补充

isRecoverable( )

setLogStream( $file )

getResultMessage( )

setDebugLevel( $level )

设置日志级别,在PHP中有四种级别

const LOG_LEVEL_ERROR = 1;
const LOG_LEVEL_WARN  = 2;
const LOG_LEVEL_INFO  = 3;
const LOG_LEVEL_DEBUG = 4;

setDeterministicConnOrder( $trueOrFalse )

PHP中zookeeper的运行实例:
https://github.com/andreiz/php-zookeeper/blob/master/examples/Zookeeper_Example.php

zookeeper中的PHP API
https://github.com/andreiz/php-zookeeper/blob/master/zookeeper-api.php

zookeeper的php扩展
https://github.com/andreiz/php-zookeeper

PHP中zookeeper的类与方法》上有1条评论

  1. Pingback引用通告: ZooKeeper原理及使用 | 精彩每一天

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>