php动物园管理员观察者不;不起作用


php zookeeper watcher doesn't work

我正在尝试在php应用程序中使用zookeeper,并且我已经完成了以下大多数get($path)/set($path, $value)/getChildren($path)函数https://github.com/andreiz/php-zookeeper,除了watch_callback函数不起作用。

我的php版本是5.6.4,线程安全被禁用,我使用的是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');
    }
}

我可以成功地获取或设置节点值,但既不能捕获watch回调日志,也不能更新redis缓存。

我写了一个更简单的演示,与此非常相似https://github.com/andreiz/php-zookeeper/wiki,并且观察者在这个演示中工作得很好。

最显著的差异是

while( true ) {
    echo '.';
    sleep(2);
}

虽然java有一个jvm容器来托管观察者,但php没有一个容器来做这件事,所以我们必须使用while(true)来保持观察者的活力。

因此,我在代码中添加了一个while(true),现在观察程序运行良好。

但我不想在web应用程序中添加一个糟糕的while(true),所以最终的解决方案是添加一个java应用程序来与zookeeper通信并将结果保存在redis中,而php应用程序只是从redis中读取信息。