简体   繁体   English

如何向所有订阅者发送消息,除了发布者也是同一个rabbitMQ队列上的监听器

[英]how to send a message to all subscribers except for the publisher which is also a listener on same rabbitMQ queue

i have a rabbitMQ installed used by a nodeJS server. 我安装了一个由nodeJS服务器使用的rabbitMQ。 i use rabbit.js library to interact with rabbit, and so far i was happy with it. 我使用rabbit.js库与兔子互动,到目前为止我很满意。

i got multiple subscribers on the same queue in fanout mode, each node which is a subscriber, is also a publisher this is good for me and works fine, because there are many situations in which i want to notify all servers about some update that happend on one of those nodes (which is also a publisher...) 我在扇出模式下在同一队列上有多个订阅者,每个节点都是订阅者,也是一个发布者,这对我有好处并且工作正常,因为在很多情况下我想要通知所有服务器有关发生的一些更新在其中一个节点上(也是发布者...)

i stumbled on a case in which i need to send all of the listeners on a queue a message, except for the one who sent it (which is also a listener on that same queue). 我偶然发现了一个案例,其中我需要向队列中的所有侦听器发送一条消息,除了发送它的人(它也是同一队列上的侦听器)。

i don't know in advance who is listening (there could be single one, there could be millions), so i can't rout it to some specific nodes by some whitelist route rule. 我事先并不知道谁在听(可能有单一的,可能有数百万),所以我不能通过一些白名单路由规则将其路由到某些特定节点。 it must be some kind of an exclusion wildcard routing rule (some blacklist) for example, send this message to everyone who listens which don't correspond to my own unique id... 例如,它必须是某种排除通配符路由规则(某些黑名单),将此消息发送给所有侦听哪些不符合我自己的唯一ID的人...

can it be done using rabbit.js? 可以用rabbit.js完成吗? can it even be done in rabbitmq somehow? 甚至可以用rabbitmq做某事吗?

i don't know rabbit that much... so be gentle with me :) 我不太了解兔子......所以对我很温柔:)

btw, if you know how to do it using rabbit.js, even better... 顺便说一句,如果你知道如何使用rabbit.js,甚至更好......

EDIT:: 编辑::

according to Derick Bailey's request here is the reason i need this 根据Derick Bailey的要求,这就是我需要这个的原因

i have a system in which there are many load balanced nodeJS servers which operate as webServices. 我有一个系统,其中有许多负载平衡的nodeJS服务器作为webServices运行。 they are totally transparent to one another. 它们彼此完全透明。 none of them knows which other nodes exists. 他们都不知道存在哪些其他节点。 and i would like to keep it this way because this seperation makes it easier for me to better scale by just adding and removing other "parallel" nodes. 我想保持这种方式,因为这种分离使我更容易通过添加和删除其他“并行”节点来更好地扩展。

each of these nodes has his own in memory local cache service. 这些节点中的每一个都有自己的内存本地缓存服务。 and i stumbled on a situation in which a single node updates some entity. 我偶然发现了一个节点更新某个实体的情况。 and now i need to make it possible for this node to notify all of the other parallel nodes (that might have the same entity in the cache) to invalidate it. 现在我需要让这个节点通知所有其他并行节点(可能在缓存中有相同的实体)来使它无效。

problem is that my node which sends the message (updating node) will also receive the message, since he is also a listener. 问题是我发送消息的节点(更新节点)也会收到消息,因为他也是一个监听器。 so i want him to somehow exclude himself from the receivers list of that specific message... hence the need for some routing blacklist. 所以我希望他以某种方式将自己排除在该特定消息的接收者列表之外......因此需要一些路由黑名单。 (he knows himself, so i could make him route to everyone except for his own id... but he does not know even if someone is indeed listening on the other end... so it defenitly cannot be a whitelist) (他知道自己,所以除了他自己的身份之外,我可以让他路过所有人...但他不知道即使有人确实正在听另一端......所以它的防守不能成为白名单)

hope my need is clearer now. 希望我的需要现在更清楚了。

i already thought of a solution to my problem, but it requires extra development on my side, and i would like to avoid it by using the current abilities of rabbit (in case it is possible) i could just add a unique ID to the content of the message. 我已经想到了我的问题的解决方案,但它需要我的额外开发,我想通过使用兔子的当前能力来避免它(如果可能的话)我可以只为内容添加一个唯一的ID的消息。 then the sending node can recognize that this message came from him and ignore that message. 然后发送节点可以识别出该消息来自他并忽略该消息。 but as you can obviously understand, this could get tricky because i need to consider additional pitfalls and other edge cases it might fail... 但是你可以明白地理解,这可能会变得棘手,因为我需要考虑额外的陷阱和其他可能失败的边缘情况......

