日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php框架 > 框架設計 > php 連接zookeeper實例

php 連接zookeeper實例

來源:程序員人生   發布時間:2015-02-07 08:48:58 閱讀次數:13577次

1、安裝成功zookeeper后,在zookeeper 的bin目錄下有啟動相應的啟動腳本

啟動Server

                      ./zkServer.sh start

啟動client:(*注:cli需要安裝java)

                     zkCli.sh

2、PHP實例:

<?php
class ZookeeperDemo extends Zookeeper {
 
  public function watcher( $i, $type, $key ) {
    echo "Insider Watcher " ;
 
    // Watcher gets consumed so we need to set a new one
    $this->get( '/test', array ($this, 'watcher' ) );
  }
}
$zoo = new ZookeeperDemo( '127.0.0.1:2181' );
$zoo->get( '/test', array ($zoo, 'watcher' ) );
while ( true ) {
  echo '.' ;
  sleep(2);
}

leader與worker任務的分配:
class Worker extends Zookeeper {
 
  const CONTAINER = '/cluster' ;
 
  protected $acl = array(
                    array (
                      'perms' => Zookeeper:: PERM_ALL,
                      'scheme' => 'world' ,
                      'id' => 'anyone' ) );
 
  private $isLeader = false;
 
  private $znode ;
 
  public function __construct( $host = '', $watcher_cb = null , $recv_timeout = 10000 ) {
    parent:: __construct( $host, $watcher_cb, $recv_timeout );
  }
 
  public function register() {
    if( ! $this->exists( self ::CONTAINER ) ) {
      $this->create( self ::CONTAINER , null, $this-> acl );
    }
 
    $this->znode = $this->create( self ::CONTAINER . '/w-' ,
                                  null ,
                                  $this->acl,
                                  Zookeeper::EPHEMERAL | Zookeeper::SEQUENCE );
 
    $this-> znode = str_replace( self ::CONTAINER .'/' , '' , $this-> znode );
 
    printf( "I'm registred as: %s ", $this-> znode );
 
    $watching = $this->watchPrevious();
 
    if( $watching == $this-> znode ) {
      printf( "Nobody here, I'm the leader " );
      $this->setLeader( true );
    }
    else {
      printf( "I'm watching %s " , $watching );
    }
  }
 
  public function watchPrevious() {
    $workers = $this->getChildren( self ::CONTAINER );
    sort( $workers );
    $size = sizeof( $workers );
    for( $i = 0 ; $i < $size ; $i++ ) {
      if( $this-> znode == $workers[ $i ] ) {
        if ( $i > 0 ) {
          $this->get( self ::CONTAINER . '/' . $workers[ $i - 1 ], array ( $this, 'watchNode' ) );
          return $workers[ $i - 1 ];
        }
 
        return $workers[ $i ];
      }
    }
 
    throw new Exception(  sprintf( "Something went very wrong! I can't find myself: %s/%s",
                          self ::CONTAINER ,
                          $this-> znode ) );
  }
 
  public function watchNode( $i, $type, $name ) {
    $watching = $this->watchPrevious();
    if( $watching == $this-> znode ) {
      printf( "I'm the new leader! " );
      $this->setLeader( true );
    }
    else {
      printf( "Now I'm watching %s " , $watching );
    }
  }
 
  public function isLeader() {
    return $this-> isLeader ;
  }
 
  public function setLeader($flag) {
    $this-> isLeader = $flag;
  }
 
  public function run() {
    $this->register();
 
    while( true ) {
      if( $this->isLeader() ) {
        $this->doLeaderJob();
    }
    else {
      $this->doWorkerJob();
    }
 
      sleep( 2 );
    }
  }
 
  public function doLeaderJob() {
    echo "Leading " ;
  }
 
  public function doWorkerJob() {
    echo "Working " ;
  }
}
$worker = new Worker( '127.0.0.1:2181' );
$worker->run();
可以啟動3個php進程,查看腳本的運行。
進程1:
[root@localhost zookeeper]# php -f worker.php
I'm registred as: w-0000000010
Nobody here, I'm the leader
Leading
進程2:
[daniel.luo@localhost zookeeper]$ php -f worker.php
I'm registred as: w-0000000011
I'm watching w-0000000010
Working
進程3:
[daniel.luo@localhost zookeeper]$ php -f worker.php
I'm registred as: w-0000000012
I'm watching w-0000000011
Working

ctrl + c 關閉leader進程后,會發現進程2與3會選舉出新的leader





生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 一区二区国产在线 | 精品久久国产 | 日韩免费在线播放 | 中国一级特黄毛片大片 | 97成人在线| 国产精品无码永久免费888 | 亚洲欧洲成人av每日更新 | 久久亚洲欧美 | 自拍欧美亚洲 | 亚洲一区二区三区影视 | 一级aaa级毛片午夜在线播放 | 国产中文一区二区三区 | 尤物国产| 日本久久精品视频 | 国产精品亚洲欧美 | 可以在线观看的av网站 | 免费成人黄色网址 | 日日摸夜夜添夜夜 | 国产精品一区一区三区 | 国产欧美精品一区二区 | 日韩精品免费观看 | 91在线精品一区二区 | 色综合精品久久久久久久 | 亚洲国产精品第一区二区 | 日本黄色成人 | 一级黄色在线播放 | 欧美性大战久久久久久久蜜臀 | 久久久久成人免费 | 欧美日韩亚洲综合 | 免费视频 久久久 | 日韩精品二区 | 欧美日韩精品一二三区 | 三级av在线 | 亚洲美女av在线 | 国产免费网址 | 成人免费黄网站 | 欧洲久久 | 久久国产精品免费视频 | 国产在线一二三区 | 综合色婷婷 | 狠狠av|