Redis实现消息队列

个人理解在项目中使用消息队列一般是有如下几个原因:

    把瞬间服务器的请求处理换成异步处理,缓解服务器的压力

    实现数据顺序排列获取

​redis实现消息队列步骤如下:

1).redis函数rpush,lpop

2).建议定时任务入队列

3)创建定时任务出队列

文件:demo.php插入数据到redis队列
 
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
//$password = '123456';
//$redis->auth($password);
$arr = array('h','e','l','l','o','w','o','r','l','d');
 
foreach($arr as $k=>$v){
 
  $redis->rpush("mylist",$v);
 
}?>

文件:index.php定时扫描出队列

 
$redis = new Redis();
 
$redis->connect('127.0.0.1',6379);
 
$password = '123456';
 
$redis->auth($password);
 
//list类型出队操作
 
$value = $redis->lpop('mylist');
 
if($value){
 
 echo "出队的值".$value;
 
}else{
 
  echo "出队完成";
 
}
 
?>

立定时任务

 */1 * * * * root php /wwwroot/workplace/redis/index.php

 */3 * * * * root php /wwwroot/workplace/redis/demo.php

 

tail -f /var/log/cron  查看定时任务执行情况

Nov  7 00:30:01 dongzi CROND[6888]: (root) CMD (php /wwwroot/workplace/redis/demo.php)

Nov  7 00:30:01 dongzi CROND[6890]: (root) CMD (php /wwwroot/workplace/redis/index.php )

定时任务执行队列写入结果如下
    
127.0.0.1:6379> lrange mylist 0 -1
 
 1) "h"
 
 2) "e"
 
 3) "l"
 
 4) "l"
 
 5) "o"
 
 6) "w"
 
 7) "o"
 
 8) "r"
 
 9) "l"
 
10) "d"

  

 定时任务执行出队列后:
复制代码

127.0.0.1:6379> lrange mylist 0 -1

1) "e"

2) "l"

3) "l"

4) "o"

5) "w"

6) "o"

7) "r"

8) "l"

9) "d"



    连接Redis
        连接Redis
        Redis入队列
    Redis出队列 - 先进先出lPop 先进后出rPop
        Redis入队列
        Redis出队列 - 先进先出lPop 先进后出rPop
        阻塞出队列 brPopblPop 同上
        查看队列长度 LLEN


【PHP使用 Redis 实现消息队列简单事例】

Redis安装说明在此不再说,可以看我另一篇“Redis与PHP安装的那些事”,这里只介绍了windows下安装。


新建立两个文件,push和pop文件


1.push.php 推送

这里通过URL直接传递参数进行 keyword

$keyword=$_GET['keyword'];
$redis=newRedis();
$redis->connect('127.0.0.1',6379);
try{
echo$redis->LPUSH('list',''.$keyword);
}catch(Exception$e){
echo$e->getMessage();
}


2.pop.php 弹出接受信息

写个死循环,一直监听

$redis=newRedis();
$redis->connect('127.0.0.1',6379);
//echo"连接成功
";
//echo"状态:".$redis->ping();
while(true){
try{
$value=$redis->LPOP('list');
//这里进行业务处理
print_r(value);

}catch(Exception$e){
echo$e->getMessage();
}
}


在命令cmd下运行pop.php,我本地是xampp集成环境。故在cmd中运行

D:/xampp/php>php.exe E:/project/p2/redis/pop.php


3、在浏览器下运行

http://lock.com/redis/push.php?keyword=hello lock

在cmd中查看应该会输出hello lock


注意要点:

1、先要开启运行redis服务,在cmd下运行,不要关闭

2. 新开一个cmd窗口,运行pop.php

3. 在浏览器下运行push.php,在pop.php的cmd下可以看到相关值




----------------------------------------------------------------------------------------------------

PHP项目需要一个消息队列,最后为了简单选择了Redis List..
中文的Redis文档 http://redisdoc.com/  

Php +Redis 做消息队列
在Redis服务器已经启动的前提下.
1. 连接Redis

$redis = new Redis();
$redis->connect("127.0.0.1", "6379");  //php客户端设置的ip及端口  

2. Redis入队列

 $redis->lPush("GPS_LIST", data-notOrarrayOrObject);


我这里测试数组取不出来

    Insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations. When key holds a value that is not a list, an error is returned.

3.Redis出队列 - 先进先出lPop 先进后出rPop

$redis->lPush(("GPS_LIST");



开始我用的这玩意,但是非阻塞的,不好使。

    return – the value of the first element, or nil when key does not exist

redis> RPUSH mylist "one"
(integer) 1
redis> RPUSH mylist "two"
(integer) 2
redis> RPUSH mylist "three"
(integer) 3
redis> LPOP mylist
"one"
redis> LRANGE mylist 0 -1
1) "two"
2) "three"


    阻塞出队列 brPop,blPop 同上

