検索
ホームページバックエンド開発PHPの問題PHP 用の Zookeeper 拡張機能をインストールする方法

Zookeeper 拡張機能を PHP でインストールする方法: 最初に Zookeeper をダウンロードし、次にインストール ディレクトリを指定し、最後に「make && make install」を通じて Zookeeper をインストールします。

PHP 用の Zookeeper 拡張機能をインストールする方法

#この記事の動作環境: Windows7 システム、PHP7.1、Dell G3 コンピューター。

ZooKeeper は、オープンソースの分散型アプリケーション調整サービスであり、Google の Chubby のオープンソース実装であり、Hadoop および Hbase の重要なコンポーネントです。分散アプリケーションに一貫したサービスを提供するソフトウェアであり、構成保守、ドメイン名サービス、分散同期、グループ サービスなどの機能が提供されます。

ZooKeeper の目標は、複雑でエラーが発生しやすい主要なサービスをカプセル化し、シンプルで使いやすいインターフェイスと、効率的なパフォーマンスと安定した機能を備えたシステムをユーザーに提供することです。

PHP で Zookeeper を使用するには、まず php Zookeeper 拡張機能をインストールする必要があります。PHP Zookeeper 拡張機能をインストールするには、まず Zookeeper をインストールする必要があります

1. Zookeeper のインストール

ダウンロード最新の安定バージョン

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 //これはインストールされているツールです。インストール時に使用されるため、自分でコンパイルしてインストールする必要がありますPHP 拡張機能は後で説明します。 prefix=/usr /local/zookeeper //インストール ディレクトリを指定します

make && make install

インストールが完了しました

2. http: にある php Zookeeper 拡張機能をインストールします。 //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

Configure php.ini

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

php-fpmを再起動します。

#[推奨事項: "

PHP ビデオ チュートリアル

"]

3. Zookeeper を起動する前に jdk をインストールします。すでにインストールされている場合は無視してください

http:/ で /www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html の

をダウンロードし、次のようにインストールします。愚か者、ここでは詳細には触れません

4. Zookeeper を起動します

cd /download/zookeeper-3.4.12/bin

./zkServer.sh start

./zkCli.sh -server 127.0.0.1:2181

cli モードで開く

注:

エラーが報告された場合:

cd ../conf

cpzoo_sample.cfgzoo.cfg

ファイルをコピーします

5、PHP コード テスト

テスト コード

