标签:
最近纠结于一个问题,就是horizon 在处理前台数据的时候非得等到cinder client将数据全部获取后才开始执行horizon的下一行代码,这给大量数据显示造成了很大的时延,其实对于用户体验来说,完全可以仿照度妈或谷爷那样,先把少量数据尽快推送显示到用户浏览器,而同时建立消息队列继续将数据发送到浏览器,使用户在点击“下一页”的时候同时能体验到“秒打开”。综上所述,完全有必要在cinder client和horizon之间建立消息队列。
刚入捕蛇行列,一起从头开始学……
实验环境:centos 6.5 64bit
1、rabbbitMQ 服务安装
从官网下载rabbit-server安装包:rabbitmq-server-3.5.1-1.noarch.rpm
2、erlang安装
从rabbitMQ download页面下载:erlang-17.4-1.el6.x86_64.rpm
(当然,erlang安装也可以从源码安装,不过少不懂事的还是别折腾了)
3、安装
rpm --import http://www.rabbitmq.com/rabbitmq-signing-key-public.asc yum install rabbitmq-server-3.5.1-1.noarch.rpm
4、启动rabbit-server: /etc/init.d/rabbitmq-server start
5、消息队列入门级介绍:[RabbitMQ+Python入门经典] 兔子和兔子窝
详细过程就不啰嗦了,大牛的博客写的很详细,贴上我稍加修改的代码:
# _*_ coding:utf-8 _*_
# file_name: amqp_consumer.py from amqplib import client_0_8 as amqp conn = amqp.Connection(host="localhost:5672", userid="guest", password="guest", virtual_host="/", insist=False) chan = conn.channel() chan.queue_declare(queue="po_box", durable=True, exclusive=False, auto_delete=False) chan.exchange_declare(exchange="sorting_room", type="direct", durable=True, auto_delete=False,) chan.queue_bind(queue="po_box", exchange="sorting_room", routing_key="jason") def recv_callback(msg): print ‘Received: \"‘ + msg.body + ‘\" from channel #‘ + str(msg.channel.channel_id) chan.basic_consume(queue=‘po_box‘, no_ack=True, callback=recv_callback, consumer_tag="testtag") while True: chan.wait() chan.basic_cancel("testtag") chan.close() conn.close()
# file_name: amqp_publisher.py
from amqplib import client_0_8 as amqp import sys conn = amqp.Connection(host="localhost:5672", userid="guest", password="guest", virtual_host="/", insist=False) chan = conn.channel() # msg = amqp.Message(sys.argv[0]) msg = amqp.Message("hello world") msg.properties["delivery_mode"] = 2 chan.basic_publish(msg,exchange="sorting_room",routing_key="jason") chan.close() conn.close()
本文只是简单做了模仿工作,更深入的研究还待继续 ^_^
附:ZeroMQ,史上最快的消息队列 - ZMQ的学习和研究
标签:
原文地址:http://www.cnblogs.com/forilen/p/4493449.html