if someone can tell me how to do it using some rabbit existing configuration, i'll be more than glad to hear how :) 如果有人可以告诉我如何使用一些兔子现有的配置,我会很高兴听到如何:)

this should be possible... but it might be a bit tricky, or require a setup different than what you're currently doing. 这应该是可能的......但它可能有点棘手,或者需要与您目前正在进行的设置不同的设置。

one thing to think about: using a fanout exchange means all of the queues that are bound will receive the message. 需要考虑的一件事:使用扇出交换意味着所有绑定的队列都将收到消息。 if you need to prevent some queues from getting the message, a fanout is probably a bad idea. 如果你需要阻止某些队列获取消息,那么扇出可能是一个坏主意。

this largely depends on the messages being sent, though. 这在很大程度上取决于发送的消息。 if there is a specific type of message that needs to be handled differently, then you may just need a different exchange / queue binding for that type of message. 如果存在需要以不同方式处理的特定类型的消息,那么您可能只需要针对该类型的消息使用不同的交换/队列绑定。

as for black-listing... i don't know if that's possible. 黑名单......我不知道这是否可能。 topic exchanges allow whitelisting of routing key components. 主题交换允许路由关键组件的白名单。

for example, a binding of foo.*.bar will allow any routing key that starts with "foo.", has any word in the middle, and ends with ".bar". 例如, foo.*.bar的绑定将允许任何以“foo。”开头的路由键,中间有任何单词,以“.bar”结尾。

to the best of my knowledge, there is no way to blacklist a routing key segment. 据我所知,没有办法将路由密钥段列入黑名单。

it seems like your best option is to re-think how you are configuring your system. 看起来您最好的选择是重新思考如何配置系统。

it would probably help if you posted an example of what you need to do... what type of message your dealing with, and why you have so many consumers, including the producer consuming the same messages. 如果你发布了一个你需要做什么的例子......你要处理什么类型的消息,以及为什么你有这么多消费者,包括生产者消费相同的消息,这可能会有所帮助。

-- -

ps I used rabbit.js for a while and found I didn't like it's limitations. ps我用了一段时间的rabbit.js,发现我不喜欢它的局限性。 I switched over to the wascally library and am building my own rabbus library for node on top of it. 我切换到了wascally库,并在它上面构建了我自己的rabbus库。 you might check them out... they're a bit more flexible and rabbus provides a lot of the same patterns out of the box. 你可能会把它们检查出来......它们有点灵活,而且rabbus提供了很多相同的模式。

Don't know why i'm answering this question (since i'm the one who asked it in the first place) i assume that my solution will help others. 不知道为什么我回答这个问题(因为我是第一个问这个问题的人)我认为我的解决方案会帮助别人。

anyway, i never really found a solution in entire amqp architechture. 无论如何,我从未在整个amqp架构中找到解决方案。 so i invented my own solution :) 所以我发明了自己的解决方案:)

what i did was to create an internal service on each node that sends those kind of "ham radio style" messages to all that care to listen but himself. 我所做的是在每个节点上创建一个内部服务,将那些“火腿无线电式”消息发送给所有关心听的人,但他自己。

this service adds a unique hash to each message that is sent on this exchange before it is passed to rabbitMQ. 此服务为传递给rabbitMQ之前在此交换机上发送的每条消息添加一个唯一的哈希值。 and save the hash inside a local collection. 并将哈希保存在本地集合中。

each node that receives a message on this exchange, first tests to see if he is the one who sent it (by validating if the message hash is available in his "sent message" queue). 在此交换机上接收消息的每个节点,首先测试是否是发送它的人(通过验证消息散列是否在他的“已发送消息”队列中可用)。

if he detects it in his collection, the local service ignores the message if he doesn't detect it, he does not ignore the message and passes it on the piece of code that should handle it 如果他在他的集合中检测到它,本地服务会忽略该消息,如果他没有检测到它,他不会忽略该消息并将其传递给应该处理它的代码片段

there's a single useless message that is sent there is a bit of a code overhead per each message sent on this queue 发送一条无用的消息,每个在此队列上发送的消息都会产生一些代码开销

but buttom line, it works like a charm 但是它的线条,它就像一个魅力

hope it helps someone 希望它可以帮到某人

i really believe that this "HAM radio style" exchange in which only the sender does not receive the message could be usefull 我真的相信这种只有发送者不接收消息的“HAM无线电式”交换可能是有用的

anyone thinks i should contact the people who design amqp architechture so they will add it? 有谁认为我应该联系设计amqp架构的人,以便他们添加它? :) :)

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

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