RabbitMQ--Hello world!(一)
Introduction
RabbitMQ is a message broker. The principal idea is pretty simple: it accepts and forwards messages. You can think about it as a post office: when you send mail to the post box you're pretty sure that Mr. Postman will eventually deliver the mail to your recipient. Using this metaphor RabbitMQ is a post box, a post office and a postman.
RabbitMQ和通常意义上的消息发送,用下面行话:
Producing means nothing more than sending. A program that sends messages is a producer. We'll draw it like that, with "P":

A queue is the name for a mailbox. It lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can be stored only inside a queue. A queue is not bound by any limits, it can store as many messages as you like ‒ it's essentially an infinite buffer. Many producers can send messages that go to one queue, many consumers can try to receive data from one queue. A queue will be drawn as like that, with its name above it:

Consuming has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages. On our drawings it's shown with "C":
helloworld例子
生产者发送消息到队列,消费者取出消息并打印。

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') def callback(ch, method, properties, body):
print(" [x] Received %r" % body) channel.basic_consume(callback,
queue='hello',
no_ack=True) print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
代码解释:send.py
#!/usr/bin/env python
import pika connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
与rabbitmq server建立连接,
channel.queue_declare(queue='hello')
创建一个queue,我们发送的消息会被放到这个消息队列。如果消息队列不存在,rabbitmq就会丢弃这条消息
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
在rabbitmq中,消息不能直接发送到queue中,通常会经过一个exchange。这里我们使用默认的exchange,用空串标识。
这个exchange是特殊的,它要求我们精确指定这条消息发送到哪个queue。queue的名字需要通过routing_key变量。
connection.close()
在退出程序前我们要关闭这个连接。
代码解释recv.py
#!/usr/bin/env python
import pika connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel() channel.queue_declare(queue='hello')
首先连接到rabbitmq server,然后要确保queue存在,创建一个queue使勇queue_declare。
我们可以运行这个命令多次,但如果名字相同,仅有一个会被创建。
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
从队列中接收消息是比较复杂的,要为queue订阅一个callback函数。当接收到一个消息,callback函数就会被调用。
我们这个回调函数打印消息内容到屏幕上。
channel.basic_consume(callback,
queue='hello',
no_ack=True)
下一步,告诉rabbitmq特殊的callback函数应该从我们的hello队列接收消息。
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
最后,开启一个无限的循环等待数据并在必要的时候运行callback函数。
运行结果:
# python send.py
[x] Sent 'Hello World!' # python receive.py
[*] Waiting for messages. To exit press CTRL+C
[x] Received b'Hello World!'
RabbitMQ--Hello world!(一)的更多相关文章
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- RabbitMq应用二
在应用一中,基本的消息队列使用已经完成了,在实际项目中,一定会出现各种各样的需求和问题,rabbitmq内置的很多强大机制和功能会帮助我们解决很多的问题,下面就一个一个的一起学习一下. 消息响应机制 ...
- 如何优雅的使用RabbitMQ
RabbitMQ无疑是目前最流行的消息队列之一,对各种语言环境的支持也很丰富,作为一个.NET developer有必要学习和了解这一工具.消息队列的使用场景大概有3种: 1.系统集成,分布式系统的设 ...
- RabbitMq应用一的补充(RabbitMQ的应用场景)
直接进入正题. 一.异步处理 场景:发送手机验证码,邮件 传统古老处理方式如下图 这个流程,全部在主线程完成,注册->入库->发送邮件->发送短信,由于都在主线程,所以要等待每一步完 ...
- RabbitMq应用一
RabbitMq应用一 RabbitMQ的具体概念,百度百科一下,我这里说一下我的理解,如果有少或者不对的地方,欢迎纠正和补充. 一个项目架构,小的时候,一般都是传统的单一网站系统,或者项目,三层架构 ...
- 缓存、队列(Memcached、redis、RabbitMQ)
本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...
- 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)
Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...
- windows下 安装 rabbitMQ 及操作常用命令
rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.它遵循Mozilla Public License开源协议,采用 Erlang 实现的工业级的消息队列(MQ)服务器,Rab ...
- RabbitMQ + PHP (三)案例演示
今天用一个简单的案例来实现 RabbitMQ + PHP 这个消息队列的运行机制. 主要分为两个部分: 第一:发送者(publisher) 第二:消费者(consumer) (一)生产者 (创建一个r ...
- RabbitMQ + PHP (二)AMQP拓展安装
上篇说到了 RabbitMQ 的安装. 这次要在讲案例之前,需要安装PHP的AMQP扩展.不然可能会报以下两个错误. 1.Fatal error: Class 'AMQPConnection' not ...
随机推荐
- 使用rundll32.exe绕过应用程序白名单(多种方法)
0x00 前言 本文演示了白名单AppLocker bypass的最常见和最熟悉的技术.我们知道,出于安全原因,系统管理员添加组策略来限制本地用户的应用程序执行.在上一篇文章中,我们讨论了“ Wind ...
- 搭建hadoop集群
hadoop的架构 HDFS + MapReduce = Hadoop MapReduce = Mapper + Reducer hadoop的生态系统 准备四个节点,系统版本为CentOS7.3 1 ...
- 《剑指offer》— JavaScript(16)合并两个排序的链表
合并两个排序的链表 题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. *** 思路 如果pHead1和pHead2中有一个为空,则result是另 ...
- Eclipse开发java程序里Test用不了,怎么导包?
1.右键项目Build Path->Add External JARs,选择要导入的jar包即可: 2.建立方法,引入junit. 3.ok
- egg.js路由的优雅改造
引言 在使用express,koa, 或者是egg.js进行node server开发的过程中,我们的路由基本上都是定义在controller层的,框架对于 node 原生路由都会进行一层封装,一版都 ...
- Tomcat权威指南-读书摘要系列3
3. 在Tomcat中部署Servlet与JSP Web应用程序 jar命令打包war文件 jar cvf examples.war .
- 转:iOS-CoreLocation:无论你在哪里,我都要找到你!
1.定位 使用步骤: 创建CLLocationManager示例,并且需要强引用它 设置CLLocationManager的代理,监听并获取所更新的位置 启动位置更新 1 2 3 _manager = ...
- IOS计算文字高度
1.计算文字长度 NSString* str = @"你好"; .f; NSStringDrawingOptions options = NSStringDrawingUsesLi ...
- [CQOI2009]DANCE跳舞(ISAP写法)
https://daniu.luogu.org/problemnew/show/3153 #include<queue> #include<cstdio> #include&l ...
- Linux系统自动备份的Shell
公司现在需要对现有的服务器进行定期备份,并将备份文件放置到正在使用的NAS中去: 为了备份的效率,还需要对备份的文件进行筛选,排除一些后缀名的文件: 实现方法如下: 1. 编写备份的shell文件 在 ...