Redis+php-resque实现消息队列

服务器硬件配置

Dell PowerEdge R310英特尔单路机架式服务器

  • Intel Xeon Processor X3430 2.4GHz, 8MB Cache
  • 8GB内存(2 x 4GB), 1333MHz, 双列RDIMMs用1于处理器配置
  • 2TB近线3.5英寸7.2K RPM 6Gbps SAS硬盘 - 非热插拔
  • SAS 6/iR 控制卡
  • 8倍速SATA超薄DVD-ROM光驱
  • 非冗余电源, 350W

软件环境

  • CentOS 6.2 minimal
  • Nginx 1.2.7
  • PHP 5.3.17
  • MySQL 5.5.28-log
  • Redis 2.6.16

一、Redis安装

1. 获取源码

shell># cd /usr/local/src

shell># wget http://download.redis.io/releases/redis-2.6.16.tar.gz

shell># tar -zxvf redis-2.6.16.tar.gz

shell># cd redis-2.6.16

2. 编译、安装

shell># make

3. 配置Redis

编辑redis.conf文件

shell># vi /usr/local/src/redis-2.6.16/redis.conf

daemonize = yes

4. 启动Redis

shell># src/redis-server /usr/local/src/redis-2.6.16/redis.conf

5. 测试连接

shell># src/redis-cli

redis> set foo bar

OK

redis> get foo

“bar”

redis> exit;

二、phpredis 安装

1. 获取源码

shell># cd /usr/local/src

shell># wget https://github.com/nicolasff/phpredis/archive/2.2.3.tar.gz -O nicolasff-phpredis-2.2.3.tar.gz

shell># tar -zxvf nicolasff-phpredis-2.2.3.tar.gz

shell># cd phpredis-2.2.3

2. 编译、安装

shell># /usr/local/php/bin/phpize

shell># ./configure –-with-php-config=/usr/local/php/bin/php-config

shell># make && make install

3. 配置php.ini

shell># vi /usr/local/php/etc/php.ini

extension=redis.so

4. 重启nginx & php-fpm

三、php-resque安装

*准备工作

需要pcntl支持

安装git

shell># yum -y install git

安装Composer

shell># cd /usr/local/bin

shell># curl –sS https://getcomposer.org/installer | php

shell># mv composer.phpar /usr/local/bin/composer

修改 php.ini

disable_functions = … …

将proc_open和proc_get_status以及exec删除

保存后,重启LNMP

1. 获取源码

shell># cd /home/wwwroot/default

shell># git clone git://github.com/chrisboulton/php-resque.git

2. 安装

shell># cd php-resque

shell># composer install

四、Demo

1. 一个Job

Job|任务:一个Job就是一个需要在后台完成的任务,比如邮件发送,就可以抽象为一个Job。在Resque中一个Job就是一个Class。

shell># cd php-resque

shell># vim demo/job.php

<?php

class PHP_Job

{

public function perform()

{

fwrite(STDOUT, 'Start job! -> ');

sleep(10);

fwrite(STDOUT, 'Job ended!' . PHP_EOL);

}

}

这个Job的具体任务:输出’Start job! ->’,然后等待10秒钟后,再次输出’Job ended!’。

在Resque的设计中,一个Job必须存在一个perform方法,Worker则会自动运行这个方法。

2. 将Job插入队列

Queue|队列:就是标题中的消息队列。在Resque中,队列由Redis来负责实现。Resque还提供了一个简单的队列管理器,可以实现将Job插入/取出队列等功能。

shell># vim demo/queue.php

<?php

if(empty($argv[1]) or empty($argv[2])) {

die('Specify the name of a queue or job to add. e.g, php queue.php default PHP_Job');

//在cli命令行中运行queue.php 后面必须加上两个参数,第一个参数为queue的名称 第二个参数为job的名称,必须同之前编写的Job类的类名。

}

require __DIR__ . '/init.php';

date_default_timezone_set('GMT');

Resque::setBackend('127.0.0.1:6379');

$args = array(

'time' => time(),

'array' => array(

'test' => 'test',

),

);

$jobId = Resque::enqueue($argv[1], $argv[2], $args, true);

echo "Queued job ".$jobId."\n\n";

在cli方式中运行:

shell># php demo/queue.php default PHP_Job

结果可以在屏幕上看到输出:

Queued job 4ee7cefcb7df03ff5475fd450e2a5bea

即Job添加入队列成功。

3. 查看Job运行情况

php-resque同样提供了查看Job运行状态的例子,直接运行:

shell># php demo/check_status.php 4ee7cefcb7df03ff5475fd450e2a5bea

可以看到输出:

Tracking status of 10de5352387ba7212bc0d4e8975a3523. Press [break] to stop.

Status of 4ee7cefcb7df03ff5475fd450e2a5bea is: 1

Status of 4ee7cefcb7df03ff5475fd450e2a5bea is: 1

Status of 4ee7cefcb7df03ff5475fd450e2a5bea is: 1

我们刚才创建的Job运行状态为1。在Resque中,一个Job有以下4中状态:

  • Resque_Job_Status::STATUS_WAITING = 1; (等待)
  • Resque_Job_Status::STATUS_RUNNING = 2; (正在执行)
  • Resque_Job_Status::STATUS_FAILED = 3; (失败)
  • Resque_Job_Status::STATUS_COMPLETE = 4; (结束)

由于没有运行Worker,所以刚才创建的Job还是等待状态。

4. 运行Worker

shell># vim demo/resque.php

<?php

date_default_timezone_set('GMT');

require 'job.php';

require '../bin/resque';

可以看到一个Worker至少需要两部分:

1.可以直接包含Job类文件,也可以使用php的自动加载机制,指定好Job Class所在路径并能实现自动加载。

2.包含Resque中默认的Worker: bin/resque

在cli方式下运行:

shell># QUEUE=default php demo/resque.php

前面的QUEUE部分是设置的环境变量,我们指定当前的Worker只负责处理default队列。也可以使用

shell># QUEUE=* php demo/resque.php

来处理所有的队列。

运行后,可以看到输出:

[notice] Starting worker s3:19060:default

[notice] Starting work on (Job{default} | ID: 4ee7cefcb7df03ff5475fd450e2a5bea | PHP_Job | [{"time":1378073476,"array":{"test":"test"}}])

Start job! -> Job ended! //Job的运行结果

[notice] (Job{default} | ID: 4ee7cefcb7df03ff5475fd450e2a5bea | PHP_Job | [{"time":1378073476,"array":{"test":"test"}}]) has finished

使用ps指令检查一下:

shell># ps aux | grep resque

可以看到有一个php的守护进程已经在运行了

root 19060 0.0 0.6 209680 11768 pts/0 S+ 06:12 0:01 php demo/resque.php

任务运行完毕后,同时屏幕可以看到输出结果!

至此利用Redis + php-resque实现消息队列的演示全部完成。

5. 参考资料

http://avnpc.com/pages/run-background-task-by-php-resque

http://kamisama.me/2012/10/09/background-jobs-with-php-and-resque-part-1-introduction/