简体   繁体   English

Rabbit Mq,在发布者之前启动消费者

[英]Rabbit Mq, Starting consumer before publisher

I have created two separate application for publisher and consumer that send and receives message from the common queue.我为发布者和消费者创建了两个单独的应用程序,它们从公共队列发送和接收消息。 I have started consumer application first and the exchange was not declared yet (It has been declared and bind to queue on publisher) that's why I got the error.我首先启动了消费者应用程序,但尚未声明交换(它已声明并绑定到发布者上的队列),这就是我收到错误的原因。

So my question is,所以我的问题是,

  1. Is it a good idea to declare and bind exchange to queue on consumer?将交换声明并绑定到消费者队列是个好主意吗?
  2. In case of declaring exchange in consumer, should consumer know about the exchange properties and exchange type.在消费者中声明交换的情况下,消费者是否应该知道交换属性和交换类型。 Here in my case, consumer only knows the exchange,queue,route to receive message from the particular queue.在我的例子中,消费者只知道从特定队列接收消息的交换、队列、路由。

First it is important to note that starting a consumer and consuming from an existing queue should not give you any error even it the queue is not bound to any exchange.首先,重要的是要注意,即使队列未绑定到任何交换,启动消费者并从现有队列消费也不应该给您任何错误。 It is not necessary for a queue to be bound to an exchange, it can exist on its own.队列不必绑定到交换,它可以独立存在。

To answer your questions: This depends on your use case.回答您的问题:这取决于您的用例。 It may be OK for the consumer to create the queue, create the exchange and then bind the queue to the exchange.消费者创建队列,创建交换然后将队列绑定到交换可能是可以的。 This allows the consumer to have control which messages are routed to the queue and can be consumed by him.这允许消费者控制将哪些消息路由到队列并可由他使用。 If the consumer is the party that should execute this control, this is fine.如果消费者是应该执行此控制的一方,这很好。 But if the use case indicates that another party but the consumer shall control the routing, this other party shall create the exchange and the binding.但是,如果用例表明除消费者之外的另一方应控制路由,则该另一方应创建交换和绑定。

Consider a topology where there are only simple exchanges bound to queues.考虑一个拓扑,其中只有简单的交换绑定到队列。 In such a topology there would be only bindings from exchanges to queues but none from exchanges to exchanges.在这样的拓扑中,只有从交换机到队列的绑定,而没有从交换机到交换机的绑定。 Such a topology can be created by the consumer.这种拓扑可以由消费者创建。

But consider a different topology with two levels of exchanges.但是考虑具有两级交换的不同拓扑。 The exchanges on the lower level are bound to queues, this is similar to the first topology.较低级别的交换绑定到队列,这类似于第一个拓扑。 But above this lower level there is a higher level of exchanges which are only bound to exchanges on the lower level.但是在这个较低级别之上有更高级别的交换,这些交换只绑定到较低级别的交换。 The exchanges on the higer level distribute messages based on rules that are not related to concrete consumers.更高级别的交换根据与具体消费者无关的规则分发消息。 In fact, the two levels of exchanges could exist without any queues and consumers.事实上,这两个级别的交换可以在没有任何队列和消费者的情况下存在。 The creation of the exchanges and bindings in this topology can not be done by a consumer.消费者无法在此拓扑中创建交换和绑定。

A consumer could become a part of the second topology by declaring a queue for himself, binding this queue to the exchanges on the lower level he is interested in, and consume from the queue.消费者可以通过为自己声明一个队列,将这个队列绑定到他感兴趣的较低级别的交换,并从队列中消费,从而成为第二个拓扑的一部分。 The consumer would not create any exchanges, he would just bind his queue to them.消费者不会创建任何交换,他只是将他的队列绑定到它们。

So to sum up: In trivial scenarios, it does not matter who declares exchanges, queues and bindings, as long as everything is done in the right order.所以总结一下:在琐碎的场景中,谁声明交换、队列和绑定并不重要,只要一切都按照正确的顺序进行。 But on more complex scenarios, the responsibility should by spread between the RabbitMQ admin, produces and consumers.但是在更复杂的场景中,责任应该在 RabbitMQ 管理员、生产者和消费者之间分散。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM