rabbitMQ实战(一)---------使用pika库实现hello world

2016-05-18 23:29 本站整理 浏览(267)
 
 

pika是RabbitMQ团队编写的官方Python AMQP库。需要先安装pika:pip3 install pika有较详细的注释,就不再详细说明了生产者代码:hello_world_producer.py:

import pika,sys

#connect to the rabbitmq,use the default vhost
credentials = pika.PlainCredentials("guest","guest")
conn_params = pika.ConnectionParameters("localhost",
credentials=credentials)
conn_broker = pika.BlockingConnection(conn_params) #get a channel used to communicate with the rabbitmq
channel = conn_broker.channel() #declare a exchange
channel.exchange_declare(exchange='hello-exchange',
type='direct',
passive=False, #if the exchange already existes,report a error.It means we want to declare an exchange.
durable=True, #durable the message
auto_delete=False) #if the last consumer is over,do not delete the exchange auto #create a message
msg = sys.argv[1]
msg_props = pika.BasicProperties()
msg_props.content_type = "text/plain"
#publish the message
channel.basic_publish(body=msg,
exchange='hello-exchange',
properties=msg_props,
routing_key='hola')

消费者代码
hello_world_consumer.py:
import pika

#connect to the rabbitmq,use the default vhost
credentials = pika.PlainCredentials("guest","guest")
conn_params = pika.ConnectionParameters("localhost",
credentials=credentials)
conn_broker = pika.BlockingConnection(conn_params) #get a channel used to communicate with the rabbitmq
channel = conn_broker.channel() #declare a exchange
channel.exchange_declare(exchange='hello-exchange',
type='direct',
passive=False, #if the exchange already existes,report a error.It means we want to declare an exchange.
durable=True, #durable the message
auto_delete=False) #if the last consumer is over,do not delete the exchange auto #declare a queue
channel.queue_declare(queue="hello-queue") #bind queue to an exchange
channel.queue_bind(queue='hello-queue',
exchange='hello-exchange',
routing_key='hola') #define the consumer method to consumer message from a queue
def msg_consumer(channel,method,header,body):
channel.basic_ack(delivery_tag=method.delivery_tag)
if body.decode("ascii") == "quit":
channel.basic_cancel(consumer_tag='hello-consumer')
channel.stop_consuming()
else:
print(body)
return
#subscribe message
channel.basic_consume(msg_consumer,
queue='hello-queue',
consumer_tag='hello-consumer')
#begin loop until a quit message is sent
channel.start_consuming()

运行代码:
需要先运行consumer,因为我们是在消费者中创建队列的,如果先生产消息,由于没有可以路由到的队列,消息会被丢弃。
$ python hello_world_consumer.pyb'good'b'hello world'
$ python hello_world_producer.py "good"$ python hello_world_producer.py "hello world"$ python hello_world_producer.py "quit"

