您好,登錄后才能下訂單哦!
一.簡介
Gearman安裝分2步,第1步是gearmand的安裝,第2步是gearman的PHP擴展的安裝。
第1步:
二.gearmand的安裝
1.首先安裝libdrizzle
#yum install libdrizzle libdrizzle-devel
2.安裝gearman(兩種方法1.yum2.源碼包)。(c版的server)
1)yum安裝
#rpm -ivh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/epel-release-6-5.noarch.rpm
#yum install -y gearmand
2)源碼包安裝
#cd /opt/build/
#wget https://launchpad.net/gearmand/trunk/0.34/+download/gearmand-0.34.tar.gz
#tar zxf gearmand-0.34.tar.gz
#cd gearmand-0.34
#./configure
#make && make install
3.啟動gearman服務
1)yum安裝方式
#/etc/init.d/gearmand start
2)源碼包安裝方式
#/opt/build/gearmand-0.34/sbin/gearmand -d
#gearmand -vvv -u root
INFO Starting up
INFO Listening on :::4730 (6)
INFO Creating wakeup pipe
INFO Creating IO thread wakeup pipe
INFO Adding event for listening socket (6)
INFO Adding event for wakeup pipe
INFO Entering main event loop
三.gearman的PHP擴展安裝
1.下載擴展程序:http://pecl.php.net/package/gearman
wget http://pecl.php.net/get/gearman-1.1.2.tgz
tar zxvf gearman-1….
cd gearman-1 …
phpize
./configure
如果提示: configure: error: Please install libgearman
則安裝:libgearman-devel,命令:yum install libgearman-devel
make && make install
很快就安裝完成,最后會展示so文件的路徑,如:/usr/lib64/php/modules/
2.在php.ini末尾加上extension=”/usr/lib64/php/modules/gearman.so”,重啟apache,輸出php –info |grep “gearman”或者php -m或者網頁輸出phpinfo()都能看到已經安裝成功。
常見問題:configure時如果提示找不到php-config,請指定。如–with-php-config=/usr/local/php/bin/php-config,注意要指定完整,不要只寫目錄。
四.gearman的啟動和測試
1.gearmand -p 4730 -L 121.111.111.111 --log-file=/tmp/gearmand-4730.log --pid-file=/tmp/gearmand-4730.pid -d
2.Client:
$myParams = array();
$myParams['level'] = "warn";
$myParams['content'] = "it's a test";
$client= new GearmanClient();
//如果gearman 300毫秒內沒反應,直接關閉連接
$client->setTimeout(300);
$client->addServer("121.111.111.111", "4730");
$client->doBackground("log", json_encode($myParams));
3.Server
$worker = new GearmanWorker();
$worker -> addServer("112.111.111.111", "4730");
$worker -> addFunction("log", "doLog");
while ($worker -> work());
function doLog($job) {
global $logger, $logLevels, $myParams;
$myParams = NULL;
$myParams = json_decode($job -> workload());
$level = $myParams->level;
if (!in_array($level,$logLevels)) $level = 'info';
$logger->$level($myParams->content);
}
參考:
http://www.cppblog.com/guojingjia2006/archive/2013/01/07/197076.html
http://www.xiaomlove.com/php安裝gearman擴展實現異步分步式任務/
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。