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. LightOj_1284 Lights inside 3D Grid

    题目链接 题意: 给一个X * Y * Z 的立方体, 每个单位立方体内都有一盏灯, 初始状态是灭的, 你每次操作如下: 1)选择一个点(x1, y1, z1)     再选择一个点(x2, y2, ...

  2. BZOJ 4008 亚瑟王

    Description 小K不慎被LL邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂亮.众所周知,亚瑟王是一个看脸的游 ...

  3. NWERC 2012 Problem I Idol

    又是个2-sat的模板题: 反正评委的选择必须有一个是正确的,1错误,那么2就必须正确: 这就是一个2-sat问题. 直接上白书的模板啊,不过稍微要注意的一点是对于第一个点必须要选择,不然就违反了题意 ...

  4. Two shortest

    sgu185:http://acm.sgu.ru/problem.php?contest=0&problem=185 题意:找两条最短路径,没有边相交的最短路劲,并且输出路径. 题解:这一题和 ...

  5. Silicon Labs电容式触摸感应按键技术原理及应用

    市场上的消费电子产品已经开始逐步采用触摸感应按键,以取代传统的机械式按键.针对此趋势,Silicon Labs公司推出了内置微控制器(MCU)功能的电容式触摸感应按键(Capacitive Touch ...

  6. QVariant类学习(非常强大的类型,甚至能处理QMap<QString ,QVariant>)

    详细描述: QVariant类作为一个最为普遍的Qt数据类型的联合. 因为c++禁止没有构造函数和析构函数的联合体,许多继承的Qt类不能够在联合体当中使用.(联合体当中的变量共用一个存储区),没有了联 ...

  7. Research Papers

    NSE, $\bbu$ [Papers]NSE, $u$, Lorentz space [Sohr, JEE, 2001] [Papers]NSE, $u$, Lorentz space [Bjorl ...

  8. 单位分配的IP地址和电脑主机绑定了,我想用设置一个无线路由器,让我的笔记本电脑和手机都能上网?

    单位分配的IP地址和电脑主机绑定了,我想用设置一个无线路由器,让我的笔记本电脑和手机都能上网?     配一个无线路由器就可以实现,将电脑IP配置成自动获取,找条网线一头插路由LAN口(路由器上有标明 ...

  9. suse linux 编译安装Apache时报“APR NOT FOUND”的解决方法

    今日编译apache时出错: #./configure --prefix……检查编辑环境时出现: checking for APR... noconfigure: error: APR not fou ...

  10. std::min 与std::max 的 Compiler Error C2780

    代码 #include<iostream>#include <algorithm> // std::min#undef minint main(){ float a =15.0 ...