2015-12-02 61 views
0

我想在PHP应用程序使用的饲养员,我已经完成了大部分的get($path)/set($path, $value)/getChildren($path)功能如下https://github.com/andreiz/php-zookeeper,除了watch_callback功能是行不通的。PHP饲养员守望不起作用

我的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

我写了一个更简单的演示,与此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应用程序只读取来自redis的信息。