RabbitMQ是开源消息队列服务器,实现了AMQP协议。提供了集群等特性。
官方文档:
https://www.rabbitmq.com/getstarted.html
RabbitMQ包含四个主要部分:
Publisher消息发布者
Exchange交换机 将消息发给队列
Queue队列
Consumer消息接受者
python 客户端pika安装:
pip install pika
例子
1.一对一简单实现:
send.py:
#!/usr/bin/python
#-*- coding: utf-8 -*-
import pika
#If we wanted to connect to a broker on a different machine we'd simply specify its name or IP address here.
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
#创建连接
channel = connection.channel()
#create a company queue to which the message will be delivered:
channel.queue_declare(queue='company')
channel.basic_publish(exchange='',#交换机种类
routing_key='company',
body='holiday news')
print('holiday news')
connection.close()
receive.py:
#!/usr/bin/python
#-*- coding: utf-8 -*-
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
#申明队列,没有就创建
channel.queue_declare(queue=