RabbitMQ 是一个消息broker。它的主要概念就是接受和转发消息。可以把它当作一个邮局:当向邮箱投递一封邮件时,你确信邮差最终会将这封邮件投递到收件人。使用这个比喻,RabbitMQ就是邮箱,邮局和邮差。

RabbitMQ和邮局最大的区别就是它不处理纸质信件而是处理二进制数据--消息

RabbitMQ和其他消息系统通常都有以下几个术语:

生产者   发送消息

队列

消费者

使用python驱动发送"Hello World!"

pip install pika

第一个测试程序send.py将会向这个队列发送单个消息。首要做的是向RabbitMQ Server建立一个连接。

#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

默认情况下是不能使用guest用户登录的

In [15]: import pikaIn [16]: credentials=pika.PlainCredentials('xxx','xxx')In [17]: parameters=pika.ConnectionParameters(host='172.28.10.71',credentials=credentials)

在发送消息之前需要确保收件人队列存在,如果发送消息到不存在的地点,RabbitMQ将会直接丢弃这个消息。

创建一个用于投递消息的队列

channel.queue_declare(queue='hello')

使用RabbitMQ,不能直接发送一条消息到队列。它需要经过一个交换机exchange.

channel.basic_publish(exchange='',routing_key='hello',body='Hello World!')
print " [x] Sent 'Hello World!'"

在退出程序之前we need to make sure the network buffers were flushed and our message was actually delivered to RabbitMQ.只需要关闭连接就行。

connection.close()

完成的程序 send.py

