多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

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

php 連接zookeeper實例

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

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





生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 精品国产欧美另类一区 | 亚洲免费影视 | 最近的中文字幕免费视频1 最近的中文字幕免费完整 最近的中文字幕视频大全高清 | 日韩久久久精品中文字幕 | 最近的中文字幕视频大全高清 | 欧美极品videossex激情 | 日韩欧美亚洲综合一区二区 | 国产精品第8页 | 精品久久免费观看 | 日本午夜在线视频 | 亚洲黄色中文字幕 | 亚洲综合国产一区在线 | 亚洲精品456人成在线 | 欧美人在线 | 久久久久久国产精品视频 | 一本大道加勒比久久 | 成人在线观看视频免费 | 久久91亚洲精品久久91综合 | 欧美亚洲激情视频 | 全国男人天堂网 | 日本69视频 | 成人亚欧网站在线观看 | 最近中文字幕mv免费高清视频免费 | 欧美日韩你懂的 | 天天躁夜夜 | 最近中文字幕免费2019高清 | 久久国产亚洲欧美日韩精品 | 国产a精品 | 毛片色 | 视频一区视频二区在线观看 | 日韩国产另类 | 2021年最新久久久视精品爱 | 午夜 在线播放 | 亚洲五月激情 | 成人午夜精品网站在线观看 | 日本护士ooowww | 自拍 另类 综合 欧美小说 | 欧美亚洲偷图色综合91 | 国产农村妇女毛片精品久久 | 欧美日韩国产一区二区三区欧 | 久久国产三级 |