Gearman实践

Gearman实践

作者:admin |  时间:2015-04-02 |  浏览:201 |  0 条评论

architecture

有三台Centos服务器,192.168.48.152,192.168.48.153,192.168.48.154

首先在三台服务器上都安装了gearman,php gearman扩展
在192.168.48.152上/home/jishipu/gearmantest目录下部署程序:
client.php
<?php
$dips = new GearmanClient();
$dips->addServer('192.168.48.153', '4730');
$dips->addServer('192.168.48.154', '4730');
$data['pSN'] = "1234567890";
$data['pData'] = "192.168.48.152";
$image = $dips->doNormal('ecard', serialize($data));
echo $image;
?>
worker.php
<?php
function make_ecard_function($job) {
   $data = unserialize($job->workload());
   $pSN = $data['pSN'];
   $pData = $data['pData'];
   $fileName = "/tmp/192.168.48.152_".$pData."_".rand().".txt";
   file_put_contents($fileName, $pSN."_".$pData);
   sleep(30);
   return $fileName;
}
$worker = new GearmanWorker();
$worker->addServer('192.168.48.153', 4730);
$worker->addServer('192.168.48.154', 4730);
$worker->addFunction('ecard', 'make_ecard_function');
while($worker->work());
?>
auto.sh
#!/bin/bash
for((i=0;i<300;i++))
do
    /data/webserver/php/bin/php /home/jishipu/gearmantest/client.php &
done
在192.168.48.153上/home/jishipu/gearmantest目录下部署程序:
worker.php
<?php
function make_ecard_function($job) {
   $data = unserialize($job->workload());
   $pSN = $data['pSN'];
   $pData = $data['pData'];
   $fileName = "/tmp/192.168.48.153_".$pData."_".rand().".txt";
   file_put_contents($fileName, $pSN."_".$pData);
   sleep(30);
   return $fileName;
}
$worker = new GearmanWorker();
$worker->addServer('192.168.48.153', 4730);
$worker->addServer('192.168.48.154', 4730);
$worker->addFunction('ecard', 'make_ecard_function');
while($worker->work());
?>
在192.168.48.154上/home/jishipu/gearmantest目录下部署程序:
worker.php
<?php
function make_ecard_function($job) {
   $data = unserialize($job->workload());
   $pSN = $data['pSN'];
   $pData = $data['pData'];
   $fileName = "/tmp/192.168.48.154_".$pData."_".rand().".txt";
   file_put_contents($fileName, $pSN."_".$pData);
   sleep(30);
   return $fileName;
}
$worker = new GearmanWorker();
$worker->addServer('192.168.48.153', 4730);
$worker->addServer('192.168.48.154', 4730);
$worker->addFunction('ecard', 'make_ecard_function');
while($worker->work());
?>
===============================================
在192.168.48.153,192.168.48.154上执行
gearmand -d -p 4730
在192.168.48.152,192.168.48.153,192.168.48.154上分别执行三次
/data/webserver/php/bin/php worker.php  &
最后在192.168.48.152上执行:
./auto.sh
最后可以在三台服务器上的/tmp目录看到均匀的执行结果。


gearman,php gearman扩展安装

首先下载gearmand-1.1.9.tar.gz(gearman主程序),gearman-1.1.1.tgz(PHP扩展)
tar zxvf gearmand-1.1.9.tar.gz
./configure
//如果报缺少boost 执行yum install boost boost-devel
//如果报缺少gperf  执行yum install gperf
//如果报缺少libevent  执行yum install libevent libevent-devel
make 
make install
tar zxvf gearman-1.1.1.tgz
/data/webserver/php/bin/phpize
./configure  --with-php-config=/data/webserver/php/bin/php-config  --with-gearman
make 
make install
vi  /data/webserver/php/etc/php.ini添加
extension = "gearman.so"

本文标签:

发表评论

电子邮件地址不会被公开。

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>