简体   繁体   English

为什么要在 RabbitMQ 中声明 Exchange?

[英]Why declare Exchange in RabbitMQ?

I am working on a project with RabbitMQ.我正在使用 RabbitMQ 进行一个项目。 My code is below.我的代码如下。

Producer:制作人:

public static void Main()
{
    var factory = new ConnectionFactory() { HostName = "localhost" };
    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare("hello", false, false, false, null);

            string message = "Hello World!";
            var body = Encoding.UTF8.GetBytes(message);

            channel.BasicPublish("", "hello", null, body);
            Console.WriteLine(" [x] Sent {0}", message);
        }
    }
}

Consumer with Exchange declared:具有 Exchange 的消费者声明:

public static void Main()
{
    var factory = new ConnectionFactory() { HostName = "localhost" };
    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.ExchangeDeclare("hello", "direct",false, false, false, null);
            channel.QueueDeclare("hello", false, false, false, null);

            var consumer = new QueueingBasicConsumer(channel);
            channel.BasicConsume("hello", true, consumer);

            Console.WriteLine(" [*] Waiting for messages." +
                                     "To exit press CTRL+C");
            while (true)
            {
                var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
            }
        }
    }

Consumer without Exchange declared:没有 Exchange 的消费者声明:

public static void Main()
{
    var factory = new ConnectionFactory() { HostName = "localhost" };
    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare("hello", false, false, false, null);

            var consumer = new QueueingBasicConsumer(channel);
            channel.BasicConsume("hello", true, consumer);

            Console.WriteLine(" [*] Waiting for messages." +
                                     "To exit press CTRL+C");
            while (true)
            {
                var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
            }
        }
    }

Both consumer code works well, so what's the main use of declaring exchange?两个消费者代码都运行良好,那么声明交换的主要用途是什么? I am confused.我很迷惑。 Can anyone clarify?任何人都可以澄清吗?

Publishing to queues lets you only implement basic publish-subscribe scenarios, where the producer and consumer use the exact queue.发布到队列允许您仅实现基本的发布订阅方案,其中生产者和消费者使用确切的队列。 In case of multiple consumers a single queue of messages is distributed between multiple consumers.在多个消费者的情况下,单个消息队列分布在多个消费者之间。

Publishing to exchanges lets you create complicated scenarios, because of routing between exchanges and queues.由于交换和队列之间的路由,发布到交换允许您创建复杂的场景。

For example, a fanout exchange routes messages to all bound queues.例如,扇出交换将消息路由到所有绑定队列。 This way, you can have one producer and multiple consumers and each message is copied to all bound queues independently and received independently.这样,您可以拥有一个生产者和多个消费者,并且每条消息都被独立复制到所有绑定队列并独立接收。

Another example of exchange, a topic exchange routes messages to bound queues based on routing key in a message and a pattern on a queue.交换的另一个示例,主题交换根据消息中的路由键和队列上的模式将消息路由到绑定队列。 This introduces an interesting possibility of tagging messages and delivering them conditionally.这引入了一种有趣的可能性,即标记消息并有条件地传递它们。

For a complete reference of exchange types and their profiles refer to the documentation:有关交换类型及其配置文件的完整参考,请参阅文档:

https://www.rabbitmq.com/tutorials/amqp-concepts.html https://www.rabbitmq.com/tutorials/amqp-concepts.html

https://www.rabbitmq.com/getstarted.html https://www.rabbitmq.com/getstarted.html

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

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