能否在RabbitMQ中将不同类型的交换绑定在一起?

问题描述 投票:0回答:2

我当前正在将消息发送到Topic交换机,在该交换机中,消息将根据匹配的路由键复制到队列中。我希望能够根据邮件头将其中一些邮件复制到其他使用者。是否可以通过将Headers交换绑定到Topic交换,然后根据传入的消息头将队列绑定到该交换,来实现此目的?

rabbitmq messaging amqp
2个回答
1
投票

实际上,可以通过交换来交换绑定,并且绑定不同类型的交换是很常见的。这也有它自己的优点

  • 交换到交换绑定在您可以设计,促进去耦和减少绑定流失的拓扑结构方面要灵活得多。
  • 交换到交换绑定据说重量很轻,因此有助于提高性能

下面显示一个非常简单的示例

enter image description here

    #!/usr/bin/env python
import pika
import sys


connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()


#Declares the entry exchange to be used by all producers to send messages. Could be external    producers as well
channel.exchange_declare(exchange='data_gateway',
            exchange_type='fanout',
            durable=True,
            auto_delete=False)

#Declares the processing exchange to be used.Routes messages to various queues. For internal use    only
channel.exchange_declare(exchange='data_distributor',
            exchange_type='topic',
            durable=True,
            auto_delete=False)

#Binds the external/producer facing exchange to the internal exchange
channel.exchange_bind(destination='data_distributor',source='data_gateway')

##Create Durable Queues binded to the data_distributor exchange
channel.queue_declare(queue='trade_db',durable=True)
channel.queue_declare(queue='trade_stream_service',durable=True)
channel.queue_declare(queue='ticker_db',durable=True)
channel.queue_declare(queue='ticker_stream_service',durable=True)
channel.queue_declare(queue='orderbook_db',durable=True)
channel.queue_declare(queue='orderbook_stream_service',durable=True)

#Bind queues to exchanges and correct routing key. Allows for messages to be saved when no  consumer is present
channel.queue_bind(queue='orderbook_db',exchange='data_distributor',routing_key='*.*.orderbook')
channel.queue_bind  (queue='orderbook_stream_service',exchange='data_distributor',routing_key='*.*.orderbook')
channel.queue_bind(queue='ticker_db',exchange='data_distributor',routing_key='*.*.ticker')
channel.queue_bind  (queue='ticker_stream_service',exchange='data_distributor',routing_key='*.*.ticker')
channel.queue_bind(queue='trade_db',exchange='data_distributor',routing_key='*.*.trade')
channel.queue_bind  (queue='trade_stream_service',exchange='data_distributor',routing_key='*.*.trade')

但是您需要注意的是,在发布消息时,需要指定适当的路由密钥。

channel.basic_publish(exchange='data_gateway',
                  routing_key=routing_key,
                  body=message,
                  properties=pika.BasicProperties(delivery_mode =2,))

1
投票

是,您将使用交换到交换绑定docs


[NOTE: RabbitMQ团队监视rabbitmq-users mailing list,并且有时仅在StackOverflow上回答问题。

© www.soinside.com 2019 - 2024. All rights reserved.