目录

分布式计算Gearman

Gearman 是一个通用应用架构,用于分布式处理,有Perl ,C 两个版本。

优点

  • 开源
  • 多语言支持
  • 灵活
  • 执行速度快
  • 可嵌入

适用场合

  • 分布式协同处理
  • 异构环境搭配(不同的操作系统,编程语言,硬件配置)
  • 异步处理消息(基于内存的快速存取队列)

工作原理

Gearman 架构图解

Gearman 架构图解

完整的Gearman 应用包含三个部分:a client , a worker , a job server

Gearman实现

网络环境:

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 )

安装

Job Server

$ tar zxvf gearmand-0.12.tar.gz
$ cd gearmand-0.12
$ ./configure
$ make
$ make install

Client Server

$ tar zxvf gearmand-0.12.tar.gz
$ cd gearmand-0.12
$ ./configure
$ make
$ make install

Worker Server

$ tar zxvf gearmand-0.12.tar.gz
$ cd gearmand-0.12
$ ./configure
$ make
$ make install

命令行工具

Worker Server

$ gearman -h 192.168.10.247 -w -f wc -- wc -l
 
#-w 选项指定gearman 工作在 worker模式
#-f 选项定义函数 wc 的功能为wc -l
#-h 指定Job Server 地址

Job Server

$ useradd gearmand -s /sbin/nologin
$ germand -d -u gearmand  
#gearmand 服务 在后台以 gearmand 用户运行。


缺省会使用4730端口,下面会用到

以调试的方式启动:

gearmand -vv

Client Server

$ 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 扩展

php-5.2.13 , gearman (注意是gearman ,而不是gearmand )

Client

$ 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 

编辑php.ini文件,添加如下条目;

extension="gearman.so"
                                                           

# 检测配置结果

$ php --info | grep "gearman support"
gearman support => enabled

Job Server

$ useradd gearmand -s /sbin/nologin
$ tar zxvf gearmand-0.12.tar.gz
$ cd gearmand-0.12
$ ./configure
$ make
$ make install 
 
$ gearmand -d -u gearmand              

Worker Server

配置同Client.

案例一 反转字符

  1. 在client上编写client.php,功能是向JOb server 发送一段字符创,并打印返回值。
<?php
$client= new GearmanClient();
$client->addServer("192.168.10.247");
print $client->do("reverse", "Hello World!"). "\n";
?>


可以设置不同级别的执行方式:
  • self::ClientInit()→doLow($key, $params);
  • self::ClientInit()→doHigh($key, $params);
  • self::ClientInit()→doBackground($key, $params);
  • self::ClientInit()→doLowBackground($key, $params);
  • self::ClientInit()→doHighBackground($key, $params);

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

Client

$ tar zxvf gearmand-0.12.tar.gz
$ cd gearmand-0.12
$ ./configure
$ make
$ make install


Job Server

$ useradd gearmand -s /sbin/nologin
$ tar zxvf gearmand-0.12.tar.gz
$ cd gearmand-0.12
$ ./configure
$ make
$ make install  
 
$ gearmand -d -u gearmand

Worker Server

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 .