简体   繁体   English

跨多个 Cloud Functions 分发 1 个 Pub/Sub 主题消息而不重复

[英]Distribute 1 Pub/Sub Topic Messages across multiple Cloud Functions without repetition

I have 8 Cloud functions that are triggered by a Pub/Sub topic.我有 8 个由 Pub/Sub 主题触发的云函数。

Example problem: When 16 messages are published to the Pub/Sub topic, all 8 functions are triggered, and each of the 8 functions receives all 16 messages.示例问题:当 16 条消息发布到 Pub/Sub 主题时,将触发所有 8 个函数,并且 8 个函数中的每一个都接收到所有 16 条消息。

Desired outcome: When 16 messages are published to the Pub/Sub topic, all 8 functions are triggered, each of the 8 functions should receive only 2 messages.预期结果:当 16 条消息发布到 Pub/Sub 主题时,将触发所有 8 个函数,这 8 个函数中的每一个都应该只接收 2条消息。

Is there a way to solve this?有办法解决这个问题吗?

I tried: setup 1 pub/sub topic, 1 pub/sub subscription, trigger 8 functions from that 1 subscription, but in yaml documentation and in the cloud console it appears I can only have a function triggered from a topic, not an existing subscription.我试过:设置 1 个发布/订阅主题、1 个发布/订阅订阅、从 1 个订阅触发 8 个函数,但在 yaml 文档和云控制台中,我似乎只能从主题触发 function,而不是现有订阅.

Possible but not desired solution: Load balance the (example) 16 messages across 8 different pub/sub topics, then each function gets triggered by 1/8 topics (2 messages per topic).可能但不是理想的解决方案:在 8 个不同的发布/订阅主题中平衡(示例)16 条消息,然后每个 function 被 1/8 主题触发(每个主题 2 条消息)。

It's an anti-pattern to have 8 separate Cloud Functions over which you want to load balance messages.拥有 8 个独立的 Cloud Functions 是一种反模式,您希望在这些 Cloud Functions 上负载平衡消息。 If the code is identical for each one, then you should really only have a single Cloud Function and let its autoscaling behavior take care of turning up instances as needed to handle the incoming messages.如果每个代码都相同,那么您实际上应该只有一个 Cloud Function,并让它的自动缩放行为负责根据需要打开实例来处理传入的消息。

If the 8 Functions have different code, then it's counter-intuitive that you want each to only handle a subset of the messages unless you want them routed to the functions based on some property of the messages themselves, in which case you'd want to set up 8 subscriptions that use filters .如果 8 个函数有不同的代码,那么您希望每个函数只处理消息的一个子集是违反直觉的,除非您希望它们根据消息本身的某些属性路由到函数,在这种情况下您想要设置 8 个使用过滤器的订阅。

If the behavior you seek is truly what you need, then you may need to put a separate Cloud Function in between that receives all messages and distributes it to other Cloud Functions that instead of using Pub/Sub as a trigger, use an HTTP request.如果您寻求的行为确实是您所需要的,那么您可能需要在两者之间放置一个单独的 Cloud Function 来接收所有消息并将其分发到其他 Cloud Functions,而不是使用 Pub/Sub 作为触发器,而是使用 HTTP 请求。 Then, you could make the HTTP request from the single function that receives Pub/Sub messages and distribute them as needed across the other 8 functions.然后,您可以从接收 Pub/Sub 消息的单个 function 发出 HTTP 请求,并根据需要将它们分发给其他 8 个函数。

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

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