rabbitMQ实战---------使用pika库实现hello world

Posted weiman3389

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rabbitMQ实战---------使用pika库实现hello world相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

rabbitmq(pika) 使用 RPC 时抛出异常

使用 pika 读取 rabbitmq 消息的 gzipped 有效负载

RabbitMQ 的 Pika 超时问题

python pika简单实现RabbitMQ通信

python 使用`pika.BlockingConnection`的RabbitMQ客户端,主要用于发布。

python采用pika库使用rabbitmq --工作队列