rabbitMQ实战(一)---------使用pika库实现hello world的更多相关文章

  1. python采用pika库使用rabbitmq总结,多篇笔记和示例

    这一段时间学习了下rabbitmq,在学习的过程中,发现国内关于python采用pika库使用rabbitmq的资料很少,官网有这方面的资料,不过是都英文的.于是笔者结合自己的理解,就这方面内容写了一 ...

  2. python采用pika库使用rabbitmq总结,多篇笔记和示例(转)

    add by zhj:作者的几篇文章参考了Rabbitmq的Tutorials中的几篇文章. 原文:http://www.01happy.com/python-pika-rabbitmq-summar ...

  3. go-micro集成RabbitMQ实战和原理

    在go-micro中异步消息的收发是通过Broker这个组件来完成的,底层实现有RabbitMQ.Kafka.Redis等等很多种方式,这篇文章主要介绍go-micro使用RabbitMQ收发数据的方 ...

  4. Java SpringBoot集成RabbitMq实战和总结

    目录 交换器.队列.绑定的声明 关于消息序列化 同一个队列多消费类型 注解将消息和消息头注入消费者方法 关于消费者确认 关于发送者确认模式 消费消息.死信队列和RetryTemplate RPC模式的 ...

  5. websocket+rabbitmq实战

    1. websocket+rabbitmq实战 1.1. 前言   接到的需求是后台定向给指定web登录用户推送消息,且可能同一账号会登录多个客户端都要接收到消息 1.2. 遇坑 基于springbo ...

  6. celery+RabbitMQ 实战记录2—工程化使用

    上篇文章中,已经介绍了celery和RabbitMQ的安装以及基本用法. 本文将从工程的角度介绍如何使用celery. 1.配置和启动RabbitMQ 请参考celery+RabbitMQ实战记录. ...

  7. RabbitMQ实战经验分享

    前言 最近在忙一个高考项目,看着系统顺利完成了这次高考,终于可以松口气了.看到那些即将参加高考的学生,也想起当年高三的自己. 下面分享下RabbitMQ实战经验,希望对大家有所帮助: 一.生产消息 关 ...

  8. 【RabbitMQ 实战指南】一 RabbitMQ 开发

    1.RabbitMQ 安装 RabbitMQ 的安装可以参考官方文档:https://www.rabbitmq.com/download.html 2.管理页面 rabbitmq-management ...

  9. 【RabbitMQ 实战指南】一 延迟队列

    1.什么是延迟队列 延迟队列中存储延迟消息,延迟消息是指当消息被发送到队列中不会立即消费,而是等待一段时间后再消费该消息. 延迟队列很多应用场景,一个典型的应用场景是订单未支付超时取消,用户下单之后3 ...

随机推荐

  1. 你一定要知道的关于Linux文件目录操作的12个常用命令

    写在前面: 1,<你一定要知道的关于Linux文件目录操作的12个常用命令>是楼主收集的关于Linux文件目录操作最常用的命令,包括文件或目录的新建.拷贝.移动.删除.查看等,是开发人员操 ...

  2. hadoop Yarn 编程API

    客户端编程库: 所在jar包: org.apache.hadoop.yarn.client.YarnClient 使用方法: 1 定义一个YarnClient实例: private YarnClien ...

  3. The Perfect Stall

    poj1274:http://poj.org/problem?id=1274 题意:有n个奶牛和m个谷仓,现在每个奶牛有自己喜欢去的谷仓,并且它们只会去自己喜欢的谷仓吃东西,问最多有多少奶牛能够吃到东 ...

  4. GConf 错误:联系配置服务器失败;某些可能原因是需要为 ORBit 启用 TCP/IP 联网

    http://blog.csdn.net/heirenheiren/article/details/8107294 http://blog.sina.com.cn/s/blog_8097f8aa010 ...

  5. StringGrid右击选中表格(发消息给句柄模拟点击,右键点击也是MouseDown)

    顺便还把单元格给变了: procedure TFGLGL.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShi ...

  6. TinyMCE logo 可视化HTML编辑器 TinyMCE

    TinyMCE是一个轻量级的基于浏览器的所见即所得编辑器,支持目前流行的各种浏览器,由JavaScript写成.功能配置灵活简单(两行代码就可以 将编辑器嵌入网页中),支持AJAX.另一特点是加载速度 ...

  7. AppDomain

    AppDomain http://www.cnblogs.com/xiashengwang/archive/2012/07/17/2594623.html http://www.cnblogs.com ...

  8. 疯狂VirtualBOX 实战讲学录:小耗子之VirtualBOX修炼全程重现

    疯狂VirtualBOX 实战讲学录:小耗子之VirtualBOX修炼全程重现 神级虚拟技术&云计算专家”小耗子”老师震撼分享 全球第—部完整深入的中文VirtualBox技术全程实战手册 全 ...

  9. LINUX6.3下RHCS的安装文档

    LINUX6.3下RHCS的安装及集群的配置文档 环境: 目前要给华为E6000系列的两个刀片安装RHCS,每一块刀片有两个业务网口和一个管理网口,但是看不见不物理网卡,而是连接到刀片自身携带的一个交 ...

  10. 浅谈Redis及其安装配置

    一.Redis的介绍 二.Redis的安装配置 三.Redis的配置文件说明 四.Redis的简单操作 简介: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型. ...