http://redis.io/commands/brpop

    BRPOP is a blocking list pop primitive. It is the blocking version of RPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the tail of the first list that is non-empty, with the given keys being checked in the order that they are given.

    BLPOP is a blocking list pop primitive. It is the blocking version of LPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the head of the first list that is non-empty, with the given keys being checked in the order that they are given.


redis->brPop("GPS_LIST", 5);
返回的是一个数组
0=> "GPS_LIST"
1=> "实际数据"

-----
Return value
Array reply: specifically:
A nil multi-bulk when no element could be popped and the timeout expired.
A two-element multi-bulk with the first element being the name of the key where an element was popped and the second element being the value of the popped element.
-----
redis> DEL list1 list2
(integer) 0
redis> RPUSH list1 a b c
(integer) 3
redis> BLPOP list1 list2 0
1) "list1"
2) "a"

   

    查看队列长度 LLEN

    Returns the length of the list stored at key. If key does not exist, it is interpreted as an empty list and 0 is returned. An error is returned when the value stored at key is not a list.

Examples
redis> LPUSH mylist "World"
(integer) 1
redis> LPUSH mylist "Hello"
(integer) 2
redis> LLEN mylist
(integer) 2
redis>

   

2. Redis入队列

 $redis->lPush("GPS_LIST", data-notOrarrayOrObject);


我这里测试数组取不出来

    Insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations. When key holds a value that is not a list, an error is returned.

3.Redis出队列 - 先进先出lPop 先进后出rPop

$redis->lPush(("GPS_LIST");


开始我用的这玩意,但是非阻塞的,不好使。

    return – the value of the first element, or nil when key does not exist

redis> RPUSH mylist "one"
(integer) 1
redis> RPUSH mylist "two"
(integer) 2
redis> RPUSH mylist "three"
(integer) 3
redis> LPOP mylist
"one"
redis> LRANGE mylist 0 -1
1) "two"
2) "three"


4. 阻塞出队列 brPop,blPop 同上

http://redis.io/commands/brpop

    BRPOP is a blocking list pop primitive. It is the blocking version of RPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the tail of the first list that is non-empty, with the given keys being checked in the order that they are given.

    BLPOP is a blocking list pop primitive. It is the blocking version of LPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the head of the first list that is non-empty, with the given keys being checked in the order that they are given.


redis->brPop("GPS_LIST", 5);
返回的是一个数组
0=> "GPS_LIST"
1=> "实际数据"

-----
Return value
Array reply: specifically:
A nil multi-bulk when no element could be popped and the timeout expired.
A two-element multi-bulk with the first element being the name of the key where an element was popped and the second element being the value of the popped element.
-----
redis> DEL list1 list2
(integer) 0
redis> RPUSH list1 a b c
(integer) 3
redis> BLPOP list1 list2 0
1) "list1"
2) "a"

   

5. 查看队列长度 LLEN

    Returns the length of the list stored at key. If key does not exist, it is interpreted as an empty list and 0 is returned. An error is returned when the value stored at key is not a list.

Examples
redis> LPUSH mylist "World"
(integer) 1
redis> LPUSH mylist "Hello"
(integer) 2
redis> LLEN mylist
(integer) 2
redis>




【redis 队列操作的例子(php)】
Reids是一个比较高级的开源key-value存储系统,采用ANSI C实现。其与memcached类似,但是支持持久化数据存储

入队操作
复制代码 代码如下:

$redis = new Redis();
$redis->connect('127.0.0.1',6379);
while(True){
try{
$value = 'value_'.date('Y-m-d H:i:s');
$redis->LPUSH('key1',$value);
sleep(rand()%3);
echo $value."\n";
}catch(Exception $e){
echo $e->getMessage()."\n";
}
}
?>

出队操作
复制代码 代码如下:

$redis = new Redis();
$redis->pconnect('127.0.0.1',6379);
while(True){
try{
echo $redis->LPOP('key1')."\n";
}catch(Exception $e){
echo $e->getMessage()."\n";
}
sleep(rand()%3);
}?>

