我试图在php应用中使用zookeeper,并且我已完成{{3}之后的大多数get($path)
/ set($path, $value)
/ getChildren($path)
个功能} ,但watch_callback
函数除外,它不起作用。
我的php版本是5.6.14并禁用了线程安全,我使用的是apache2.4。
以下是一些代码段
class Zookeeper_Module {
private $zookeeper;
public function __construct(){
$this->ci = & get_instance();
$zookeeper_server = $this->ci->config->item('zookeeper_server');
$this->zookeeper = new Zookeeper($zookeeper_server);
}
public function set($path, $value){
$this->zookeeper->set($path, $value);
}
public function get($path, $watch_cb = null){
return $this->zookeeper->get($path, $watch_cb);
}
public function get_watch_cb($event_type = '', $stat = '', $path = ''){
error_log('hello from get_watcher_cb');
$value = $this->get($path, array($this, 'get_watch_cb'));
// update redis cache
$this->ci->cache->redis->save('some cache key', $value);
}
}
class MyTest{
public function get(){
$zookeeper = new Zookeeper_Module ();
$value = $zookeeper->get( '/foo/bar', array (
$zookeeper,
'get_watch_cb'
) );
}
public function set(){
$zookeeper = new Zookeeper_Module ();
$zookeeper->set( '/foo/bar', 'some value');
}
}
我可以成功获取或设置节点值,但我既无法捕获回调日志,也无法更新redis缓存。
答案 0 :(得分:0)
我写了一个更简单的演示,与此 https://github.com/andreiz/php-zookeeper/wiki非常相似,观察者在此演示中运行良好。
最重要的区别是
while( true ) {
echo '.';
sleep(2);
}
虽然java有一个jvm容器托管观察者,但php没有容器可以做到这一点,所以我们必须使用while(true)
让观察者活着。
所以我在代码中添加while(true)
,现在观察者工作正常。
但我不想在网络应用中添加可怕的while(true)
,所以最终的解决方案是添加一个java应用与zookeeper通信并将结果保存在redis中,而php app只是读取信息来自redis。