Gearman 是一个通用应用架构,用于分布式处理,有Perl ,C 两个版本。
官方网站 http://gearman.org/
开源 多语言支持 灵活 执行速度快 可嵌入
分布式协同处理 异构环境搭配(不同的操作系统,编程语言,硬件配置) 异步处理消息(基于内存的快速存取队列)
Gearman 架构图解
完整的Gearman 应用包含三个部分:a client , a worker , a job server
网络环境:
client IP:192.168.10.246 job server IP:192.168.10.247 worker IP:192.168.10.248
操作系统和软件:
CentOS-5.4-X86_64 gearmand-0.12.tar.gz (C server )
$ tar zxvf gearmand-0.12.tar.gz $ cd gearmand-0.12 $ ./configure $ make $ make install
$ tar zxvf gearmand-0.12.tar.gz $ cd gearmand-0.12 $ ./configure $ make $ make install
$ tar zxvf gearmand-0.12.tar.gz $ cd gearmand-0.12 $ ./configure $ make $ make install
$ gearman -h 192.168.10.247 -w -f wc -- wc -l #-w 选项指定gearman 工作在 worker模式 #-f 选项定义函数 wc 的功能为wc -l #-h 指定Job Server 地址
$ useradd gearmand -s /sbin/nologin $ germand -d -u gearmand #gearmand 服务 在后台以 gearmand 用户运行。
缺省会使用4730端口,下面会用到
以调试的方式启动:
gearmand -vv
$ gearman -h 192.168.10.247 -f wc < /etc/passwd #调用原先定义的函数 wc 对/etc/passwd 文件进行处理。 $ 34 #经过 worker处理后返回的值为34 整个处理过程发生在 worker服务器上,资源的消耗都在worker服务器上。 $ wc -l < /etc/passwd $ 34 #本地使用wc -l 命令对 /etc/passwd 进行处理 ,返回结果也为34
php-5.2.13 , gearman (注意是gearman ,而不是gearmand )
$ tar zxvf gearmand-0.12.tar.gz #安装gearmand $ cd gearmand-0.12 $ ./configure $ make $ make install $ tar xzf gearman-0.12.tgz # 安装gearman ,使 php支持 gearmand. $ cd gearman-0.12 $ phpize $ ./configure $ make $ make install
$ php --ini # 找出php.ini配置文件 Configuration File (php.ini) Path: /usr/local/php/etc Loaded Configuration File: /usr/local/php/etc/php.ini
extension="gearman.so"
# 检测配置结果
$ php --info | grep "gearman support" gearman support => enabled
$ useradd gearmand -s /sbin/nologin $ tar zxvf gearmand-0.12.tar.gz $ cd gearmand-0.12 $ ./configure $ make $ make install $ gearmand -d -u gearmand
配置同Client.
<?php $client= new GearmanClient(); $client->addServer("192.168.10.247"); print $client->do("reverse", "Hello World!"). "\n"; ?>
2. 在worker上编写worker.php,功能是将client.php发送过来的字符串进行反转,并向client 返回结果
<?php $worker= new GearmanWorker(); $worker->addServer("192.168.10.247"); $worker->addFunction("reverse", "my_reverse_function"); while ($worker->work()); function my_reverse_function($job){ return strrev($job->workload()); } ?>
执行worker.php
php worker.php &
3. 在client上执行client.php
php client.php !dlroW olleH #字符串“Hello World !”
php-5.2.13 ,gearman-0.7.0, imagick-2.3.0, ImageMagick-6.6.0-9, gearmand-0.12.tar.gz
$ tar zxvf gearmand-0.12.tar.gz $ cd gearmand-0.12 $ ./configure $ make $ make install
$ useradd gearmand -s /sbin/nologin $ tar zxvf gearmand-0.12.tar.gz $ cd gearmand-0.12 $ ./configure $ make $ make install $ gearmand -d -u gearmand
gearmand , gearman ,php 的配置同 先前php-extension 部分的worker配置相同
ImageMagick, imagick 的配置步骤如下:
$ yum install -y libjpeg-devel.x86_64 #注意,如果不安装libjpeg-devel,ImageMagick在编译后,将无法支持jpg格式 $ tar zxvf ImageMagick-6.6.0-9.tar.gz $ cd ImageMagick-6.6.0-9 $ ./configure && make && make install $ tar zxvf imagick-2.3.0.tgz $ cd imagick-2.3.0 $ phpize $ ./configure && make && make install
$ vi php.ini extension=imagick.so $ php --info | grep "imagick module " imagick module => enabled imagick module version => 2.3.0 #ImagickMagick支持配置成功
In the worker:编写 resize_worker.php ,代码如下:
<?php $worker= new GearmanWorker(); $worker->addServer("192.168.10.247"); $worker->addFunction("resize", "my_resize_function"); while ($worker->work()); function my_resize_function($job){ $thumb = new Imagick(); $thumb->readImageBlob($job->workload()); if ($thumb->getImageHeight() > 600) $thumb->scaleImage(0, 600); else if ($thumb->getImageWidth() > 800) $thumb->scaleImage(800, 0); return $thumb->getImageBlob(); } ?>
执行
resize_worker.php
php resize_worker.php &
Client Server,在当前目录准备一张命名为 nature.jpg 的图片;
$ gearman -h 192.168.10.247 -f resize < nature.jpg > new.jpg $ ls -ll ./*.jpg -rw-r--r-- 1 root root 578842 Mar 30 14:50 ./nature.jpg -rw-r--r-- 1 root root 204975 Mar 31 14:19 ./new.jpg
可以看出 nature.jpg 被worker端的ImagickMagick 处理过,图片的大小由原来的 578K 压缩为 204K .