RabbitMQ广播模式

广播模式: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()

原文地址:https://www.cnblogs.com/zqyanywn/p/10115478.html

时间: 2024-08-01 14:45:53

RabbitMQ广播模式的相关文章

uip UDP 服务器广播模式(客户端可以任意端口,并且主动向客户端发送数据)

目前移植uip,发现UDP 服务器模式下,必须指定本地端口以及客户端端口,否则只能讲客户端端口设置为0,才能接收任意端口的数据,但是无法发送数据,因为此时客户端端口设置为0了,我通过将原始数据包中的客户端端口保存下来,并且在发送的时候将客户端端口替换为指定的端口,发送完成之后又设置为0,这样就实现了向任意客户端端口发送数据. uip.c if(uip_udp_conn->lport != 0 && UDPBUF->destport == uip_udp_conn->lpo

Python-RabbitMQ direct广播模式

fanout广播模式是全部都能收到信息,那我要是想要有条件选择的接收呢,需要用到direct模式 这张图的大概意思是Exchange的类型为direct,发的error级别的消息投递到第一个队列,消息级别为info.error.warning级别的消息投递到第二个队列.先定义一个生产者 再定义消费者 进行测试打开级别级别为info.warning.error三个级别的消费者 在生产者端发送一个级别为error的消息 观察三个级别的消费者,最终只能级别为error的消费者能收到下消息 原文地址:h

Rabbitmq -Publish_Subscribe模式- python编码实现

what is Exchanges ?? Let's quickly go over what we covered in the previous tutorials: A producer is a user application that sends messages. A queue is a buffer that stores messages. A consumer is a user application that receives messages. The core id

使用rabbitmq rpc 模式

服务器端 安装 ubuntu 16.04 server 安装 rabbitmq-server 设置 apt 源 curl -s https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.python.sh | bash 使用 apt-get install rabbitmq-server 安装 rabbitmq 服务器 按键Y或者 y 确认安装 rabbitmq-server 简单管理 rabbitm

uip UDP server广播模式(client能够随意port,而且主动向client发送数据)

眼下移植uip,发现UDP server模式下,必须指定本地port以及clientport,否则仅仅能讲clientport设置为0,才干接收随意port的数据,可是无法发送数据,由于此时clientport设置为0了,我通过将原始数据包中的clientport保存下来,而且在发送的时候将clientport替换为指定的port,发送完毕之后又设置为0,这样就实现了向随意clientport发送数据. uip.c if(uip_udp_conn->lport != 0 && UDP

RabbitMQ事物模式

Rabbit的消息确认机制(事务+confirm)在rabbmitmq中我们可以通过持久化数据解决rabbitmq服务器异常的数据丢失问题问题:生产者将消息发送出去之后消息到底有没有到达rabbitmq服务器默认的情况是不知道的; 事物两种方式:AMQP实现了事务机制Confirm模式 事务机制txSelect.txCommit.txRollbacktxSelect:用户将当前 channel设置成transation横式txCommit:用于搜交事务txRollback:回滚事务 一.AMQ模

Rabbitmq -Routeing模式- python编码实现

(using the pika 0.10.0 Python client) In the previous tutorial we built a simple logging system. We were able to broadcast log messages to many receivers. In this tutorial we're going to add a feature to it - we're going to make it possible to subscr

广播模式下的生产者与消费者fanout模式

生产者 #coding=utf-8 import pika import sys connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) #建立socket连接可以加各种参数,端口,用户名,等 channel = connection.channel() #声明一个管道 channel.exchange_declare(exchange='logs',exchange_type='fanout')

RabbitMQ消息模式02

消费端限流 什么是消费端的限流? 假设一个场景,首先,我们RabbitMQ服务器有上万条未处理的消息,我们随便打开一个消费者客户端,会出现下面情况: 巨量的消息瞬间全部推送过来,但是我们单个客户端无法同时处理这么多数据! 消费端限流RabbitMQ提供的解决方案 RabbitMQ提供了一种qos(服务质量保证)功能,即在非自动确认消息的前提下,如果一定数目的消息(通过基于Consumer或者Channel设置Qos的值)未被确认前,不进行消费新的消息 Void BasicQos(uint pre