Gearman的介紹php
主頁在http://gearman.org/index.php, 它的主要優勢有:服務器
Gearman的安裝配置負載均衡
1.安裝Gearman server and library:異步
wget http://launchpad.net/gearmand/tr ... gearmand-0.8.tar.gz tar zxf gearmand-0.8.tar.gz cd gearmand-0.8 ./configure make make install分佈式
2.安裝Gearman PHP extension:函數
wget http://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性能
3.編輯php.ini配置文件加載相應模塊並使之生效:.net
extension = "gearman.so"調試
4.啓動Job:server
gearmand -d
若是當前用戶是root的話,則須要這樣操做:
gearmand -d -u root
缺省會使用4730端口,下面會用到。
以調試的方式啓動:
gearmand -vv
5.編寫Worker:
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()); } ?>
設置後臺運行work:
php worker.php &
6.編寫Client:
client.php文件內容以下:
<?php $client= new GearmanClient(); $client->addServer('127.0.0.1', 4730); echo $client->do('reverse', 'Hello World!'), "/n"; ?>
運行client:
php client.php
輸出:!dlroW olleH