在 RabbitMQ 消费者上声明交换的目的

Purpose of declaring an exchange on a RabbitMQ consumer

我有一个连接到 RabbitMQ (3.2.4) 不可删除扇出交换的 Java 消费者应用程序,名为 "my_exhange_foo":

Connection connection = connectionFactory.newConnection(consumerPool);
Channel channel = connection.createChannel();
channel.exchangeDeclare("my_exhange_foo", "fanout"); // is this necessary?

String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, "my_exhange_foo", "");

QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);

无论是否声明交换,客户端消费者应用程序都会接收消息。

我遵循了本教程中的示例 ReceiveLogsDirect.java https://www.rabbitmq.com/tutorials/tutorial-four-java.html

并阅读了 api 但无法弄清楚在消费者方面声明交换的目的是什么。如果有人能对此有所了解,我将不胜感激。

what is the purpose of declaring the exchange on the consumer side?

它允许在生产者进程启动之前启动消费者进程。没有它,如果首先启动消费者,则会出错。在使用生产系统时,能够灵活地首先启动消费者非常有用,它减少了由重新启动系统的固有时间引起的可能问题。