如何使用Redis 做队列操作
Reids是一个比较高级的开源key-value存储系统,采用ANSI C实现。其与memcached类似,但是支持持久化数据存储,同时value支持多种类型:字符串 (同memcached中的value),列表 ,集合 (Set),有序集合 (OrderSet)和Hash 。所有的值类型均支持原子操作,如列表中追加弹出元素,集合中插入移除元素等。Rdids的数据大部分位于内存中,其读写效率非常高,其提供AOF(追加 式操作记录文件)和DUMP(定期数据备份)两种持久化方式。Redis支持自定义的VM(虚拟内存)机制,当数据容量超过内存时,可以将部分Value 存储到文件中。同时Redis支持Master-Slave机制,可以进行数据复制。
可以把Redis的list结构当队列来用.
从上面Redis的场景和作用来说,对于我们现在的开发活动,究竟能把Redis引入在那些场景,而不是把这么好的东东演变成“为了使用Redis,而Redis”的惨烈局面呢?当然,具体问题具体分析,这个真的很重要哈。
缓存?分布式缓存?
队列?分布式队列?
某些系统应用(例如,电信、银行和大型互联网应用等)都会使用到,当然,现在大行其道的memcache就是很好的证明;但从某一方面来说,memcache是否能把两张囊括其中,而且能做到更好(没有实际的应用过,所以只是抛出)。但从Redis身上,我就能感觉到,Redis,就能把队列和缓存两张都囊括其中,而且都不会产生并发环境下的困扰,因为Redis中的操作都是原子操作来着。
至于评论两者的孰好孰坏就免了,存在就是理由,选择适合的就是最好的。
下面开始玩玩Redis中的队列(分布式)设计YY吧,请大虾们多多指点。
状况场景:
现在的项目,都是部署在多个服务器,或者多个IP上,而且前台经由F5分发,所以用户的请求究竟落在那一台的服务器上,是无法确定的。对于项目中,有一秒杀设计,刚开始没有考虑到这种部署,同时也是使用最容易处理的方式,直接给数据库表锁行记录(Oracle上的)。可以说,对于不同的应用部署,而只有一台数据库服务器来说,很“轻松”的就解决了这个并发的问题。所以现在考虑一下,是不是挪到应用上,避免数据库服务器也掺杂到业务上。
比如,现在有2台应用服务器,1台数据库服务器。想法是,把Redis部署在数据库服务器上,两台服务器在操作并发缓存或者队列时,先从Redis服务器上,取得在两台应用服务器的代理对象,再做入列出列的操作。
看代码实现(PHP)
入队列操作文件 list_push.php
复制代码 代码如下:

$redis = getRedisInstance();//从Redis服务器拿到redis实例
$redis->connect('Redis服务器IP', 6379);
while (true) {
$redis->lPush('list1', 'A_'.date('Y-m-d H:i:s'));
sleep(rand()%3);
}
?>

执行# php list_push.php &
出队列操作 list_pop.php文件
复制代码 代码如下:

$redis = getRedisInstance();//从Redis服务器拿到redis实例
$redis->pconnect('Redis服务器IP', 6379);
while(true) {
try {
var_export( $redis->blPop('list1', 10) );
} catch(Exception $e) {
//echo $e;
}
}

实现方法(Python)
1.入队列(write.py)
复制代码 代码如下:

#!/usr/bin/env python
import time
from redis import Redis
redis = Redis(host='127.0.0.1', port=6379)
while True:
now = time.strftime("%Y/%m/%d %H:%M:%S")
redis.lpush('test_queue', now)
time.sleep(1)

2.出队列(read.py)
复制代码 代码如下:

#!/usr/bin/env python
import sys
from redis import Redis
redis = Redis(host='127.0.0.1', port=6379)
while True:
res = redis.rpop('test_queue')
if res == None:
pass
else:
print str(res)

在操作时,注意,要操作的是同一个list对象。
呵呵,现在的主要思路就差不多就是如此,不过真实场景中,会有出入。


【Redis实现简单消息队列】

任务异步化

打开浏览器,输入地址,按下回车,打开了页面。于是一个HTTP请求(request)就由客户端发送到服务器,服务器处理请求,返回响应(response)内容。

我们每天都在浏览网页,发送大大小小的请求给服务器。有时候,服务器接到了请求,会发现他也需要给另外的服务器发送请求,或者服务器也需要做另外一些事情,于是最初们发送的请求就被阻塞了,也就是要等待服务器完成其他的事情。

更多的时候,服务器做的额外事情,并不需要客户端等待,这时候就可以把这些额外的事情异步去做。从事异步任务的工具有很多。主要原理还是处理通知消息,针对通知消息通常采取是队列结构。生产和消费消息进行通信和业务实现。
生产消费与队列

上述异步任务的实现,可以抽象为生产者消费模型。如同一个餐馆,厨师在做饭,吃货在吃饭。如果厨师做了很多,暂时卖不完,厨师就会休息;如果客户很多,厨师马不停蹄的忙碌,客户则需要慢慢等待。实现生产者和消费者的方式用很多,下面使用Python标准库Queue写个小例子:

import random
import time
from Queue import Queue
from threading import Thread

queue = Queue(10)

class Producer(Thread):
    def run(self):
        while True:
            elem = random.randrange(9)
            queue.put(elem)
            print "厨师 {} 做了 {} 饭 --- 还剩 {} 饭没卖完".format(self.name, elem, queue.qsize())
            time.sleep(random.random())

