怎么安裝php的zookeeper擴(kuò)展

這篇文章主要介紹“怎么安裝php的zookeeper擴(kuò)展”,在日常操作中,相信很多人在怎么安裝php的zookeeper擴(kuò)展問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么安裝php的zookeeper擴(kuò)展”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

創(chuàng)新互聯(lián)建站是一家專業(yè)提供滿洲企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站制作、網(wǎng)站設(shè)計(jì)H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為滿洲眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。

php安裝 zookeeper擴(kuò)展的方法:首先下載zookeeper;然后指定一下安裝目錄;最后通過(guò)“make && make install”安裝zookeeper即可。

怎么安裝php的zookeeper擴(kuò)展

本文操作環(huán)境:Windows7系統(tǒng)、PHP7.1、Dell G3電腦。

ZooKeeper是一個(gè)分布式的,開放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是Google的Chubby一個(gè)開源的實(shí)現(xiàn),是Hadoop和Hbase的重要組件。它是一個(gè)為分布式應(yīng)用提供一致性服務(wù)的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等。

ZooKeeper的目標(biāo)就是封裝好復(fù)雜易出錯(cuò)的關(guān)鍵服務(wù),將簡(jiǎn)單易用的接口和性能高效、功能穩(wěn)定的系統(tǒng)提供給用戶。

要在php中使用zookeeper,先要安裝php zookeeper擴(kuò)展,要安裝php zookeeper擴(kuò)展,得先安裝zookeeper

1、安裝zookeeper

在這里面下載最新版的穩(wěn)定版

http://mirror.bit.edu.cn/apache/zookeeper/stable/

cd /download

wget http://mirror.bit.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz //這個(gè)是已經(jīng)安裝好的工具,下面我們還需要自己編譯安裝一下,因?yàn)楹竺姘惭bphp的擴(kuò)展時(shí)用得到

tar -zxvf zookeeper-3.4.12.tar.gz

cd zookeeper-3.4.12/src/c/

./configure --prefix=/usr/local/zookeeper  //指定一下安裝目錄

make && make install

就這樣安裝完了

2、安裝php zookeeper的擴(kuò)展  在 http://pecl.php.net/package/zookeeper中找

cd /download

wget http://pecl.php.net/get/zookeeper-0.6.2.tgz

tar -zxvf zookeeper-0.6.2.tgz

cd zookeeper-0.6.2

./configure --with-libzookeeper-dir=/usr/local/zookeeper //要指定依賴

make && make install

配置php.ini

extension="/usr/local/Cellar/php/7.2.6/pecl/20170718/zookeeper.so"

重啟php-fpm即可。

【】

3、啟動(dòng)zookeeper前要安裝jdk  已經(jīng)安裝的可以忽略

在http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html里面下載

怎么安裝php的zookeeper擴(kuò)展

然后傻瓜式安裝,這里不說(shuō)了

4、啟動(dòng)zookeeper

cd /download/zookeeper-3.4.12/bin

./zkServer.sh start

怎么安裝php的zookeeper擴(kuò)展

./zkCli.sh -server 127.0.0.1:2181

cli方式開啟

注意:

如果報(bào)錯(cuò):

怎么安裝php的zookeeper擴(kuò)展

cd ../conf

cp zoo_sample.cfg zoo.cfg

復(fù)制一下文件

5、php代碼測(cè)試

