下面的内容基本都是参考Gearman的官方文档。详情请见gearman.org
什么是Gearman
Gearman是一个分布式任务调度框架,最早在 livejournal.com 用于分布式文件存储系统、MogileFS的协调机制等。“gearman”这个名字的来历也挺有意思。之所以叫“gearman”,是因为它是“manager”被颠倒字母顺序后组成的一个单词。为什么在manager颠倒字母后的多个单词中单单选中“gearman”,我想是因为“gear”有传动装置、协调的意思,“man”就是一个人,加一起就是一个协调者,传动者。Gearman就像一个manager,自己不做具体工作,而是分发任务给下属。
下面截取一段gearman官方网站给出的介绍:
Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages. It can be used in a variety of applications, from high-availability web sites to the transport of database replication events. In other words, it is the nervous system for how distributed processing communicates.
翻译过来就是:Gearman是一个通用的应用框架,它可以把任务分发到专门处理这些任务的节点上(处理任务的进程或机器)。它可以并行的处理任务、处理任务时做负载均衡,并且支持多语言之间的函数调用。可以用于从高可用web站点到数据库备份传输等一系列的应用场景中。它相当于一个神经系统,用来决定分布式处理程序之间的通信细节。
Gearman是如何工作的
Gearman总共分为三个部分:
- client:产生task,并把task提交给job server
- job server:接收client提交的task,找到相应的worker,并把task交给worker执行,把worker返回的结果返回给client
- worker:接收来自job server分发的task,执行,返回结果给job server

下面是一个例子:通过gearman实现字符串反转功能。
客户端代码实现:1
2
3
4
5
6//初始化客户端
$client = new GearmanClient();
//绑定job server,这里job server是本机
$client->addServer('127.0.0.1', '4730');
//客户端发送调用请求,函数是reverse,参数是"hello world"字符串。
print $client->do('reverse', 'hello world');
这里客户端会把调用请求发送给job server,发送时,会把请求的数据(包括函数名和参数)按照gearman的包协议打包,然后再发送给job server。函数名和参数对于gearman可以是任意的。
worker代码实现:1
2
3
4
5
6
7
8
9
10//初始化worker
$worker = new GearmanWorker();
//绑定job server,这里job server是本机(注册worker,job server会记录worker来源)
$worker->addServer('127.0.0.1', '4730');
//定义一个反转字符串的函数,并注册名为reverse
//job server会记录这个worker注册了reverse函数
$worker->addFunction("reverse", function($job) {
return strrev($job->workload());
});
while($worker->work()); //等待client的请求
把worker代码运行起来,客户端发送请求后会输出”hello world”.

Gearman的使用方式
- 多语言支持:you can use Gearman as an interface between a client and a worker written in different languages.
- 负载均衡、部署方式:put the worker code on a separate machine (or cluster of machines) that are better suited to do the work. 比如收到图片处理的请求后,可以在单独的机器上做图片处理工作。 you also get a natural form of load balancing since the job server only sends new jobs to idle workers. If all the workers running on a given machine are busy, you don’t need to worry about new jobs being sent there.
- 可扩展性:worker机器可伸缩。It is also seamless to add new machines to expand your worker pool, just boot them up, install the worker code, and have them connect to the existing job server.
- 容错性,避免单点故障。what if the job server dies? You are able to run multiple job servers and have the clients and workers connect to the first available job server they are configured with. This way if one job server dies, clients and workers automatically fail over to another job server. You probably don’t want to run too many job servers, but having two or three is a good idea for redundancy.

Gearman的安装与管理
Job Server
首先我们安装Job Server。去官方网站下载稳定tar包,然后编译安装。
tar xzf gearmand-X.Y.tar.gz
cd gearmand-X.Y
./configure
make
make install
启动Job Server:
gearmand -d
Client和Worker API
Gearmand启动后,接下来就是选择client和worker api。文档中介绍了两种方式:一种是命令行工具,另一种是php扩展。
首先我们看下命令行工具。
Command Line Tool
在安装gearman的Job Server时,会包含gearman的命令行工具。我们可以启动一个gearman worker。
gearman -w -f wc -- wc -l
gearman -w 表示以worker方式执行,-f表示函数。这里,worker将会连接本地(localhost)的job server,并向job server注册一个名为 wc 的函数(函数的具体实现就是 wc -l )。当当worker收到job server转发过来的job时,就开始执行 wc -l 命令。
gearman -f wc < /etc/passwd
27
gearman去掉 -w选项后,就表示以client方式执行。这里,client会提交一个job到本地(localhost)的job server,job就是执行wc函数,传入的参数就是/etc/passwd。最终就相当于执行:
wc -l /etc/passwd
上述的command line tool的例子中,job server、client、worker都是在同一台机器上运行。它们也可以分散到不同的机器上。Command Line Tool在分布式领域应用 很广泛,比如快速原型搭建、分布式集群管理工具、分发shell脚本到不同机器上做处理(比如日志分析)等等。
PHP扩展
在 PECL上下载gearman的php扩展包。然后安装:
tar xzf gearman-X.Y.tgz
cd gearman-X.Y
phpize
./configure
make
make install
安装完成后,把gearman.so添加到php.ini中
extension=gearman.so
测试一下gearman的php接口是否正常
<?php
print gearman_version() . "\n";
?>
运行:
/usr/local/php/bin/php test.php
1.1.12
下面是一个基于gearman php扩展的例子。
worker实现的功能:接收一个字符串,返回这个字符串的倒序结果。
worker.php
<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("reverse", "my_reverse_function");
//一直会等待job server分发过来的task,一旦有任务过来,就开始执行my_reverse_function
while ($worker->work());
function my_reverse_function($job) {
return strrev($job->workload());
}
?>
启动worker
/usr/local/php/bin/php worker.php
client实现功能:向job server提交task,调用reverse函数,参数为“hello world”.
client.php
<?php
$client= new GearmanClient();
$client->addServer();
print $client->do("reverse", "Hello World!");
?>
运行client
/usr/local/php/bin/php client.php
!dlroW olleH
最后
Gearman官方网站上面有更详细的说明和应用实例。