需要安装erlang和RabbitMQ
1. 一对一
producer:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
# 声明queue
channel.queue_declare(queue='hello',durable=True) # durable=True,将队列持久化,哪怕 RabbitMQ服务重启,此队列依然存在
# RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!',
properties=pika.BasicProperties(delivery_mode=2) # 将队列中的消息也持久化
)
print(" [x] Sent 'Hello World!'")
connection.close()
consumer:
import pika
import time
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
# 为什么又声明一遍这个队列(生产者已经声明过了),因为如果消费者先运行起来,此时又没有声明的话,会报错,
# 所以在这里重新声明一遍,防止出错罢了。
channel.queue_declare(queue='hello',durable=True) # durable=True,将队列持久化,哪怕 RabbitMQ服务重启,此队列依然存在
def callback(ch, method, properties, body):
print('-->',ch,method,properties)
# time.sleep(30)
print(" [x] Received %r" % body)
ch.basic_ack(delivery_tag=method.delivery_tag) # 手动应答,只有消费者确认了,才会从队列删除
channel.basic_qos(prefetch_count=1) # 只要一个消费者的队列中还有1个消息,就不继续推送给这个消费者
channel.basic_consume('hello',
callback,
# auto_ack=True # 自动应答,消费者一收到消息,消息就自动从队列中删除,如果消费者没有处理完此消息,也会删除。
)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
2. 一对多广播:fanout
producer:
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', # 随意声明一个叫 logs 的 exchange
exchange_type='fanout') # 类型用fanout,可以对所有绑定此exchange的channel进行广播
message = "info: Hello World!"
channel.basic_publish(exchange='logs',
routing_key='', # 不设定routing_key,就是对所有绑定此exchange的channel进行推送消息
body=message)
print(" [x] Sent %r" % message)
connection.close()
consumer:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs',
exchange_type='fanout')
result = channel.queue_declare('',exclusive=True) # queue名字为空,rabbit会随机分配一个名字,exclusive=True会在使用此queue的消费者断开后,自动将queue删除
queue_name = result.method.queue
channel.queue_bind(exchange='logs', # 绑定名为logs的exchange
queue=queue_name)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] %r" % body)
channel.basic_consume(queue_name,
callback,
auto_ack=True)
channel.start_consuming()
3. 有选择的接受消息:direct
producer:
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
exchange_type='direct')
severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='direct_logs',
routing_key=severity,
body=message)
print(" [x] Sent %r:%r" % (severity, message))
connection.close()
consumer:
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs', # 声明一个exchange的名字和类型
exchange_type='direct')
result = channel.queue_declare('',exclusive=True)# 随机生成一个唯一的queue
queue_name = result.method.queue
severities = sys.argv[1:] # 从命令行获取参数
if not severities:
# sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
# sys.exit(1)
severities = ['info']
for severity in severities:
channel.queue_bind(exchange='direct_logs', # 绑定到direct_logs上
queue=queue_name,
routing_key=severity) # 指向相应的生产者上
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] %r:%r" % (method.routing_key, body))
channel.basic_consume(queue_name,
callback,
auto_ack=True)
channel.start_consuming()
3. 过滤特殊字段的消息:topic
producer:
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs',
exchange_type='topic')
routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='topic_logs',
routing_key=routing_key,
body=message)
print(" [x] Sent %r:%r" % (routing_key, message))
connection.close()
consumer:
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs',
exchange_type='topic')
result = channel.queue_declare('',exclusive=True)
queue_name = result.method.queue
binding_keys = sys.argv[1:]
if not binding_keys:
# sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])
# sys.exit(1)
binding_keys = ['anonymous.*'] # *代表匹配任意字符,# 代表匹配所有:binding_keys=['#','*.abc','aaa.*']
for binding_key in binding_keys:
channel.queue_bind(exchange='topic_logs',
queue=queue_name,
routing_key=binding_key)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] %r:%r" % (method.routing_key, body))
channel.basic_consume(queue_name,
callback,
auto_ack=True)
channel.start_consuming()