简体   繁体   English

RabbitMQ:直接回复?

[英]RabbitMQ: direct reply-to?

Is there a good example (C#) of how to do the direct reply-to in RabbitMQ? 是否有一个很好的例子(C#)如何在RabbitMQ中进行直接回复? What I want to do is for X Producers to post a message ("I've got some work for somebody") and I want one of X Consumers to pick it up, do the work and send the response back. 我想要做的是让X Producers发布一条消息(“我为某人做了一些工作”)我希望X消费者中的一个拿起它,完成工作并发回响应。 Not a basic Ack, but some data, the result of the calculation. 不是基本的Ack,而是一些数据,计算的结果。 Of course, the response has to go back to the right producer. 当然,回应必须回到正确的制作人。

Producer: 制片人:

    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello",
                                 durable: true,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

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

            var properties = channel.CreateBasicProperties();
            properties.Persistent = true;

            channel.BasicPublish(exchange: "",
                                 routingKey: "hello",
                                 basicProperties: properties,
                                 body: body);

            Console.WriteLine(" [x] Sent {0}", message);
        }
    }

Consumer: 消费者:

    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello",
                                 durable: true,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            };

            channel.BasicConsume(queue: "hello",
                                 noAck: false,
                                 consumer: consumer);

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }

It's not very clear from the minimal docs on how to set up both sides. 关于如何设置双方的最小文档并不是很清楚。 I know somebody has to do something with the "amq.rabbitmq.reply-to" queue, but its not clear which side and what they have to do with it. 我知道有人必须对“amq.rabbitmq.reply-to”队列做一些事情,但不清楚哪一方面以及它们与它有什么关系。

Have you seen the tutorials on the rabbitmq website? 你有没有看过rabbitmq网站上的教程? http://www.rabbitmq.com/tutorials/tutorial-six-dotnet.html http://www.rabbitmq.com/tutorials/tutorial-six-dotnet.html

You would set up your code the same way as the RPC example, above, with only a few minor differences (noted in the docs you've referenced: https://www.rabbitmq.com/direct-reply-to.html ). 您可以像上面的RPC示例一样设置代码,只有一些细微差别(在您引用的文档中注明: https//www.rabbitmq.com/direct-reply-to.html ) 。

When publishing a message from the original message producer, set the "replyTo" to amq.rabbitmq.reply-to 从原始消息生成器发布消息时,将“replyTo”设置为amq.rabbitmq.reply-to

Have the original message producer also be a message consumer, consuming from the amq.rabbitmq.reply-to queue 让原始消息生成器也是消息使用者,从amq.rabbitmq.reply-to队列消耗

When the code that handles the original request is done processing, you will publish a message from that worker, through the default (empty, no-name, "") exchange, with the routing key also set to amq.rabbitmq.reply-to 当处理原始请求的代码完成处理时,您将通过默认(空, amq.rabbitmq.reply-to ,“”)交换从该工作者发布消息,路由键也设置为amq.rabbitmq.reply-to

So: 所以:

  • client begins consuming messages from amq.rabbitmq.reply-to queue 客户端开始使用来自amq.rabbitmq.reply-to消息amq.rabbitmq.reply-to队列
  • client sends request for work, with amq.rabbitmq.reply-to as the replyTo property 客户端发送工作请求,使用amq.rabbitmq.reply-to作为replyTo属性
  • worker picks up message, does work, publishes a response through the "" exchange, using amq.rabbitmq.reply-to as the routing key 工作人员选择消息,工作,通过“”交换发布响应,使用amq.rabbitmq.reply-to作为路由密钥

that should be about it 这应该是关于它

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

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