RabbitMQ 使用自定义标头来存储消息参数 [英] RabbitMQ using custom headers to store message-parameters

查看:40
本文介绍了RabbitMQ 使用自定义标头来存储消息参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 RabbitMQ 的新手,有点迷失在文档中.

I'm new to RabbitMQ, and I'm somewhat lost in the documentation.

目前,作为一个例子,我正在尝试构建一个小型邮件服务来侦听队列,但我有点卡在应该将我的服务具有的参数(目的地、主题、...)

Currently, as an example, I'm trying to build a small mailer-service that listens to a queue, but I'm somewhat stuck on where I should put the parameters that my service has (destination, subject, ...)

我应该将它们放在某种编码格式 (json) 中,在我的消息中,还是应该使用标头构造,如下例所示:

Should I put them inside some encoded format (json), inside my messages, or should I use the header-construction, like the following example:

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

var properties = new BasicProperties();
properties.Headers = new Dictionary<string, object>();
properties.Headers.Add("destination", "matthias123@localhost");

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

使用标题是否提供额外的好处?例如,是否可以过滤发送到特定目的地的消息?

Does using the headers offer additional benefits? Like, for example, would it be possible to filter messages that are sent to a specific destination?

推荐答案

我不会为您尝试做的事情使用标题.在我看来,该信息属于邮件正文.

I wouldn't use headers for what you are trying to do. That information belongs in the body of the message, in my opinion.

这样看:

邮件正文应包含完成所请求工作所需的所有内容.在这种情况下,它将是发件人、主题、电子邮件内容等.

The body of the message should contain everything you need to complete the work requested. In this case, it would be the sender, subject, email content, etc.

另一方面,标头是关于 AMQP 消息的数据位,而不是消息内容.

Headers on the other hand, are bits of data about the AMQP message, not the message contents.

这里有很多潜在的混淆,你要做的工作是电子邮件".AMQP 消息和电子邮件消息之间的术语重叠过多.

There's a lot of potential confusion here with your work to be done being "email". Too much overlap in terminology between the AMQP message, and email message.

话虽如此,我将选择一个不同的工作示例:计算斐波那契数列.

That being said, I'll pick a different example of work to do: calculate the fibonacci sequence.

在这种情况下,您通过 rabbitmq 发送的消息将包含诸如预先计算多少斐波那契位置,然后再发送回多少.

In this case, the message you send across rabbitmq would contain something like how many places of fibonacci to calculate up front and then how many to send back, after that.

例如,您可能会发送这样的消息(在本例中为 json):

For example you might send a message like this (as json in this case):

{
  start: 1,
  take: 3
}

这应该产生 1, 1, 2 的结果,因为它从第一个位置开始并从序列中返回 3 个项目.

This should produce a result of 1, 1, 2 because it starts at the first position and returns 3 items from the sequence.

使用您的特定问题和逻辑:我应该将 starttake 属性放入消息的标题中吗?

Using your specific question and logic: should I put the start and take attributes into headers of the message?

没有.

如果我这样做了,那意味着我的消息是空的,因为关于要完成的工作的所有信息都包含在标题中.

If I did, that would mean my message is empty as all of the information about the work to be done would be contained in the headers.

当我这样看时它没有意义,因为现在没有要发送的消息......只有标题.

It doesn't make sense when I look at it this way because now there is no message to send... only headers.

另一方面,如果我将这两个数据点保留在消息正文中,则标头作为发送有关 AMQP 消息本身的元数据的一种方式变得更加有用...不是有关消息内容的信息,而是有关消息的想法的信息.

On the other hand, if I keep these two points of data in the message body, the headers become more useful as a way to send metadata about the AMQP message itself... Not information about the content of the message, but information about the idea of the message.

在这种情况下,我是说我想从斐波那契数列中返回项目.换句话说,我正在参与 RPC(远程过程调用)并期待返回值.

In this case I'm saying that I want to return items from the fibonacci sequence. In other words, I'm engaging in RPC (remote procedure call) and am expecting a return value.

AMQP 不直接支持返回值.然而,我能做的是将队列名称填充到标题中并将结果发送到该队列.然后请求斐波那契数的代码可以监听该队列并获取结果.

AMQP doesn't support return values directly. What I can do, however, is stuff a queue name into the headers and send the result to that queue. Then the code that requested the fibonacci numbers can listen to that queue and get the results.

所以我在发送消息时可能会这样做:

So I might do something like this when sending the message:

var properties = new BasicProperties();
properties.Headers = new Dictionary();
properties.Headers.Add("return-queue", "fibreturn");

在这里,我设置了一个返回队列"标头 - 有关消息的信息,或者在这种情况下请求信息 - 在标头内部.处理斐波那契数列的代码将读取此标头并将响应发送回此队列.

Here I'm setting up a "return-queue" header - information about the message, or request for information in this case - inside of the headers. The code that handles the fibonacci sequence will read this header and send the response back to this queue.

这是对标头的更好使用,因为它使标头存储有关消息的信息...在这种情况下,应将响应发送到何处.但是,标题不包含有关要完成的实际工作的信息.这些都直接存储在消息正文中.

This is a better use of headers, as it makes the header store information about the message... In this case, where the response should be sent. The headers don't contain information about the actual work to be done, though. That is all stored in the message body directly.

附:我故意不使用您通常应该使用的回复"属性来执行 RCP.我用这个作为一个例子来说明为什么你不应该把你的目的地"放在标题中.为了更好地实现斐波那契序列的想法,请参阅 RMQ 文档以及它如何正确使用回复"https://www.rabbitmq.com/tutorials/tutorial-six-dotnet.html

P.S. I am purposely not using the "reply-to" property like you normally should, to do RCP. I'm using this as an example of why you shouldn't put your "destination" in the headers. For a better implementation of the fibonacci sequence idea, see the RMQ docs and how it uses "reply-to" correctly https://www.rabbitmq.com/tutorials/tutorial-six-dotnet.html

这篇关于RabbitMQ 使用自定义标头来存储消息参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