1,gearmand是什么?
Gearman是一个用来把工作委派给其他机器、分布式的调用更适合做某项工作的机器、并发的做某项工作在多个调用间做负载均衡、或用来在调用其它语言的函数的系统
2,gearmand应用场景
异步处理:图片处理,订单处理,批量邮件/通知之类的
要求高CPU或内存的处理:大容量的数据处理,MapReduce运算,日志聚集,视频编码
分布式和并行的处理 定时处理:增量更新,数据复制
限制速率的FIFO处理
分布式的系统监控任务
3,gearmand的组成部分以及工作原理
一个client 一个worker 一个任务服务器
Client的作用是提出一个 Job 任务 交给 Job Server 任务服务器。Job Server 会去寻找一个 合适的 Worker 来完成这项任务。Worker 执行由 Client 发送过来的 Job,并且将结果通过 Job Server 返回给 Client。Gearman 提供了 Client 和 Worker 的 API,利用这些API 应用可以同 Gearman Job Server来进行通信。Gearman 内部 Client 和 Worker 之间的通信都是通过 TCP 连接来进行的。
示例与代码分析
代码来自官方的代码
reverse_worker.php
<?php
echo "Starting\n";
# Create our worker object.
$gmworker= new GearmanWorker();//实例化一个Worker实例
# Add default server (localhost).
$gmworker->addServer();//给work增加一个Job Sever 参数为host跟port
# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("reverse", "reverse_fn");
//addFunction:给worker注册方法,4个参数,第一个为函数,第二个为回调函数,第三个为返回值,第四个为timeout
print "Waiting for job...\n";
while($gmworker->work())
{
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
//returnCode返回最后一个Gearman返回码
{
echo "return_code: " . $gmworker->returnCode() . "\n";
break;
}
}
function reverse_fn($job)
{
echo "Received job: " . $job->handle() . "\n";
$workload = $job->workload();
$workload_size = $job->workloadSize();
echo "Workload: $workload ($workload_size)\n";
# This status loop is not needed, just showing how it works
for ($x= 0; $x < $workload_size; $x++)
{
echo "Sending status: " . ($x + 1) . "/$workload_size complete\n";
$job->sendStatus($x, $workload_size);
sleep(1);
}
$result= strrev($workload);
echo "Result: $result\n";
# Return what we want to send back to the client.
return $result;
}
# A much simpler and less verbose version of the above function would be:
function reverse_fn_fast($job)
{
return strrev($job->workload());
}
?>
reverse_client.php
<?php
# Create our client object.
$gmclient= new GearmanClient();
//实例化一个Gearman客户端
# Add default server (localhost).
$gmclient->addServer();
//增加一个job server
echo "Sending job\n";
# Send reverse job
do
{
$result = $gmclient->do("reverse", "Hello!");
# Check for various return packets and errors.
switch($gmclient->returnCode())
{
case GEARMAN_WORK_DATA:
echo "Data: $result\n";
break;
case GEARMAN_WORK_STATUS:
list($numerator, $denominator)= $gmclient->doStatus();
echo "Status: $numerator/$denominator complete\n";
break;
case GEARMAN_WORK_FAIL:
echo "Failed\n";
exit;
case GEARMAN_SUCCESS:
echo "Success: $result\n";
break;
default:
echo "RET: " . $gmclient->returnCode() . "\n";
exit;
}
}
while($gmclient->returnCode() != GEARMAN_SUCCESS);
?>
疑问
1如果client server跟worker server不在同一个服务器上,会发生什么情况。根本就找不到对方吗?推论:他们的server指的是同一个server。client的server只是一个列表而不进行I/O
对于client server 官方解释如下:
Adds a job server to a list of servers that can be used to run a task. No socket I/O happens here; the server is simply added to the list.
2.工作流程
运行worker worker会处于运行状态,运行client client会将job传给job server ,job server将job传给 worker,worker会执行相关的job