#!/usr/bin/pythonimport pikaconnection=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()
...............[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# rabbitmqctl list_queues
Listing queues ...
hello   28

每执行一次就往名为hello的队列中添加一条消息,使用rabbitmqctl list_queues可以查看队列长度

第二个程序receive.py 将会从这个队列中接收消息并打印它们

首先需要连接RabbitMQ,然后需要确保队列存在

channel.queue_declare(queue='hello')

从队列中接受消息要复杂得多,它的工作方式是向这个队列订阅一个callback函数。任何时候,接收到一条消息,callback函数会被pika库调用

def callback(ch, method, properties, body):print " [x] Received %r" % (body,)

然后需要告诉RabbitMQ这个特别的函数是需要从hello队列接收消息

channel.basic_consume(callback,queue='hello',no_ack=True)

最后,引入一个永不结束的循环等待接收数据并运行callback函数

print ' [*] Waiting for messages. To exit press CTRL+C'channel.start_consuming()

receive.py

#!/usr/bin/pythonimport pikaconnection=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()

然后测试:

[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python send.py
[x] Sent 'Hello World!'
[root@test71 rabbitmq]# python receive.py
[*] Waiting for messages.To exit press CTRL+c
[x] Received 'Hello World!'
[x] Received 'Hello World!'
[x] Received 'Hello World!'
[x] Received 'Hello World!'
[x] Received 'Hello World!'
[x] Received 'Hello World!'
[x] Received 'Hello World!'
[x] Received 'Hello World!'
[x] Received 'Hello World!'
^CTraceback (most recent call last):File "receive.py", line 16, in <module>channel.start_consuming()File "/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py", line 955, in start_consumingself.connection.process_data_events()File "/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py", line 240, in process_data_eventsif self._handle_read():File "/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py", line 347, in _handle_readif self._read_poller.ready():File "/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py", line 43, in innerreturn f(*args, **kwargs)File "/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py", line 85, in readyevents = self.poller.poll(self.poll_timeout)
KeyboardInterrupt

参考资料:

http://previous.rabbitmq.com/v3_3_x/tutorials/tutorial-one-python.html

转载于:https://blog.51cto.com/john88wang/1670904

RabbitMQ学习二相关推荐

  1. RabbitMQ学习系列二:.net 环境下 C#代码使用 RabbitMQ 消息队列

    上一篇已经讲了Rabbitmq如何在Windows平台安装,不懂请移步:RabbitMQ学习系列一:windows下安装RabbitMQ服务 一.理论: .net环境下,C#代码调用RabbitMQ消 ...

  2. 官网英文版学习——RabbitMQ学习笔记(二)RabbitMQ安装

    一.安装RabbitMQ的依赖Erlang 要进行RabbitMQ学习,首先需要进行RabbitMQ服务的安装,安装我们可以根据官网指导进行http://www.rabbitmq.com/downlo ...

  3. RabbitMQ(二):Work Queues、循环分发、消息确认、持久化、公平分发

    内容翻译自:RabbitMQ Tutorials Java版 RabbitMQ(一):Hello World程序 RabbitMQ(二):Work Queues.循环分发.消息确认.持久化.公平分发 ...

  4. RabbitMQ 学习笔记

    RabbitMQ 学习笔记 RabbitMQ 学习笔记 1. 中间件 1.1 什么是中间件 1.2 为什么要使用消息中间件 1.3 中间件特点 1.4 在项目中什么时候使用中间件技术 2. 中间件技术 ...

  5. 乐行学院RabbitMQ学习教程 第一章 RabbitMQ介绍(可供技术选型时使用)

    乐行学院RabbitMQ学习教程 第一章 RabbitMQ介绍 RabbitMQ介绍 1.RabbitMQ技术简介 2.RabbitMQ其他扩展插件 2.1监控工具rabbitmq-managemen ...

  6. RabbitMQ(二):RabbitMQ高级特性

    RabbitMQ(二):RabbitMQ高级特性 RabbitMQ是目前非常热门的一款消息中间件,不管是互联网大厂还是中小企业都在大量使用.作为一名合格的开发者,有必要了解一下相关知识,RabbitM ...

  7. RabbitMQ学习总结 第一篇:理论篇

    目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...

  8. RabbitMQ学习笔记四:RabbitMQ命令(附疑难问题解决)

    RabbitMQ学习笔记四:RabbitMQ命令(附疑难问题解决) 参考文章: (1)RabbitMQ学习笔记四:RabbitMQ命令(附疑难问题解决) (2)https://www.cnblogs. ...

  9. rabbitmq 学习-9- RpcClient发送消息和同步接收消息原理

    rabbitmq 学习-9- RpcClient发送消息和同步接收消息原理

最新文章

  1. Pause/Resume Instance 操作详解 - 每天5分钟玩转 OpenStack(34)
  2. QT最方便的LOG库使用Easylogging++,只需要一个头文件
  3. latex表格的整理是需要一定的时间的
  4. php redis 队列,Redis 实现队列
  5. PHP中提问频率最高的11个面试题和答案
  6. 长语音识别_长文本语音识别_语音 识别 - 云+社区 - 腾讯云
  7. go mockweb接口_GitHub - duxiaoman/AnyMock: 通用接口Mock平台
  8. 私人影院音响效果如何有效提升?
  9. (5)数据分析-T检验
  10. 香港股票交易成本计算器 android,股票交易手续费计算器
  11. Python爬虫爬取豆瓣TOP250
  12. linux集群pbs管理,PBS-Torque集群部署
  13. 计算机专业研究生核心能力培养(2)——如何更好的写代码
  14. 51单片机实现浮点数四则运算计算器(C语言的偏软硬件实现)
  15. 【论文笔记_知识蒸馏_目标检测_2022】Decoupled Knowledge Distillation
  16. JAVA第五课:正则表达式
  17. 王者战力查询教程,每天可查,数据准确~
  18. 关系数据模型——三个组成部分
  19. 安卓手机卸载手机自带软件(adb)
  20. MySQL性能问题以及查找和处理

热门文章

  1. java基础数据结构查询最快_Java 数据结构快速入门
  2. pb公共变量怎么找_阿迪达斯的4D怎么就火不起来呢?
  3. 云服务器40g能装sqlserver_双十一买2核4G云服务器,哪里更便宜
  4. linux 更改文件和目录权限
  5. 大学学习论坛 需求分析报告.菜鸟版.多喷.看看问题所在
  6. 基础练习 十六进制转八进制 c语言
  7. Python装饰器学习(九步入门)
  8. OpenCV直线和圆形检测
  9. JSON入门之二:org.json的基本用法
  10. Chat:NLP 中文短文本分类项目实践(上)