本文介绍了使用RabbitMQ和Python进行基于内容的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RabbitMQ和Python是否可以执行基于内容的路由?

Is it possible with RabbitMQ and Python to do content-based routing?

AMQP标准和RabbitMQ声称支持基于内容的路由,但是是否有任何库用于支持指定基于内容的绑定等的Python?

The AMQP standard and RabbitMQ claims to support content-based routing, but are there any libraries for Python which support specifying content-based bindings etc.?

我当前正在使用的库(py-amqplib )似乎仅支持带有简单模式匹配(#,*)的基于主题的路由。

The library I am currently using (py-amqplib http://barryp.org/software/py-amqplib/) seems to only support topic-based routing with simple pattern-matching (#, *).

推荐答案

答案是是,但还有更多...:)

The answer is "yes", but there's more to it... :)

我们首先同意基于内容的路由的含义。有两种可能的含义。有人说它是基于消息的 header 部分。其他人说它是基于消息的 data 部分。

Let's first agree on what content-based routing means. There are two possible meanings. Some people say that it is based on the header portion of a message. Others say it's based on the data portion of a message.

如果采用第一个定义,则或多或少是我们所做的假设:
数据在某个地方存在,并通过某些软件发送给AMQP经纪人。我们假定该软件对数据了解足够多,可以将键值(KV)对放入描述内容的消息的 header 中。理想情况下,发送者还是数据的产生者,因此它具有我们想要的尽可能多的信息。假设数据是图像。然后,我们可以让发送方将KV对放入消息标头中,如下所示:

If we take the first definition, these are more or less the assumptions we make:The data comes into existence somewhere, and it gets sent to the AMQP broker by some piece of software. We assume that this piece of software knows enough about the data to put key-value (KV) pairs in the header of the message that describe the content. Ideally, the sender is also the producer of the data, so it has as much information as we could ever want. Let's say the data is an image. We could then have the sender put KV pairs in the message header like this:

width=1024
height=768
mode=bw
photographer=John Doe

现在我们可以实现内容了-通过创建适当的队列进行基于路由的路由。假设我们要对黑白图像执行单独的操作,对彩色图像执行单独的操作。我们可以创建两个队列,一个队列使用 mode = bw 接收消息,另一个队列使用 mode = colour 接收消息。然后,我们有单独的客户端在这些队列上侦听。代理执行路由,并且我们的客户端中没有什么需要知道路由的。

Now we can implement content-based routing by creating appropriate queues. Let's say we have a separate operation to perform on black-and-white images and a separate one on colour images. We can create two queues, one that receives messages with mode=bw and another with mode=colour. Then we have separate clients listening on those queues. The broker performs the routing, and there is nothing in our client that needs to be aware of the routing.

如果采用第二种定义,我们将采用不同的假设。我们假设数据存在于某个地方,并通过某种软件发送到AMQP代理。但是,我们认为要求该软件在头标中填充KV对是不明智的。相反,我们要基于 data 本身做出路由决策。

If we take the second definition, we go from different assumptions. We assume that the data comes into existence somewhere, and it gets sent to AMQP broker by some piece of software. But we assume that it's not sensible to demand that that software should populate the header with KV pairs. Instead, we want to make a routing decision based on the data itself.

在AMQP中有两个选择:您可以决定可以为您的特定数据格式实现新的交换,也可以将路由委派给客户端。

There are two options for this in AMQP: you can decide to implement a new exchange for your particular data format, or you can delegate the routing to a client.

在RabbitMQ中,直接(一对一)扇出(1对N),标头(标头过滤的1对N)和主题(标头过滤的1对N)交换,但是您可以根据AMQP标准实现自己的交换。这将需要阅读大量RabbitMQ文档并在Erlang中实现交换。

In RabbitMQ, there are direct (1-to-1), fanout (1-to-N), headers (header-filtered 1-to-N) and topic (topic-filtered 1-to-N) exchanges, but you can implement your own according to the AMQP standard. This would require reading a lot of RabbitMQ documentation and implementing the exchange in Erlang.

另一个选择是用Python制作一个AMQP客户端,以监听特殊的内容路由队列。每当消息到达队列时,您的路由器-客户端都会接听消息,执行路由决策所需的一切,然后将消息发送回代理到合适的队列。因此,要实现上述情况,您的Python程序将检测图像是黑白图像还是彩色图像,并将其(重新)发送到黑白或彩色队列,其中一些合适的客户会接管。

The other option is to make an AMQP client in Python that listens to a special "content routing queue". Whenever a message arrives at the queue, your router-client picks it up, does whatever is needed to make a routing decision, and sends the message back to the broker to a suitable queue. So to implement the scenario above, your Python program would detect whether an image is in black-and-white or colour, and would (re)send it to a "black-and-white" or a "colour" queue, where some suitable client would take over.

因此,在第二个问题上,您的客户实际上没有做任何基于内容的绑定的操作。您的客户可以如上所述工作,或者您可以在RabbitMQ本身中创建新的交换类型。然后,在客户设置代码中,将交换类型定义为新类型。

So on your second question, there's really nothing that you do in your client that does any content-based binding. Either your client(s) work as described above, or you create a new exchange type in RabbitMQ itself. Then, in your client setup code, you define the exchange type to be your new type.

希望这能回答您的问题!

Hope this answers your question!

这篇关于使用RabbitMQ和Python进行基于内容的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:49