Gearman的使用

简介

Gearman提供了一种通用的程序框架来将你的任务分发到不同的机器或者不同的进程当中。它提供了你进行并行工作的能力、负载均衡处理的能力,以及在不同程序语言之间沟通的能力。Gearman能够应用的领域非常广泛,从高可用的网站到数据库的复制任务。总之,Gearman就是负责分发处理的中枢系统,它的优点包括

开源:Gearman免费并且开源而且有一个非常活跃的开源社区,如果你想来做一些贡献,请点击 。
多语言支持:Gearman支持的语言种类非常丰富。让我们能够用一种语言来编写Worker程序,但是用另外一种语言编写Client程序。
灵活:不必拘泥于固定的形式。您可以采用你希望的任何形式,例如 Map/Reduce。
快速:Gearman的协议非常简单,并且有一个用C语言实现的,经过优化的服务器,保证应用的负载在非常低的水平。
可植入:因为Gearman非常小巧、灵活。因此您可以将他置入到现有的任何系统中。
没有单点:Gearman不仅可以帮助扩展系统,同样可以避免系统的失败。

Gearman的工作原理

使用Gearman的应用通常有三部分组成:一个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 连接来进行的。工作的流程如下图所示

Gearman的用处

Gearman首先提供了一个多语言通讯的接口,当然还有比这个更简单有效的办法。Gearman可以将工作的负载分担到不同的机器中,如下图所示

安装 Gearman

向一个机器添加 Gearman 需要两步:第一步构建并启动这个守护程序,第二步构建与 PHP 版本相匹配的 PHP 扩展。这个守护程序包包括构建此扩展所需的所有库

Install gcc

yum install gcc-c++

 安装boost

wget https://github.com/boostorg/build/archive/2014.10.tar.gz
./bootstrap.sh
./b2 install

 安装qperf

yum install gperf

 安装libevent libevent-devel

yum -y install libevent libevent-devel

 安装libuuid

wget http://downloads.sourceforge.net/project/libuuid/libuuid-1.0.3.tar.gz
tar zxvf libuuid-1.0.3.tar.gz
./configure 
make & make install

 安装Gearmand

wget https://launchpad.net/gearmand/1.2/1.1.11/+download/gearmand-1.1.11.tar.gz
tar zxvf gearmand-1.1.11.tar.gz
cd gearmand-1.1.11
./configure
make & make install

 安装gearman的PHP扩展

wget http://pecl.php.net/get/gearman-1.1.2.tgz
phpize
./configure
make & make install

 启动 Job Server

gearmand -d

[root@localhost www]# ps -ef | grep gearmand
root     19109     1  0 01:06 ?        00:00:00 /usr/local/sbin/gearmand -d
root     22185  3160  0 03:19 pts/1    00:00:00 grep gearmand

 命令行使用

ldconfig 
[worker]
[root@localhost www] gearman -w -f wc -- wc -l

[client]
[root@localhost www] gearman -f wc < /etc/passwd
32

 PHP脚本使用

[worker.php]

<?php

$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction("strtoupper", "my_func");

while ($worker->work());

function my_func($job)
{
    return strtoupper($job->workload());
}

[client.php]

<?php

$client = new GearmanClient();
$client->addServer();
$string = $client->doNormal("strtoupper", "hello world");
echo $string;

[执行]

[root@localhost www]php worker.php

[root@localhost www]php client.php
HELLO WORLD

 Gearmand 使用 Mysql 作为队列存储

[启动]
gearmand -d -q mysql --mysql-host=localhost --mysql-port=3306 --mysql-user=root  
--mysql-password=123456 --mysql-db=test --mysql-table=gearman_queue
[查看数据表]
mysql> show tables;
+-------------------+
| Tables_in_gearman |
+-------------------+
| gearman_queue     |
+-------------------+
1 row in set (0.00 sec)

[测试]

#addTask: gearman -f testqueue -b xx00

mysql> select * from gearman_queue;
+--------------------------------------+---------------+----------+------+-------------+
| unique_key                           | function_name | priority | data | when_to_run |
+--------------------------------------+---------------+----------+------+-------------+
| 75f18bac-2f95-11e5-87c3-cb400fa24a6b | testqueue     |        1 | xx00 |           0 |
+--------------------------------------+---------------+----------+------+-------------+
1 row in set (0.00 sec)

#doJob:gearman -f testqueue -w

mysql> select * from gearman_queue;
Empty set (0.00 sec)

Gearman 的高级特性

在一个 Web 应用程序内可能有许多地方都会用到 Gearman。可以导入大量数据、发送许多电子邮件、编码视频文件、挖据数据并构建一个中央日志设施 — 所有这些均不会影响站点的体验和响应性。可以并行地处理数据。而且,由于 Gearman 协议是独立于语言和平台的,所以您可以在解决方案中混合编程语言。比如,可以用 PHP 编写一个 producer,用 C、Ruby 或其他任何支持 Gearman 库的语言编写 worker。

一 个连接客户机和 worker 的 Gearman 网络实际上可以使用任何您能想象得到的结构。很多配置能够运行多个代理并将 worker 分配到许多机器上。负载均衡是隐式的:每个可操作的可用 worker(可能是每个 worker 主机具有多个 worker)从队列中拉出作业。一个作业能够同步或异步运行并具有优先级。

Gearman 的最新版本已经将系统特性扩展到了包含持久的作业队列和用一个新协议来通过 HTTP 提交工作请求。对于前者,Gearman 工作队列保存在内存并在一个关系型数据库内存有备份。这样一来,如果 Gearman 守护程序故障,它就可以在重启后重新创建这个工作队列。另一个最新的改良通过一个 memcached 集群增加队列持久性。memcached 存储也依赖于内存,但被分散于几个机器以避免单点故障。

Gearman 是一个刚刚起步却很有实力的工作分发系统。据 Gearman 的作者 Eric Day 介绍,Yahoo! 在 60 或更多的服务器上使用 Gearman 每天处理 600 万个作业。新闻聚合器 Digg 也已构建了一个相同规模的 Gearman 网络,每天可处理 400,000 个作业。Gearman 的一个出色例子可以在 Narada 这个开源搜索引擎(参见 参考资料)中找到。

参考资料

http://www.ibm.com/developerworks/cn/opensource/os-php-gearman/

http://www.cnblogs.com/cocowool/archive/2011/08/18/2145144.html

原文地址:https://www.cnblogs.com/chenpingzhao/p/4782194.html