Gearman是一个用来把工作委派给其他机器、分布式的调用更适合做某项工作的机器、并发的做某项工作在多个调用间做负载均衡、或用来在调用其它语言的函数的系统。
编译安装 gearmand
cd gearmand-1.1.12
[root@AY1403121111165237f2Z gearmand-1.1.12]# ls
aclocal.m4 build-aux configure.ac gear_config.in libgearman-1.0 libhashkit-1.0 Makefile.am README tests
AUTHORS ChangeLog COPYING gearmand libgearmancore libhostile Makefile.in rpm THANKS
benchmark configmake.h docs HACKING libgearman-server libtest man scripts util
bin configure examples libgearman libhashkit m4 NEWS support version.m4
[root@AY1403121111165237f2Z gearmand-1.1.12]# ./configure
configure: error: could not find boost 错误解决
[root@AY1403121111165237f2Z gearmand-1.1.12]#yum install boost-devel*
configure: error: could not find gperf 错误解决
[root@AY1403121111165237f2Z gearmand-1.1.12]#yum install gperf*
configure: error: Unable to find libevent 错误解决
[root@AY1403121111165237f2Z gearmand-1.1.12]#yum install libevent-devel*
configure: error: Unable to find libuuid 错误解决
[root@AY1403121111165237f2Z gearmand-1.1.12]#yum install libuuid-devel
上面错误的原因是编译器找不到 头文件
[root@AY1403121111165237f2Z gearmand-1.1.12]#make
[root@AY1403121111165237f2Z gearmand-1.1.12]#make install
测试
[root@AY1403121111165237f2Z /]# gearman -w -f wc -- wc -l
[root@AY1403121111165237f2Z /]# gearman -f wc < /etc/passwd
运行过程
一个Gearman请求的处理过程涉及三个角色:Client -> Job -> Worker。
Client:请求的发起者,可以是 C,PHP,Perl,MySQL UDF 等等。
Job:请求的调度者,用来负责协调把 Client 发出的请求转发给合适的 Work。
Worker:请求的处理者,可以是 C,PHP,Perl 等等。
因为 Client,Worker 并不限制用一样的语言,所以有利于多语言多系统之间的集成。
我们可以通过增加更多的 Worker,可以很方便的实现应用程序的分布式负载均衡架构。
Gearman PHP扩展安装
[root@AY1403121111165237f2Z gearman-1.1.2]# /www/wdlinux/php/bin/phpize
[root@AY1403121111165237f2Z gearman-1.1.2]# ./configure --with-php-config=/www/wdlinux/php/bin/php-config
[root@AY1403121111165237f2Z gearman-1.1.2]# make
[root@AY1403121111165237f2Z gearman-1.1.2]# make install
修改PHP的配置文件 重新启动 Apache
测试Gearman PHP扩展
work.php
<?php
$worker= new GearmanWorker();
$worker->addServer("127.0.0.1", 4730);
$worker->addFunction("title","title_function");
while ($worker->work());
function title_function($job)
{
$str = $job->workload();
return strlen($str);
}
?>
-----------------------------------------------
client.php
<?php
$client= new GearmanClient();
$client->addServer("127.0.0.1", 4730);
print $client->do("title","Linvo");
?>
祝大家玩的开心
作者 范遥 [@buddy-L]