Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Gearman 實現分散式計算入門:安裝、Worker/Client 開發與命令列驗證

編輯:Linux 教學

核心概念

Gearman 以 Client → Job Server → Worker 三角色解決跨語言系統整合與分散式負載均衡,輕量且無需 WebService 複雜協定。

安裝流程(單機驗證)

Gearman Server & Library


wget https://launchpad.net/gearmand/trunk/0.8/+download/gearmand-0.8.tar.gz
tar zxf gearmand-0.8.tar.gz
cd gearmand-0.8
./configure && make && make install

PHP Extension


wget https://pecl.php.net/get/gearman-0.4.0.tgz
tar zxf gearman-0.4.0.tgz
cd gearman-0.4.0
phpize && ./configure && make && make install
echo 'extension="gearman.so"' >> /etc/php.ini

啟動 Job Server


gearmand -d           # 一般用戶
gearmand -d -u root   # root
# 監聽 4730 port

Worker 開發 (PHP)


<?php
$worker = new GearmanWorker();
$worker->addServer('127.0.0.1', 4730);
$worker->addFunction('reverse', 'my_reverse_function');
while ($worker->work());

function my_reverse_function($job) {
    return strrev($job->workload());
}
?>

php worker.php &

Client 開發 (PHP)


<?php
$client = new GearmanClient();
$client->addServer('127.0.0.1', 4730);
echo $client->do('reverse', 'Hello World!'), "\n";
?>

php client.php
# 輸出:!dlroW olleH

擴展應用

  • 跨語言:Worker 可用 C/Perl/Python 實作,Client 不變。
  • 負載均衡:啟動多個 Worker 進程,Job Server 自動分發。
  • 非同步:doBackground() 火即不管。

命令列工具零依賴驗證


# 啟動 Worker(執行 wc -l)
gearman -w -f wc -- wc -l &
# 送入資料
gearman -f wc < /etc/passwd

官方資源

詳細 API 請參考 Gearman 官方文件與 PDF 指南。

Copyright © Windows教程網 All Rights Reserved