Cloud Functions和Firebase Functions有什么区别?

125

Cloud FunctionsFirebase Functions(或“Cloud Functions for Firebase”) 看起来很相似。请描述它们各自的用法。

它们都使用 HTTP 函数。

Cloud Functions中:

exports.helloHttp = function helloHttp (req, res) {
  res.send(`Hello ${req.body.name || 'World'}!`);
};

Firebase Functions 中:

exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello from Firebase!");
});

这些之间有什么区别?


2
Firebase的术语实际上是Cloud Functions for Firebase,这基本上只是将Cloud Functions与Firebase服务集成在一起。 - AL.
2
那么这两者之间没有任何区别吗? - Muhammad chhota
想添加一个简单的观点,不完全回答了你的问题。你可以使用 Google Cloud Functions 在不同的编程语言中编写代码(NodeJS、Python。听说 Go 即将推出)。 - viggy28
4个回答

229

没有名为Firebase Functions的产品。

实际上有三个不同的东西:

  1. Google Cloud Functions,允许您在响应事件时在Google基础设施中运行代码片段。
  2. Cloud Functions for Firebase,它可以基于Firebase中的事件(如数据库或文件写入、用户创建等)触发Google Cloud Functions。
  3. Firebase SDK for Cloud Functions,其中包括一个库(令人困惑地称为firebase-functions),您可以在函数代码中使用该库来访问Firebase数据(例如写入到数据库中的数据快照)。

因此,Firebase提供了一个(相对较薄的)封装层,以使Google Cloud Functions更易于使用,并将其与Firebase集成。在这方面,它类似于Firebase将Google Cloud Storage集成到“Cloud Storage for Firebase”(以前称为Firebase存储)中的方式。

如果您正在使用没有Firebase的Google云平台,则应使用普通的Google Cloud Functions。如果您在Firebase上或者是对Cloud Functions感兴趣的移动开发人员,您应该使用Cloud Functions for Firebase


3
FYI:Firebase 工具允许开发人员访问所有 Google Cloud 事件。"firebase-functions" SDK 和 Firebase CLI 相结合,通过简单的部署命令管理一组函数 -- 它易于入门,而当您需要时,您仍然可以完全访问 Google Cloud 平台。 - Ultrasaurus
1
价格不是有所不同吗?在 Firebase 上下文之外使用 Google Cloud Platform 功能为我提供了每月免费 5GB 的出站数据。从 Firebase 上下文中调用 GCP 函数会禁止访问免费层次中的非谷歌服务。在 $25 级别或 Blaze 级别(按使用量付费)允许外部网络访问,但即使在 Blaze 级别,您也需要支付每百万次调用 40 美分的费用,但通过 GCP,您的前两百万次调用是免费的,然后每百万次调用收取 0.40美元。 - mancini0
1
它们运行在完全相同的基础设施上,因此它们执行的方式不会有任何差异。在 Blaze 方案中,有相同的免费配额。来自定价页面:“在 Blaze 方案中,云函数提供永久的免费层。每个月提供前 2,000,000 次调用、400,000 GB-秒、200,000 CPU-秒和 5 GB 的 Internet egress 流量免费配额。超过这个免费配额后,您只需要按使用量付费。” - Frank van Puffelen
2
Firebase的Cloud Functions不支持使用Python编写函数,我说得对吗?根据“您需要一个Node.js环境来编写函数(...)”这句话。 - WJA
现在它可以做到:https://cloud.google.com/functions/docs/concepts/python-runtime - Frank van Puffelen

13

还有一个额外的区别:Firebase Functions 只能使用 JS 或 Node.JS 实现,而 Cloud Functions 还允许使用 Python 和 Go。

在 Spark Plan 的定价上也存在一些小差异。如果您使用 Spark Plan,请参考https://firebase.google.com/pricinghttps://cloud.google.com/functions/pricing。如果您使用 Blaze Plan,则价格相同。

我碰巧在我的 Firebase 项目中同时使用了两者。


8

谷歌云平台(Google Cloud Platform,GCP)有一篇文章解答了这个问题,Google Cloud Functions and Firebase

Google Cloud Functions和Firebase

Google Cloud Functions是谷歌的无服务器计算解决方案,用于创建事件驱动的应用程序。它是谷歌云平台团队和Firebase团队的联合产品。

对于谷歌云平台开发人员,Cloud Functions作为连接层,允许您通过监听和响应事件在Google Cloud Platform(GCP)服务之间编织逻辑。

对于Firebase开发人员,Cloud Functions for Firebase提供了一种通过添加服务器端代码来扩展Firebase行为并集成Firebase功能的方式。

这两个解决方案都在完全托管的环境中提供快速可靠的函数执行,您无需担心管理任何服务器或预配任何基础架构。

...

Cloud Functions for Firebase针对Firebase开发人员进行了优化:

  • Firebase SDK通过代码配置您的函数
  • 与Firebase控制台和Firebase CLI集成
  • 与Google Cloud Functions相同的触发器,以及Firebase实时数据库、Firebase身份验证和Firebase分析触发器

5

官方Google视频介绍区别:GCP和Firebase-函数和Firestore

  1. Firebase提供将您的功能包装成可通过Firebase SDK调用的可调用功能。
  2. 语言支持,GCP还支持Go、Python和Java。
  3. GCP可以通过控制台或CLI部署,但Firebase函数只能通过CLI部署。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接