class Consumer(Thread):
    def run(self):
        while True:
            elem = queue.get()
            print "吃货{} 吃了 {} 饭 --- 还有 {} 饭可以吃".format(self.name, elem, queue.qsize())
            time.sleep(random.random())

def main():
    for i in range(3):
        p = Producer()
        p.start()
    for i in range(2):
        c = Consumer()
        c.start()

if __name__ == '__main__':
    main()

大概输出如下:

厨师 Thread-1 做了 1 饭 --- 还剩 1 饭没卖完
厨师 Thread-2 做了 8 饭 --- 还剩 2 饭没卖完
厨师 Thread-3 做了 3 饭 --- 还剩 3 饭没卖完
吃货Thread-4 吃了 1 饭 --- 还有 2 饭可以吃
吃货Thread-5 吃了 8 饭 --- 还有 1 饭可以吃
吃货Thread-4 吃了 3 饭 --- 还有 0 饭可以吃
厨师 Thread-1 做了 0 饭 --- 还剩 1 饭没卖完
厨师 Thread-2 做了 0 饭 --- 还剩 2 饭没卖完
厨师 Thread-1 做了 1 饭 --- 还剩 3 饭没卖完
厨师 Thread-1 做了 1 饭 --- 还剩 4 饭没卖完
吃货Thread-4 吃了 0 饭 --- 还有 3 饭可以吃
厨师 Thread-3 做了 3 饭 --- 还剩 4 饭没卖完
吃货Thread-5 吃了 0 饭 --- 还有 3 饭可以吃
吃货Thread-5 吃了 1 饭 --- 还有 2 饭可以吃
厨师 Thread-2 做了 8 饭 --- 还剩 3 饭没卖完
厨师 Thread-2 做了 8 饭 --- 还剩 4 饭没卖完

Redis 队列

Python内置了一个好用的队列结构。我们也可以是用redis实现类似的操作。并做一个简单的异步任务。

Redis提供了两种方式来作消息队列。一个是使用生产者消费模式模式,另外一个方法就是发布订阅者模式。前者会让一个或者多个客户端监听消息队列,一旦消息到达,消费者马上消费,谁先抢到算谁的,如果队列里没有消息,则消费者继续监听。后者也是一个或多个客户端订阅消息频道,只要发布者发布消息,所有订阅者都能收到消息,订阅者都是ping的。
生产消费模式

主要使用了redis提供的blpop获取队列数据,如果队列没有数据则阻塞等待,也就是监听。

import redis

class Task(object):
    def __init__(self):
        self.rcon = redis.StrictRedis(host='localhost', db=5)
        self.queue = 'task:prodcons:queue'

    def listen_task(self):
        while True:
            task = self.rcon.blpop(self.queue, 0)[1]
            print "Task get", task

if __name__ == '__main__':
    print 'listen task queue'
    Task().listen_task()

发布订阅模式

使用redis的pubsub功能,订阅者订阅频道,发布者发布消息到频道了,频道就是一个消息队列。

import redis


class Task(object):

    def __init__(self):
        self.rcon = redis.StrictRedis(host='localhost', db=5)
        self.ps = self.rcon.pubsub()
        self.ps.subscribe('task:pubsub:channel')

    def listen_task(self):
        for i in self.ps.listen():
            if i['type'] == 'message':
                print "Task get", i['data']

if __name__ == '__main__':
    print 'listen task channel'
    Task().listen_task()

Flask 入口

我们分别实现了两种异步任务的后端服务,直接启动他们,就能监听redis队列或频道的消息了。简单的测试如下:

import redis
import random
import logging
from flask import Flask, redirect

app = Flask(__name__)

rcon = redis.StrictRedis(host='localhost', db=5)
prodcons_queue = 'task:prodcons:queue'
pubsub_channel = 'task:pubsub:channel'

@app.route('/')
def index():

    html = """


Redis Message Queue




生产消费者模式




发布订阅者模式

"""
    return html


@app.route('/prodcons')
def prodcons():
    elem = random.randrange(10)
    rcon.lpush(prodcons_queue, elem)
    logging.info("lpush {} -- {}".format(prodcons_queue, elem))
    return redirect('/')

@app.route('/pubsub')
def pubsub():
    ps = rcon.pubsub()
    ps.subscribe(pubsub_channel)
    elem = random.randrange(10)
    rcon.publish(pubsub_channel, elem)
    return redirect('/')

if __name__ == '__main__':
    app.run(debug=True)

启动脚本,使用

siege -c10 -r 5 http://127.0.0.1:5000/prodcons
siege -c10 -r 5 http://127.0.0.1:5000/pubsub

可以分别在监听的脚本输入中看到异步消息。在异步的任务中,可以执行一些耗时间的操作,当然目前这些做法并不知道异步的执行结果,如果需要知道异步的执行结果,可以考虑设计协程任务或者使用一些工具如RQ或者celery等。


你可能感兴趣的:(Redis)