RabbitMQ二----' helllo world '
RabbitMQ实现了AMQP定义的消息队列。它实现的功能”非常简单“:从Producer接收数据然后传递到Consumer。它能保证多并发,数据安全传递,可扩展。
我们将会设计两个程序,一个发送Hello world,另一个接收这个数据并且打印到屏幕。
整体如下图:

一、环境配置:
RabbitMQ实现了AMQP。因此,我们需要安装AMPQ的library。幸运的是对于多种编程语言都有实现。我们可以使用以下lib的任何一个:
- py-amqplib
- txAMQP
- pika
pip install pika==0.9.8
这个安装依赖于pip和Git-core。
On Ubuntu:
$ sudo apt-get install python-pip git-core
On Debian:
$ sudo apt-get install python-setuptools git-core
$ sudo easy_install pipOn Windows:To install easy_install, run the MS Windows Installer for setuptools
> easy_install pip
> pip install pika==0.9.8
二、发送消息(Sending):

第一个program send.py:发送Hello world 到queue。正如我们在上篇文章提到的,你程序的第一句话就是建立连接,第二句话就是创建channel:
#!/usr/bin/env python
import pika connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
创建连接传入的参数就是RabbitMQ Server的ip或者name。
关于谁创建queue,上篇文章也讨论过:Producer和Consumer都应该去创建。
接下来我们创建名字为hello的queue:
channel.queue_declare(queue='hello')
创建了channel,我们可以通过相应的命令来list queue:
$ sudo rabbitmqctl list_queues
Listing queues ...
hello 0
...done.
现在我们已经准备好了发送了。
从架构图可以看出,Producer只能发送到exchange,它是不能直接发送到queue的。现在我们使用默认的exchange(名字是空字符)。这个默认的exchange允许我们发送给指定的queue。routing_key就是指定的queue名字。
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print " [x] Sent 'Hello World!'"
退出前别忘了关闭connection。
connection.close()
3、接收(Receiving)

