一.简介
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
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);
}
本文转自:程序员鼓励师