在 Rabbit MQ 中切换路由 [英] Toggle routing in Rabbit MQ
问题描述
在 RabbitMQ 中,是否有一种方法可以像下面的设置那样原子地切换队列的绑定以进行交换.Q1 &Q2 交替绑定.
In RabbitMQ, is there a way to atomically toggle the binding of queues to exchange like the setup below. Q1 & Q2 are alternatingly bound.
X -> Q1
-> Q2
通过交替绑定,它会产生一个问题,即如果 Q1 &Q2 在切换过程中同时绑定,如果两者都没有,则丢失.要求是不丢失或重复消息.我意识到丢失消息的窗口会非常小,但我宁愿不处理随之而来的逻辑问题.
By binding on an alternating basis, it creates the problem that messages will be either duplicated if Q1 & Q2 are bound at the same time during a toggle or lost if neither are. The requirement is to not lose or duplicate messages. I realise the window for lost messages will be very small, but I'd rather not deal with the logic issues that ensue.
实际上,我想要实现的是一种从交换机 X 到队列(Q1 或 Q2)的路由形式,但不要让发布消息的应用程序关注这一点,该消息发布到交换机 X.
Effectively what I want to achieve is a form of routing from exchange X to a queue (Q1 or Q2), but not make this a concern of the application that publishes the message, which publishes to an exchange X.
实现这一点的一种方法是让一个进程从中间队列 Q 中消费消息,并通过为 Q1 和 Q2 分别提供自己的交换来实现该进程中的切换路由.
One way of achieving this is to have a process consuming message off a intermediate queue Q and implement the toggle routing in that process by giving Q1 and Q2 each an exchange of their own.
这让我觉得效率低下,并引入了更多的活动部件来出错.
This strikes me as inefficient and introduces more moving parts to go wrong.
另一种方法是编写一个自定义交换器,当没有队列(X -> Q1 或 X -> Q2)被绑定时充当缓冲区/队列,但我的 Erlang 不存在.
An alternate way would be to write a custom exchange that acts as a buffer/queue when no queues (X -> Q1 or X -> Q2) are bound, but my Erlang is non existent.
最简单的方法是在 X -> Q1 和 X -> Q2 之间切换绑定,但是(afaik)没有绑定的原子切换,消息可能会丢失(取消绑定 Q1,然后绑定 Q2)或重复(绑定 Q2,然后解绑 Q1).
The easiest way would be to toggle the bindings between X -> Q1 and X -> Q2, but (afaik) there is no atomic toggling of bindings, messages could either get lost (unbind Q1, then bind Q2) or duplicated (Bind Q2, then unbind Q1).
我能想到的唯一解决方法是在绑定切换之前暂时使交换 X 不可用(ACL?重命名?)并希望客户端处理错误情况.
The only workaround I can think of would be to temporarily make exchange X unavailable (ACL? rename?) prior to the binding toggle and hope the clients deal with the error conditions.
是否有一种开箱即用的方法可以干净地实现这一目标?如果没有,您会推荐什么方法?
Is there an out-of-the-box way of achieving this cleanly? If not, what approach would you recommend?
推荐答案
切换绑定可以在 替代交换 RabbitMQ 扩展:
Toggle bindings may be done with the help of Alternate Exchangs RabbitMQ extension:
- 创建额外的交换,比如
X-AE
并将Q2
绑定到X-AE
. - 将
X-AE
设置为 替代交易所 在X
交换(将所有消息(无法处理的消息)路由到Q2
队列). - 从
X
解除Q1
的绑定(消息将通过X-AE
流向Q2
).立> - 将
Q2
绑定到X
. - 从
X
交换中删除备用交换X-AE
策略. - 从
X-AE
解除绑定Q2
并移除X-AE
交换.
- Create extra exchange, say
X-AE
and bindQ2
toX-AE
. - Setup
X-AE
as an Alternate Exchange onX
exchange (to route all messages (that one that can't be handled) toQ2
queue). - Unbind
Q1
fromX
(messages from will flow toQ2
viaX-AE
). - Bind
Q2
toX
. - Remove alternate exchange
X-AE
policy fromX
exchange. - Unbind
Q2
fromX-AE
and removeX-AE
exchange.
请注意,如果您的 X
交换上有其他一些替代交换,则确切的步骤可能会有所不同,但想法仍然相同.
Note, if you have some other Alternate Exchanges on your X
exchange exact steps may vary, but the idea still the same.
这篇关于在 Rabbit MQ 中切换路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!