測(cè)試代碼

  1. <?php
     
    /**
     
     * 
     
     */
     
    class zookeeperDemo
     
    {
     
    private $zookeeper;
     
    function __construct($address)
     
    {
     
    $this->zookeeper = new Zookeeper($address);
     
    }
     
    /*
     
     * get 
     
     */
     
    public function get($path)
     
    {
     
    if (!$this->zookeeper->exists($path)) {
     
    return null;
     
    }
     
    return $this->zookeeper->get($path);
     
    }
     
     
     
    public function getChildren($path) {
     
    if (strlen($path) > 1 && preg_match('@/$@', $path)) {
     
    // remove trailing /
     
    $path = substr($path, 0, -1);
     
    }
     
    return $this->zookeeper->getChildren($path);
     
    }
     
    /*
     
     * set 值
     
     *
     
     *
     
     */
     
    public function set($path, $value)
     
    {
     
    if (!$this->zookeeper->exists($path)) {
     
    //創(chuàng)建節(jié)點(diǎn)
     
    $this->makePath($path);
     
    } else {
     
    $this->zookeeper->set($path,$value);
     
    }
     
     
     
    }
     
    /*
     
     * 創(chuàng)建路徑
     
     */
     
    private function makePath($path, $value='')
     
    {
     
    $parts = explode('/', $path);
     
    $parts = array_filter($parts);//過(guò)濾空值
     
    $subPath = '';
     
    while (count($parts) > 1) {
     
    $subPath .= '/' . array_shift($parts);//數(shù)組第一個(gè)元素彈出數(shù)組
     
    if (!$this->zookeeper->exists($subpath)) {
     
    $this->makeNode($subPath, $value);
     
    }
     
    }
     
    }
     
    /*
     
     * 創(chuàng)建節(jié)點(diǎn)
     
     *
     
     */
     
    private function makeNode($path, $value, array $params = array())
     
    {
     
    if (empty($params)) {
     
    $params = [
     
    [
     
    'perms' => Zookeeper::PERM_ALL,
     
    'scheme' => 'world',
     
    'id' => 'anyone'
     
    ]
     
    ];
     
    }
     
    return $this->zookeeper->create($path, $value, $params);
     
    }
     
    /*
     
     * 刪除
     
     **/
     
    public function deleteNode($path)
     
    {
     
    if (!$this->zookeeper->exists($path)) {
     
    return null;
     
    } else {
     
    return $this->zookeeper->delete($path);
     
    }
     
    }
     
     
     
    }
     
    $zk = new zookeeperDemo('localhost:2181');
     
    //var_dump($zk->get('/zookeeper'));
     
    var_dump($zk->getChildren('/foo'));
     
    //var_dump($zk->deleteNode("/foo"));
     
     
     
    ?>
    測(cè)試代碼2、
     
    <?php
     
    /**
     
     * PHP Zookeeper
     
     *
     
     * PHP Version 5.3
     
     *
     
     * The PHP License, version 3.01
     
     *
     
     * @category Libraries
     
     * @package PHP-Zookeeper
     
     * @author Lorenzo Alberton <l.alberton@quipo.it>
     
     * @copyright 2012 PHP Group
     
     * @license http://www.php.net/license The PHP License, version 3.01
     
     * @link https://github.com/andreiz/php-zookeeper
     
     */
     
    /**
     
     * Example interaction with the PHP Zookeeper extension
     
     *
     
     * @category Libraries
     
     * @package PHP-Zookeeper
     
     * @author Lorenzo Alberton <l.alberton@quipo.it>
     
     * @copyright 2012 PHP Group
     
     * @license http://www.php.net/license The PHP License, version 3.01
     
     * @link https://github.com/andreiz/php-zookeeper
     
     */
     
    class Zookeeper_Example
     
    {
     
    /**
     
     * @var Zookeeper
     
     */
     
    private $zookeeper;
     
    /**
     
     * @var Callback container
     
     */
     
    private $callback = array();
     
    /**
     
     * Constructor
     
     *
     
     * @param string $address CSV list of host:port values (e.g. "host1:2181,host2:2181")
     
     */
     
    public function __construct($address) {
     
    $this->zookeeper = new Zookeeper($address);
     
    }
     
    /**
     
     * Set a node to a value. If the node doesn't exist yet, it is created.
     
     * Existing values of the node are overwritten
     
     *
     
     * @param string $path The path to the node
     
     * @param mixed $value The new value for the node
     
     *
     
     * @return mixed previous value if set, or null
     
     */
     
    public function set($path, $value) {
     
    if (!$this->zookeeper->exists($path)) {
     
    $this->makePath($path);
     
    $this->makeNode($path, $value);
     
    } else {
     
    $this->zookeeper->set($path, $value);
     
    }
     
    }
     
    /**
     
     * Equivalent of "mkdir -p" on ZooKeeper
     
     *
     
     * @param string $path The path to the node
     
     * @param string $value The value to assign to each new node along the path
     
     *
     
     * @return bool
     
     */
     
    public function makePath($path, $value = '') {
     
    $parts = explode('/', $path);
     
    $parts = array_filter($parts);
     
    $subpath = '';
     
    while (count($parts) > 1) {
     
    $subpath .= '/' . array_shift($parts);
     
    if (!$this->zookeeper->exists($subpath)) {
     
    $this->makeNode($subpath, $value);
     
    }
     
    }
     
    }
     
    /**
     
     * Create a node on ZooKeeper at the given path
     
     *
     
     * @param string $path The path to the node
     
     * @param string $value The value to assign to the new node
     
     * @param array $params Optional parameters for the Zookeeper node.
     
     * By default, a public node is created
     
     *
     
     * @return string the path to the newly created node or null on failure
     
     */
     
    public function makeNode($path, $value, array $params = array()) {
     
    if (empty($params)) {
     
    $params = array(
     
    array(
     
    'perms' => Zookeeper::PERM_ALL,
     
    'scheme' => 'world',
     
    'id' => 'anyone',
     
    )
     
    );
     
    }
     
    return $this->zookeeper->create($path, $value, $params);
     
    }
     
    /**
     
     * Get the value for the node
     
     *
     
     * @param string $path the path to the node
     
     *
     
     * @return string|null
     
     */
     
    public function get($path) {
     
    if (!$this->zookeeper->exists($path)) {
     
    return null;
     
    }
     
    return $this->zookeeper->get($path);
     
    }
     
    /**
     
     * List the children of the given path, i.e. the name of the directories
     
     * within the current node, if any
     
     *
     
     * @param string $path the path to the node
     
     *
     
     * @return array the subpaths within the given node
     
     */
     
    public function getChildren($path) {
     
    if (strlen($path) > 1 && preg_match('@/$@', $path)) {
     
    // remove trailing /
     
    $path = substr($path, 0, -1);
     
    }
     
    return $this->zookeeper->getChildren($path);
     
    }
     
     
     
    /**
     
     * Delete the node if it does not have any children
     
     * 
     
     * @param string $path the path to the node
     
     * 
     
     * @return true if node is deleted else null
     
     */
     
     
     
    public function deleteNode($path)
     
    {
     
    if(!$this->zookeeper->exists($path))
     
    {
     
    return null;
     
    }
     
    else
     
    {
     
    return $this->zookeeper->delete($path);
     
    }
     
    }
     
     
     
    /**
     
     * Wath a given path
     
     * @param string $path the path to node
     
     * @param callable $callback callback function
     
     * @return string|null
     
     */
     
    public function watch($path, $callback)
     
    {
     
    if (!is_callable($callback)) {
     
    return null;
     
    }
     
     
     
    if ($this->zookeeper->exists($path)) {
     
    if (!isset($this->callback[$path])) {
     
    $this->callback[$path] = array();
     
    }
     
    if (!in_array($callback, $this->callback[$path])) {
     
    $this->callback[$path][] = $callback;
     
    return $this->zookeeper->get($path, array($this, 'watchCallback'));
     
    }
     
    }
     
    }
     
     
     
    /**
     
     * Wath event callback warper
     
     * @param int $event_type
     
     * @param int $stat
     
     * @param string $path
     
     * @return the return of the callback or null
     
     */
     
    public function watchCallback($event_type, $stat, $path)
     
    {
     
    if (!isset($this->callback[$path])) {
     
    return null;
     
    }
     
     
     
    foreach ($this->callback[$path] as $callback) {
     
    $this->zookeeper->get($path, array($this, 'watchCallback'));
     
    return call_user_func($callback);
     
    }
     
    }
     
     
     
    /**
     
     * Delete watch callback on a node, delete all callback when $callback is null
     
     * @param string $path
     
     * @param callable $callback
     
     * @return boolean|NULL
     
     */
     
    public function cancelWatch($path, $callback = null)
     
    {
     
    if (isset($this->callback[$path])) {
     
    if (empty($callback)) {
     
    unset($this->callback[$path]);
     
    $this->zookeeper->get($path); //reset the callback
     
    return true;
     
    } else {
     
    $key = array_search($callback, $this->callback[$path]);
     
    if ($key !== false) {
     
    unset($this->callback[$path][$key]);
     
    return true;
     
    } else {
     
    return null;
     
    }
     
    }
     
    } else {
     
    return null;
     
    }
     
    }
     
    }
     
    $zk = new Zookeeper_Example('localhost:2181');
     
    // var_dump($zk->get('/'));
     
    // var_dump($zk->getChildren('/'));
     
    // var_dump($zk->set('/test', 'abc'));
     
    // var_dump($zk->get('/test'));
     
    // var_dump($zk->getChildren('/'));
     
    // var_dump($zk->set('/foo/001', 'bar1'));
     
    // var_dump($zk->set('/foo/002', 'bar2'));
     
    // var_dump($zk->get('/'));
     
    // var_dump($zk->getChildren('/'));
     
    // var_dump($zk->getChildren('/foo'));
     
     
     
    //watch example 一旦/test節(jié)點(diǎn)的值被改變,就會(huì)調(diào)用一次callback
     
    function callback() {
     
    echo "in watch callback\n";
     
    }
     
    //$zk->set('/test', 1);
     
    $ret = $zk->watch('/test', 'callback'); 
     
    //$zk->set('/test', 2);//在終端執(zhí)行
     
    while (true) {
     
    sleep(1);
     
    }
     
     
     
    /*
     
    class ZookeeperDemo extends Zookeeper {
     
     
     
     public function watcher( $i, $type, $key ) {
     
     echo "Insider Watcher\n";
     
     
     
     // Watcher gets consumed so we need to set a new one
     
     
     
    //ZooKeeper提供了可以綁定在znode的監(jiān)視器。如果監(jiān)視器發(fā)現(xiàn)znode發(fā)生變化,該service會(huì)立即通知所有相關(guān)的客戶端。這就是PH//P腳本如何知道變化的。Zookeeper::get方法的第二個(gè)參數(shù)是回調(diào)函數(shù)。當(dāng)觸發(fā)事件時(shí),監(jiān)視器會(huì)被消費(fèi)掉,所以我們需要在回調(diào)函
     
    //數(shù)中再次設(shè)置監(jiān)視器。
     
     
     
     $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);
    }
    */
    /*
    
    // $zc = new Zookeeper();
     
    // $zc->connect('127.0.0.1:2181');
    // var_dump($zc->get('/zookeeper'));
    // exit;
    */
     
    ?>

到此,關(guān)于“怎么安裝php的zookeeper擴(kuò)展”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

本文名稱:怎么安裝php的zookeeper擴(kuò)展
網(wǎng)頁(yè)路徑:http://muchs.cn/article32/gedssc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、用戶體驗(yàn)、靜態(tài)網(wǎng)站、微信公眾號(hào)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)