注意:广播是实时的,produce只负责发出去,不会管对端是否收到,若发送的时刻没有对端接收,那消息就没了,因此在广播模式下设置消息持久化是无效的。
三种广播模式:
fanout: 所有bind到此exchange的queue都可以接收消息(纯广播,绑定到RabbitMQ的接受者都能收到消息);
direct: 通过routingKey和exchange决定的那个唯一的queue可以接收消息;
topic:所有符合routingKey(此时可以是一个表达式)的routingKey所bind的queue可以接收消息;
原理:
fanout:接受者会在RabbitMQ中创建一个queue用来接收消息,发送者往queue中发送消息。(发送给全员)
direct:队列绑定关键字,发送者将数据根据关键字发送到消息exchange,exchange根据 关键字 判定应该将数据发送至指定队列。(发送给指定人)
topic:接受者可以收取指定内容的消息
代码饭粒1:fanout广播模式
# publisher发送者
import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host=‘localhost‘)) channel = connection.channel() channel.exchange_declare(exchange=‘broadcast‘, exchange_type=‘fanout‘) message = ‘big bang!‘ channel.basic_publish( exchange=‘broadcast‘, routing_key=‘‘, body=message, ) print("[v] Send %r" % message) connection.close()
# subscriber接收者
import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host=‘localhost‘)) channel = connection.channel() channel.exchange_declare(exchange=‘broadcast‘, exchange_type=‘fanout‘) result = channel.queue_declare(exclusive=True) # 不指定queue名字,Rabbit会随机分配一个名字,并在使用此queue的消费者断开后,自动将queue删除 queue_name = result.method.queue channel.queue_bind(exchange=‘broadcast‘,queue=queue_name) print(" [*] Waiting for broadcast. To exit press Ctrl+C") def callback(ch, method, properties, body): print(" [v] Get broadcast:",body) channel.basic_consume( callback, queue=queue_name, ) channel.start_consuming()
代码饭粒2:direct广播模式
# publisher发送者
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()
# subscriber接收者
import pika import sys connection = pika.BlockingConnection(pika.ConnectionParameters(host=‘localhost‘)) channel = connection.channel() channel.exchange_declare(exchange=‘direct_logs‘, exchange_type=‘direct‘) result = channel.queue_declare(exclusive=True) queue_name = result.method.queue severities = sys.argv[1:] if not severities: sys.stderr.write("Usage: %s [info] [warning] [error] " % sys.argv[0]) sys.exit(1) for severity in severities: channel.queue_bind( exchange=‘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( callback, queue=queue_name, no_ack=True ) channel.start_consuming()
代码饭粒3:topic广播模式
# publisher发送者
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()
# subscriber接收者
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]... " % sys.argv[0]) sys.exit(1) 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( callback, queue=queue_name, no_ack=True ) channel.start_consuming()