广播模式:1对多,produce发送一则消息多个consumer同时收到。
注意:广播是实时的,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]\n" % 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]...\n" % 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()

最新文章

  1. BZOJ3670 [Noi2014]动物园
  2. [Leetcode] Repeated DNA Sequences
  3. *HDU 2108 计算几何
  4. mongoDB之Pipeline Aggregation Stages
  5. codevs1064 虫食算
  6. CSS系列:表达式(Expression)`淘汰`
  7. String性能优化
  8. hihoCoder #1174 : 拓扑排序·一 (判断循环图)
  9. HDU-4737 A Bit Fun 维护
  10. Cesium的api之关于viewer(二)
  11. Ubuntu 13.10 PHP 5.5.x mcrypt missing – Fatal Error: Undefined function mcrypt_encrypt()!
  12. API通常的url语法
  13. Vim的snipMate插件
  14. 配置Tomcat中的Context元素中的中文问题
  15. beginBackgroundTaskWithExpirationHandle
  16. php---数组序列化
  17. 清北学堂入学测试P4751 H’s problem(h)
  18. trie从入门到入殓
  19. LeetCode之“字符串”:最长回文子串
  20. 小白的python之路10/30 vim编辑器

热门文章

  1. awk:好用的数据处理工具
  2. Spring的2.5版本中提供了一种:p名称空间的注入(了解)
  3. iOS.Compiler
  4. springmvc 整合shiro
  5. Unable to load tag handler class "com.showId.Id.ShowId" for tag "ShowId:ShowId"] with root cause错误的解决方案
  6. java Exception 出错的栈信息打印到日志中 打印堆栈信息
  7. python 文件合并和编号
  8. [可用]android hack
  9. Java 原始类型JComboBox的成员JComboBox(E())的调用 未经过检查
  10. windows下命令提示符中有空格路径的解决方法