我们是否可以使用一项Google Cloud功能来收听多个主题? [英] Would it be possible that we have one Google Cloud function listen to multiple topics?

查看:72
本文介绍了我们是否可以使用一项Google Cloud功能来收听多个主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我们可以将Cloud Function设置为侦听一个主题,例如此处显示的图表.

我们可能有一个功能可以收听多个主题吗?

例如,我可以使用一个函数将错误发送给我.而且,如果我可以使用此功能来收听多个主题(每个主题都将报告一个特定的过程错误),则不必为每个主题都具有相同功能的每个功能都提供一个功能,请向我发送错误消息./p>

谢谢!

解决方案

每个PubSub主题都需要一个单独的触发器,但是可以轻松地调用一个简单的高阶函数来完成所有工作.

  exports.helloPubSub = functions.pubsub.topic('topic-name').onPublish((message)=> {pubsubHandler('主题名称',消息)});Exports.worldPubSub = functions.pubsub.topic('topic-name2').onPublish((message)=> {pubsubHandler('topic-name2',消息)});函数pubsubHandler(topic,message){...} 

So we can set up the Cloud Function to listen to one topic, like the graph showed here.

Would it be possible that we have one function listen to multiple topics?

For example, I could have a function email me the errors. And if I can have this function to listen to multiple topics (each topic will be reporting one particular process error), I don't have to have one function for each one topic which doing the same thing, email me the error message.

Thanks!

解决方案

You will need a separate trigger for each PubSub topic, but can easily then call a simple higher-order function to do all the work.

exports.helloPubSub = functions.pubsub.topic('topic-name').onPublish((message) => {
  pubsubHandler('topic-name', message)
});

exports.worldPubSub = functions.pubsub.topic('topic-name2').onPublish((message) => {
  pubsubHandler('topic-name2', message)
});

function pubsubHandler(topic, message) {
  ...
}

这篇关于我们是否可以使用一项Google Cloud功能来收听多个主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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