第二个program receive.py 将从queue中获取Message并且打印到屏幕。
第一步还是创建connection。第二步创建channel。第三步创建queue,name = hello:
channel.queue_declare(queue='hello')
接下来要subscribe了。在这之前,需要声明一个回调函数来处理接收到的数据。
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
subscribe:
channel.basic_consume(callback,
queue='hello',
no_ack=True)
最后,准备好无限循环监听吧:
print ' [*] Waiting for messages. To exit press CTRL+C'
channel.start_consuming()
4.最终版本:
send.py
#!/usr/bin/env python
import pika connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel() channel.queue_declare(queue='hello') channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()
receive.py
#!/usr/bin/env python
import pika connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel() channel.queue_declare(queue='hello') print ' [*] Waiting for messages. To exit press CTRL+C' def callback(ch, method, properties, body):
print " [x] Received %r" % (body,) channel.basic_consume(callback,
queue='hello',
no_ack=True) channel.start_consuming()
5.最终运行
先运行send.py program
$ python send.py
[x] Sent 'Hello World!'
send.py 每次运行完都会停止,注意:现在数据库已经到queue里面了。接受它:
$ python receive.py
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Hello World!'
RabbitMQ二----' helllo world '的更多相关文章
- RabbitMQ(二):理解消息通信RabbitMQ
原文:RabbitMQ(二):理解消息通信RabbitMQ 一.消费者.生产者和信道 生产者(producer):生产者创建消息,然后发布(发送)到代理服务器(RabbitMQ),可以说发送消息的程序 ...
- 安装RabbitMQ(二)
RabbitMQ的简易安装 前一篇博文的RabbitMQ安装有点复杂,经过搜索发现简单的安装方式如下. 1.Erlang Yum Repos 基于 SSL 高版本包含插件 rpm -Uvh http: ...
- rabbitmq (二) 持久化
默认情况下rabbitmq 是根据消费者多少依次投递,投递后就删除消息. 消息不会重复投递给不同的消费者. 消费者如果遇到长时间的任务,会执行完一个消息之后再执行下一个消息, 消费者持久化: 如果一个 ...
- RabbitMQ(二):Java 操作队列
1. 简单模式 模型: P:消息的生产者 队列:rabbitmq C:消息的消费者 获取 MQ 连接 public static Connection getConnection() throws I ...
- RabbitMQ(二):mandatory标志的作用
本文转自:http://m.blog.csdn.net/article/details?id=54311277 在生产者通过channel的basicPublish方法发布消息时,通常有几个参数需要设 ...
- Rabbitmq(二)
1.安装 Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装RabbitMQ之前要先安装Erlang. erlang:http://www.erlang.org/download. ...
- RabbitMQ (二) 简单队列
参考:https://blog.csdn.net/vbirdbest/article/details/78583480 简单队列的模型: P : 生产者,即 Producer C : 消费者,即 Co ...
- 消息队列--RabbitMQ(二)
1.常用的几种队列简介 RabbitMQ有五种常用的队列,分别是:简单队列.work模式.发布订阅模式.路由模式.主题(Topic)模式.其实发布订阅.路由.主题这三种模式都从属于与routingke ...
- 快速掌握RabbitMQ(二)——四种Exchange介绍及代码演示
在上一篇的最后,编写了一个C#驱动RabbitMQ的简单栗子,了解了C#驱动RabbitMQ的基本用法.本章介绍RabbitMQ的四种Exchange及各种Exchange的使用场景. 1 direc ...
随机推荐
- [ CodeVS冲杯之路 ] P1091
不充钱,你怎么AC? 题目:http://codevs.cn/problem/1091/ 大家都写的 DFS,然而我想到了一种贪心的做法,重点是可以A 普遍的贪心是每次删掉该深度子树最大的点,但是如果 ...
- Appium+python自动化18-brew、carthage和appium-doctor【转载】
前言 本篇安装brew.carthage,解决启动appium时的报错问题,另外安装appium-doctor检查appium的环境 1.brew 2.carthage 3.appium-doctor ...
- POJ 1298 The Hardest Problem Ever【字符串】
Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was ke ...
- unittest (python标准库-开发工具-单元测试框架)
unittest官方文档摘录 翻译 reffer to: https://docs.python.org/3/library/unittest.html#unittest.TextTestRunner ...
- 10.9 顾z校内互坑题
T1 (help) 题意简述 给定一个长度为\(n\)的序列.然后给出多组询问. 询问\([l,r]\)区间内不等于该段区间\(gcd\)的数的个数. 分析 看到区间问题,优先考虑线段树 or 树状数 ...
- 【 模_板 】 for NOIP 2017
高精度 #include <cstring> #include <cstdio> #define max(a,b) (a>b?a:b) inline void read( ...
- java读取配置文件(properties)的时候,unicode码转utf-8
有时我们在读取properties结尾的配置文件的时候,如果配置文件中有中文,那么我们读取到的是unicode码的中文,需要我们在转换一下,代码如下 /** * 将配置文件中的Unicode 转 ut ...
- SQL SERVER 内存学习系列
http://www.cnblogs.com/double-K/p/5049417.html http://blog.sina.com.cn/s/blog_5deb2f5301014wti.html ...
- ubifs & mtd
前天晚上在写完另一篇总结之时,赵XX向我咨询了关于mtd 和ubifs的相关内容.而我在这方面只是略懂皮毛,所以向他许愿共同调查这个方面的知识.经过昨天一天的调查,最后感觉是有了一定的经验和基础了,所 ...
- MessageFormat.format 字符串的模板替换
项目目前在消息的模版,模版中需要替换很多参数,比方说“用户名”,“日期”等等.不过目前没有想到更好的替换参数的方法,所以目前只能使用一个比较简单的方式来实现.这个方式太死板,参数对应必须要在代码中写死 ...