简体   繁体   English

如何在谷歌云中保存一些规则或配置,然后调用它来处理云函数中的发布订阅主题数据?

[英]How can I save some rules or config in google cloud and then call it to process a pubsub topic data in cloud functions?

I am working with google cloud functions.我正在使用谷歌云功能。 I am coming from a serverbased applications so I don't know much about the serverless world.我来自基于服务器的应用程序,所以我对无服务器世界了解不多。

I have a scenario where i have two kinds in google datastore ie iot_kind and config_kind.我有一个场景,我在谷歌数据存储中有两种,即 iot_kind 和 config_kind。 One kind has config which is like a lookup table and other kind has normal payload values.一种具有类似于查找表的配置,另一种具有正常的有效载荷值。

I am publishing the data from a IoT device into the pubsub topic and getting that data into the cloud functions.我正在将来自 IoT 设备的数据发布到 pubsub 主题并将该数据获取到云功能中。

eg IoT payload is例如物联网有效载荷是

{"id":"213213", 'price': 20, 'name':"some_name"}

then I am querying the config KIND and extracting some data from it like price multiplier然后我查询配置 KIND 并从中提取一些数据,如价格乘数

select * 
from config_kind 
where id = "213213" #lets say output is 2

Then I am multiplying the price*2 and saving it to IoT_kind eg value=40然后我乘以价格 * 2 并将其保存到 IoT_kind 例如 value=40

Now in this way I have to make a read every time I get a data into the cloud function which is i think quite expensive and also a bit processing.现在以这种方式,每次我将数据输入到云函数中时,我都必须进行读取,我认为这非常昂贵,而且处理起来也有点麻烦。

Is there any better way of doing it?有没有更好的方法来做到这一点? Like I save latest config somewhere and then whenever payload comes it sees the config and do the processing according to the config and then save it to a database?就像我将最新的配置保存在某处,然后每当有效负载到来时,它都会看到配置并根据配置进行处理,然后将其保存到数据库中?

And can I do something like make a cloud function and store the latest value there and then call this cloud function from another cloud function to get the values out of it?我可以做一些事情,比如创建一个云函数并在那里存储最新的值,然后从另一个云函数调用这个云函数以从中获取值吗? would it be less expensive or expensive ?它会更便宜还是更贵? Thanks a lot!非常感谢!

Cloud Function is a serverless and stateless service. Cloud Function 是一种无服务器和无状态服务。 When an instance is created, it lived for a while and it has been stopped.当一个实例被创建时,它存在了一段时间并且它已经停止了。 No disk are mount, all work in memory.没有磁盘都挂载,都在内存中工作。 That means that you can't save any value in it persistently.这意味着您不能持久地在其中保存任何值。

Anyway, you have 3 solutions to reduce the number of config read into datastore无论如何,您有 3 种解决方案来减少读入数据存储的配置数量

  • Even if you can't store persistently data, you can still store them in memory (up to 2Gb of memory, your application footpring included).即使您无法持久存储数据,您仍然可以将它们存储在内存中(最多 2Gb 内存,包括您的应用程序占用空间)。 Thereby, you have to perform the first request to Datastore an then, you can store the config data into a map defined as global variable.因此,您必须向 Datastore 执行第一个请求,然后,您可以将配置数据存储到定义为全局变量的映射中。 Because Cloud Functions can handle only 1 request in the same time you don't have race condition.因为 Cloud Functions 可以同时处理 1 个请求,所以您没有竞争条件。 (It's not the same thing with Cloud Run for example) (例如,这与 Cloud Run 不同)
  • If your config doesn't change often, you can store it into a file and store into Cloud Storage.如果您的配置不经常更改,您可以将其存储到文件中并存储到 Cloud Storage。 At the beginning of the function you load the file (and store it into a global variable as before, for reading it only once), and you perform you search into the file.在函数开始时,您加载文件(并像以前一样将其存储到全局变量中,以便仅读取一次),然后执行对文件的搜索。 That's work only if the config file size is bellow the function memory limit.仅当配置文件大小低于函数内存限制时才有效。
  • If the read cost is above 50$ per month, you can use Cloud SQL database instead of Datastore.如果每月读取成本超过 50 美元,您可以使用 Cloud SQL 数据库代替 Datastore。

Is that make sense for you?这对你有意义吗?

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

相关问题 如何从Google Cloud pubsub中获取数据到Firebase实时数据库 - How can I take data from google cloud pubsub to firebase realtime data base 谷歌云物联网 - 如何使用多个物联网设备发布到同一主题并通过云功能进行处理 - Google Cloud IoT- How to publish to the same topic with multiple IoT devices and process it by cloud functions Google Cloud Pubsub数据丢失了 - Google Cloud Pubsub Data lost 如何使用 Google Cloud Functions / Tasks / PubSub 进行批处理? - How to use Google Cloud Functions / Tasks / PubSub for Batch Processing? 如何使用PUBSUB作为触发器从Google Cloud函数返回JSON? - How to return a JSON from Google Cloud functions using PUBSUB as trigger? 在PubSub主题上使用Cloud Run - Using Cloud Run on a PubSub topic 在Compute Engine中使用Google Cloud Pubsub发布到主题 - Publish to a topic using Google Cloud Pubsub within Compute Engine 订阅PubSub主题的速率限制/限制Google云端功能 - Rate limit / throttle google cloud function that subscribes to a PubSub topic 通过 mosquitto 代理发布到谷歌云中不同的 pubsub 主题? - Publish to different pubsub topic in Google cloud via mosquitto broker? 如何仅使用主题名称(而不是订阅名称)订阅 Google Cloud pubsub? - How to subscribe to Google Cloud pubsub using Topic name alone (instead of subscription name)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM