使用 RabbitMQ 创建动态队列 [英] Dynamic queue creation with RabbitMQ

查看:131
本文介绍了使用 RabbitMQ 创建动态队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习 RabbitMQ 的各种拓扑,但是,我找不到任何对从生产者发出的动态队列创建(又名声明队列)的引用.这个想法是根据特定事件(例如 HTTP 请求)动态创建队列.该队列将是临时的,设置了 TTL,并以事件 ID 命名.然后,消费者可以订阅主题event.*"并合并与其相关的所有消息.

I've been learning RabbitMQ various topologies, however, I couldn't find any reference to dynamic queue creation (aka Declare Queue) emitted from a producer. The idea would be to create queues dynamically depending on a particular event (e.g a HTTP request). The queue would be temporary with a TTL set and named after the event ID. A consumer could then, subscribe to the topic "event.*" and merge all the messages related to it.

示例:

  1. 收到 HTTP POST创建用户"
  2. 生产者创建一个队列 user.ID
  3. 推送关于用户队列中的所有后续消息(例如添加用户名"、添加电子邮件"...)
  4. worker 被分配到一个随机队列user.*"并将所有内容合并到一个用户帐户中
  5. TTL 过期后队列自动删除

现在,这个场景是否适用于 RabbitMQ?

Now, is this scenario feasible with RabbitMQ ?

推荐答案

本质上,您想要做的是使用 RabbitMQ 来缓冲在一组队列中等待的消息(根据定义,这是消息队列系统所做的).:)

Essentially, what you want to do is use RabbitMQ to buffer messages waiting in a set of queues (which is what a message queuing system does by definition). :)

假设您知道消费方的队列是什么,您就不会遇到任何问题.没有生产者不能创建队列的限制.需要注意的是,当队列过期时,队列中的所有消息都会被丢弃(或者,可以将它们设置为死信队列).

Assuming you know what your queues are from the consuming side, you won't have any issues. There is no constraint that a producer can't create a queue. As a caveat, when queues expire, all messages in the queue are discarded (or optionally, they can be set to go to a dead-letter queue).

你试过什么代码?

进一步澄清后(根据您的评论)-您正在寻找通配符消费"与通配符发布.RabbitMQ 目前不支持这种拓扑(这篇文章 要求提供类似的功能.

Upon further clarification (from your comment) - you are looking for "wildcard consuming" vs wildcard publishing. RabbitMQ does not support such a topology at the present time (this post asks for a similar feature).

您需要做的是定期枚举队列(使用 RabbitMQ API);之后,您的应用程序可以决定要使用哪些应用程序.当队列被删除时,消费者自动关闭.

What you would need to do is periodically enumerate the queues (using the RabbitMQ API); following that, your app could decide which ones to consume from. When a queue is deleted, the consumer is automatically closed.

特别说明应该理解,这里问的是反模式.使用队列的系统的典型行为是根据内容将消息路由到队列.因此,一个适当编排的系统将有一组工人在一个或多个静态定义的队列上运行.不同的工人可能需要不同的队列,这取决于专业化.当一系列交互导致消息发布到队列时,分配给队列的工作人员将以先来先服务的方式处理消息(但是,作为 这篇文章 讨论了多个消费者无法保证订单).期望的系统行为随后出现为在队列上执行各种功能的工作人员的组合.

Special Note It should be understood that what is being asked here is an anti-pattern. The typical behavior of a system using queues is to route messages to queues based upon content. Thus, a properly-orchestrated system would have a set of workers operating on one or more statically-defined queues. Different workers may take different queues, depending upon specialization. When a series of interactions results in messages being published to the queue, the workers assigned to the queues will handle the messages in a first-come-first-served fashion (but, as this post discusses, order cannot be guaranteed with multiple consumers). The desired system behavior then emerges as a composition of workers performing various functions operating on queues.

这篇关于使用 RabbitMQ 创建动态队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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