PHP uses Redis as Message Queuing

Source: Internet
Author: User
Tags error handling install php php script install redis
Install Redis

Redis is not in the CentOS official source, you need to manually download Epel (Extra Packages for Enterprise Linux) to install.
Epel is equivalent to the expansion of the original source, the specific introduction can look at these two articles

Http://blog.chinaunix.net/uid-2469966-id-3916408.html

Http://f.dataguru.cn/thread-47927-1-1.html

Note that when we go to Http://fedoraproject.org/wiki/EPEL this site to find the installation files, it should be consistent with your system version, such as I use the CENTOS7, I downloaded the address.

https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm

RPM-IVH epel-release-6-8.noarch.rpm    #安装epel扩展
yum-y install redis        #安装redis
yum Install Php-redis       #安装php扩展
Service redis start         #启动redis服务器
chkconfig redis on          #设置redis开机启动

Php-redis Extension Manual address: Https://github.com/phpredis/phpredis#connect-open use list as Redis queue

List in the Redis is a two-way queue, we can from the queue to the left team, from the right side of the queue. This allows you to implement first-come-first-served queue operations. Team Script

Whenever a server request is received, the information to be processed is inserted from the left side of the list.

$params = Json_encode ($_request);
$redis->lpush ($key, $params);
out Team Script

Write the script, execute the script through the crontab, and remove the error handling from the right of the list.

The advantage of this approach is that there will be no loss of data, only the timing of the script to process the relevant information before it is removed from the queue; The disadvantage is obvious, the response is not timely, because it is timed access to the Redis of the list formation, so can not real-time processing of messages in the queue.

$params = $redis->rpop ($key);

Timing the information from the queue, you can use the PHP script sleep mode, you can also use crontab implementation.

PHP implementation

while (true) {
    //Thanks to the bright stars fly comments
    //blocking queue way avoids the waste of system resources, improves the response speed of processing queues, and is superior to Rpop mode
    handle ($params = $redis-> Brpop ($key));

Crontab implementation

* * * * * * * sleep 20; /usr/bin/php/var/www/html/handler.php #20s执行一次 */1 * * * * * *
/usr/bin/php/var/www/html/handler.php #每分钟执行一次
use Subscribe/publish as a queue

In Redis, we support a publishing subscription mechanism, just like a radio broadcast. A after subscribing to a channel, if B has posted a message on the channel, a will be able to hear it. Publish Script

Whenever a server request is received, the information to be processed is published to the corresponding channel.

$params = Json_encode ($_request);
$redis->publish ($channel, $params);
Subscribe to Scripts

Write a script that executes in the Linux background and listens in real-time to the message that the channel broadcasts.

The advantage of this approach is that data can be processed in real time, and is Redis actively to PHP script push message, the disadvantage is that there may be loss of data, because the message is published in real time, the Publish/subscribe channel does not save the message, only as a message delivery channel, if the message is not subscribed to the script to capture, will result in loss of data.

handler.php

$redis->subscribe (Array ($channel), ' Handlefun ');
function Handlefun ($redis, $chan, $msg) {
    $params = Json_decode ($msg, true);
    ....

The above data processing script handler.php, has been running in the background.

We can use the following command to perform in the background

nohup/usr/bin/php/var/www/handler.php >>/tmp/handler.log 2>&1 &

We are writing a timed task to monitor the execution of the script in order to prevent the script from accidentally terminating (errors, etc.).

monitor.sh

#!/bin/bash
alive= ' ps aux|grep \/usr\/bin\/php|grep-v grep|wc-l '
if [$alive-eq 0]
then
nohup/usr/bi n/php/var/www/handler.php >>/tmp/handler.log 2>&1 &
fi
CRONTAB-E */1 * * * * * *
/var/www/monitor.sh
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.