How to install the zookeeper extension in php: first download zookeeper; then specify the installation directory; finally install zookeeper through "make && make install".
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.
ZooKeeper is a distributed, open source distributed application coordination service. It is an open source implementation of Google's Chubby and an important component of Hadoop and Hbase. It is a software that provides consistent services for distributed applications. The functions provided include: configuration maintenance, domain name services, distributed synchronization, group services, etc.
The goal of ZooKeeper is to encapsulate complex and error-prone key services and provide users with simple and easy-to-use interfaces and a system with efficient performance and stable functions.
To use zookeeper in php, you must first install the php zookeeper extension. To install the php zookeeper extension, you must first install zookeeper
1. Install zookeeper
Download here The latest stable version
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 //This is a tool that has been installed. Now we need to compile and install it ourselves, because it will be used when installing PHP extensions later.
tar -zxvf zookeeper-3.4.12.tar.gz
cd zookeeper-3.4.12/src/c/
./configure --prefix=/usr /local/zookeeper //Specify the installation directory
make && make install
The installation is complete
2. Install the php zookeeper extension at http://pecl. Find in 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 //Specify dependencies
make && make install
Configure php.ini
extension="/usr/local/Cellar/php/7.2.6/pecl/20170718/zookeeper.so"
Restart php-fpm.
[Recommendation: "PHP Video Tutorial"]
3. Install jdk before starting zookeeper. If you have already installed it, you can ignore it
at http:/ Download
in /www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html and then install it like a fool, I won’t go into details here
4. Start zookeeper
cd /download/zookeeper-3.4.12/bin
./zkServer.sh start
./zkCli.sh -server 127.0.0.1:2181
Open in cli mode
Note:
If an error is reported:
cd ../conf
cp zoo_sample.cfg zoo.cfg
Copy the file
5, PHP code test
Test Code
<?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)) { //创建节点 $this->makePath($path); } else { $this->zookeeper->set($path,$value); } } /* * 创建路径 */ private 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); } } } /* * 创建节点 * */ 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")); ?> 测试代码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节点的值被改变,就会调用一次callback function callback() { echo "in watch callback\n"; } //$zk->set('/test', 1); $ret = $zk->watch('/test', 'callback'); //$zk->set('/test', 2);//在终端执行 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的监视器。如果监视器发现znode发生变化,该service会立即通知所有相关的客户端。这就是PH//P脚本如何知道变化的。Zookeeper::get方法的第二个参数是回调函数。当触发事件时,监视器会被消费掉,所以我们需要在回调函 //数中再次设置监视器。 $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; */ ?>
Code reference link:
https://github.com/php-zookeeper/php-zookeeper/blob/master/ examples/Zookeeper_Example.php
The above is the detailed content of How to install the zookeeper extension for php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment