跨語言、支援多 Worker 實現分散式負載均衡。
wget https://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz
tar zxvf libevent-1.4.12-stable.tar.gz
cd libevent-1.4.12-stable
./configure --prefix=/usr
make && make install
/sbin/ldconfig
cd ..
wget https://launchpad.net/gearmand/trunk/0.9/+download/gearmand-0.9.tar.gz
tar zxvf gearmand-0.9.tar.gz
cd gearmand-0.9
./configure
make && make install
/sbin/ldconfig
cd ..
wget https://pecl.php.net/get/gearman-0.5.0.tgz
tar zxvf gearman-0.5.0.tgz
cd gearman-0.5.0
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-gearman
make && make install
cd ..
extension = "gearman.so"
gearmand -d # 一般用戶
gearmand -d -u root # root 用戶
# 預設監聽 4730 port
ps aux | grep gearman # 驗證進程
<?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 /var/www/html/worker.php & # 背景執行
<?php
$client = new GearmanClient();
$client->addServer('127.0.0.1', 4730);
echo $client->do('reverse', 'Hello World!'), "\n";
?>
php /var/www/html/client.php
# 輸出:!dlroW olleH
$client->doBackground('sendEmail', serialize($emailData)):非阻塞分發,Client 立即繼續。
gearmand -L 192.168.0.1 -p 4730 -u root -d
# Worker
gearman -w -f wc -- wc -l &
# Client
gearman -f wc < /etc/passwd