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. JSP内置对象(上)

    在JSP中为了简化页面的开发提供了一些内置的对象.这些对象不需要由JSP的编写者通过new关键字实例化,他们都由容器实现和管理,在所有的JSP页面中都可以使用内置对象. JSP中共有9大内置对象: o ...

  2. Contest20140906 ProblemC 菲波拉契数制 DP

    C.菲波拉契数制时间:2s   内存:65536KB我们定义如下数列为菲波拉契数列:                    F (1) = 1                    F (2) = 2 ...

  3. tyvj 1150 绳子围点 Pick定理 防溢出策略

    P1150 - 绳子围点 From 332404521    Normal (OI)总时限:10s    内存限制:128MB    代码长度限制:64KB 背景 Background 最近小小鱼在研 ...

  4. [BZOJ 1502] [NOI2005] 月下柠檬树 【Simpson积分】

    题目链接: BZOJ - 1502 题目分析 这是我做的第一道 Simpson 积分的题目.Simpson 积分是一种用 (fl + 4*fmid + fr) / 6 * (r - l) 来拟合 fl ...

  5. Install php-mcrypt on CentOS 6

    http://stackoverflow.com/questions/17109818/install-php-mcrypt-on-centos-6

  6. UE编辑器加载格式化代码插件astyle

    UE 的格式化功能不强,自带的astyle版本陈旧,一般采用开源工具astyle来实现代码格式化. 1. 首先下载最新的astyle,因为ue自带的astyle版本太老,不支持空格.中文名等. 2. ...

  7. [LeetCode#212]Word Search II

    Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...

  8. vs未找到导入的项目,请确认 <Import> 声明中的路径正确

    当使用vs出现下列情况: D:\xxxx\Web\Web.csproj : error  : 无法读取项目文件“Web.csproj”. D:\xxxx\WebServiceManager\Web\W ...

  9. 【转】[WCF REST] 帮助页面与自动消息格式(JSON/XML)选择

    可以说WebHttpBinding和WebHttpBehavior是整个Web HTTP编程模型最为核心的两个类型,前者主要解决消息编码问题,而余下的工作基本上落在了终结点行为WebHttpBehav ...

  10. maya 操作自我整理(一)

    绘制曲线时的点的控制 当我们在使用CV Curve Tool或者EP Curve Tool创建NURBS曲线的过程中,按下"Insert"键,配合键盘上的上.下箭头方向键,可以自由 ...