<?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(&#39;@/$@&#39;, $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=&#39;&#39;)
 
{
 
$parts = explode(&#39;/&#39;, $path);
 
$parts = array_filter($parts);//过滤空值
 
$subPath = &#39;&#39;;
 
while (count($parts) > 1) {
 
$subPath .= &#39;/&#39; . array_shift($parts);//数组第一个元素弹出数组
 
if (!$this->zookeeper->exists($subpath)) {
 
$this->makeNode($subPath, $value);
 
}
 
}
 
}
 
/*
 
 * 创建节点
 
 *
 
 */
 
private function makeNode($path, $value, array $params = array())
 
{
 
if (empty($params)) {
 
$params = [
 
[
 
&#39;perms&#39; => Zookeeper::PERM_ALL,
 
&#39;scheme&#39; => &#39;world&#39;,
 
&#39;id&#39; => &#39;anyone&#39;
 
]
 
];
 
}
 
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(&#39;localhost:2181&#39;);
 
//var_dump($zk->get(&#39;/zookeeper&#39;));
 
var_dump($zk->getChildren(&#39;/foo&#39;));
 
//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&#39;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 = &#39;&#39;) {
 
$parts = explode(&#39;/&#39;, $path);
 
$parts = array_filter($parts);
 
$subpath = &#39;&#39;;
 
while (count($parts) > 1) {
 
$subpath .= &#39;/&#39; . 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(
 
&#39;perms&#39; => Zookeeper::PERM_ALL,
 
&#39;scheme&#39; => &#39;world&#39;,
 
&#39;id&#39; => &#39;anyone&#39;,
 
)
 
);
 
}
 
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(&#39;@/$@&#39;, $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, &#39;watchCallback&#39;));
 
}
 
}
 
}
 
 
 
/**
 
 * 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, &#39;watchCallback&#39;));
 
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(&#39;localhost:2181&#39;);
 
// var_dump($zk->get(&#39;/&#39;));
 
// var_dump($zk->getChildren(&#39;/&#39;));
 
// var_dump($zk->set(&#39;/test&#39;, &#39;abc&#39;));
 
// var_dump($zk->get(&#39;/test&#39;));
 
// var_dump($zk->getChildren(&#39;/&#39;));
 
// var_dump($zk->set(&#39;/foo/001&#39;, &#39;bar1&#39;));
 
// var_dump($zk->set(&#39;/foo/002&#39;, &#39;bar2&#39;));
 
// var_dump($zk->get(&#39;/&#39;));
 
// var_dump($zk->getChildren(&#39;/&#39;));
 
// var_dump($zk->getChildren(&#39;/foo&#39;));
 
 
 
//watch example 一旦/test节点的值被改变,就会调用一次callback
 
function callback() {
 
echo "in watch callback\n";
 
}
 
//$zk->set(&#39;/test&#39;, 1);
 
$ret = $zk->watch(&#39;/test&#39;, &#39;callback&#39;); 
 
//$zk->set(&#39;/test&#39;, 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( &#39;/test&#39;, array($this, &#39;watcher&#39; ) );
 
 
 
 }
 
 
 
}

$zoo = new ZookeeperDemo(&#39;127.0.0.1:2181&#39;);
$zoo->get( &#39;/test&#39;, array($zoo, &#39;watcher&#39; ) );
while( true ) {
 echo &#39;.&#39;;
 sleep(2);
}
*/
/*

// $zc = new Zookeeper();
 
// $zc->connect(&#39;127.0.0.1:2181&#39;);
// var_dump($zc->get(&#39;/zookeeper&#39;));
// exit;
*/
 
?>

コード参照リンク:

https:/ /github.com/php-zookeeper/php-zookeeper/blob/master/examples/Zookeeper_Example.php

以上がPHP 用の Zookeeper 拡張機能をインストールする方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
酸とベースデータベース:違いとそれぞれを使用するタイミング。酸とベースデータベース:違いとそれぞれを使用するタイミング。Mar 26, 2025 pm 04:19 PM

この記事では、酸とベースのデータベースモデルを比較し、その特性と適切なユースケースを詳述しています。酸は、財務およびeコマースアプリケーションに適したデータの整合性と一貫性を優先し、ベースは可用性に焦点を当て、

PHPセキュアファイルアップロード:ファイル関連の脆弱性の防止。PHPセキュアファイルアップロード:ファイル関連の脆弱性の防止。Mar 26, 2025 pm 04:18 PM

この記事では、コードインジェクションのような脆弱性を防ぐために、PHPファイルのアップロードを確保することについて説明します。ファイルタイプの検証、セキュアストレージ、およびアプリケーションセキュリティを強化するエラー処理に焦点を当てています。

PHP入力検証:ベストプラクティス。PHP入力検証:ベストプラクティス。Mar 26, 2025 pm 04:17 PM

記事では、組み込み関数、ホワイトリストアプローチ、サーバー側の検証などの手法に焦点を当てたセキュリティを強化するためのPHP入力検証のベストプラクティスについて説明します。

PHP APIレート制限:実装戦略。PHP APIレート制限:実装戦略。Mar 26, 2025 pm 04:16 PM

この記事では、Token BucketやLeaky BucketなどのアルゴリズムやSymfony/Rate-Limiterなどのライブラリを使用するなど、PHPでAPIレート制限を実装するための戦略について説明します。また、監視、動的に調整されたレートの制限、および手をカバーします

PHPパスワードハッシュ:password_hashおよびpassword_verify。PHPパスワードハッシュ:password_hashおよびpassword_verify。Mar 26, 2025 pm 04:15 PM

この記事では、パスワードを保護するためにPHPでpassword_hashとpassword_verifyを使用することの利点について説明します。主な議論は、これらの関数が自動塩の生成、強力なハッシュアルゴリズム、およびSecurを通じてパスワード保護を強化するということです

OWASPトップ10 PHP:共通の脆弱性を説明し、軽減します。OWASPトップ10 PHP:共通の脆弱性を説明し、軽減します。Mar 26, 2025 pm 04:13 PM

この記事では、PHPおよび緩和戦略におけるOWASPトップ10の脆弱性について説明します。重要な問題には、PHPアプリケーションを監視および保護するための推奨ツールを備えたインジェクション、認証の壊れ、XSSが含まれます。

PHP XSS予防:XSSから保護する方法。PHP XSS予防:XSSから保護する方法。Mar 26, 2025 pm 04:12 PM

この記事では、PHPでのXSS攻撃を防ぐための戦略について説明し、入力の消毒、出力エンコード、セキュリティを向上させるライブラリとフレームワークの使用に焦点を当てています。

PHPインターフェイスvs抽象クラス:それぞれを使用する時期。PHPインターフェイスvs抽象クラス:それぞれを使用する時期。Mar 26, 2025 pm 04:11 PM

この記事では、PHPでのインターフェイスと抽象クラスの使用について説明し、それぞれをいつ使用するかに焦点を当てています。インターフェイスは、無関係なクラスや複数の継承に適した、実装なしで契約を定義します。抽象クラスは共通の機能を提供します

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター