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. Objective-C 入门(给新人的)

    http://www.hengxinsoft.com/2010/12/objective-c-%E5%85%A5%E9%97%A8%EF%BC%88%E7%BB%99%E6%96%B0%E4%BA%B ...

  2. Host group 信息

  3. new 与override 区别

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Newover ...

  4. 导航软件 CH Round #57 - Story of the OI Class

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2357%20-%20Story%20of%20the%20OI%20Class/导航软件 题解:刚开始看见题目, ...

  5. unity3d 制造自己的水体water effect(二)

    前篇:unity3d 制造自己的水体water effect(一) 曲面细分:Unity3d 使用DX11的曲面细分 PBR: 讲求基本算法 Unity3d 基于物理渲染Physically-Base ...

  6. Unity Twist Effect Black Hole

    Shader "Hidden/Twist Effect" {Properties { _MainTex ("Base (RGB)", 2D) = "w ...

  7. Unity之Avatar原理

    今天花了一些时间理了理Unity的动画系统. 之前给不同模型配动画时没怎么在意,只知道用Avatar可以让一个模型使用另一个模型的动画.由于用的基本上都是人物模型,基本上没出现什么错误. 不过在用到异 ...

  8. 把mysql 中的字符gb2312 改为gbk的方法

    第一步:查找mysql的字符: mysql> show variables like '%char%';+--------------------------+----------------- ...

  9. SonarQube4.4+Jenkins进行代码检查实例之二

    SonarQube4.4+Jenkins进行代码检查实例之二 SonarQube4.4+Jenkins进行代码检查实例之二

  10. 364. Nested List Weight Sum II

    这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...