博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fcm和firebase_Firebase Cloud功能:出色,出色和丑陋
阅读量:2527 次
发布时间:2019-05-11

本文共 10177 字,大约阅读时间需要 33 分钟。

fcm和firebase

by Pier Bover

通过Pier Bover

Firebase Cloud功能:出色,出色和丑陋 (Firebase Cloud Functions: the great, the meh, and the ugly)

When last year, I complained that it wasn’t completely serverless. A Node server was still needed for common functionalities such as sending emails or creating thumbnails.

去年,当 ,我抱怨说它并非完全没有服务器。 常见功能(例如发送电子邮件或创建缩略图)仍需要Node服务器。

Firebase Cloud Functions were announced a few months later. The service is still in beta, but I’ve been using it happily for a couple of months in production.

几个月后宣布了Firebase Cloud Functions。 该服务仍处于测试阶段,但是我已经愉快地使用了几个月。

Let’s see how it’s doing.

让我们看看它的运行情况。

什么是Firebase Cloud功能? (What are Firebase Cloud Functions?)

If you’ve never heard of cloud functions before, the concept is quite straightforward. Deploy concise logic to a server in the form of functions and some diligent elves can be magically invoked from limbo to do a task for you. All of this without caring about infrastructure and paying only for execution resources.

如果您以前从未听说过云功能,那么这个概念非常简单。 以功能的形式将简洁的逻辑部署到服务器,一些勤奋的精灵可以从地势上神奇地调用来为您完成任务。 所有这些都无需关心基础架构,而只为执行资源付费。

In many cases, this new paradigm can simplify writing, maintaining, and running backend code.

在许多情况下,这种新范例可以简化编写,维护和运行后端代码。

Firebase Cloud Functions in particular are like Lego blocks that you can connect to any Firebase service. For example, a function can be triggered when an image is uploaded to Firebase Storage to create a thumbnail, or maybe clean some user data when a node is deleted in the Realtime Database. Pretty much anything of interest that happens in Firebase can trigger a function.

特别是Firebase Cloud Functions,就像Lego块一样,您可以连接到任何Firebase服务。 例如,可以在将图像上载到Firebase存储以创建缩略图时触发功能,或者在实时数据库中删除节点时清除某些用户数据。 Firebase中发生的几乎所有有趣的事情都可以触发功能。

If that isn’t enough, you can also use HTTP to trigger functions with GET, POST, etc. Check out this amazing video on how to combine Firebase Hosting with Cloud Functions to create a complete Express app:

如果这还不够,您还可以使用HTTP来触发具有GET,POST等功能。请观看以下精彩视频,了解如何将Firebase托管与Cloud Functions结合在一起以创建完整的Express应用:

最棒的 (The Great)

基础架构再简单不过了 (Infrastructure doesn’t get any easier than this)

Infrastructure is completely abstracted from you, much like the rest of Firebase. Every time a function is triggered, a new virtual server comes to life, does its job, and returns to limbo. Google Cloud’s magic will keep triggering your functions and scale infrastructure according to workload automatically.

基础架构完全像Firebase的其余部分一样从您那里抽象出来。 每次触发功能时,都会有一个新的虚拟服务器运行起来,完成其工作,然后恢复正常。 Google Cloud的魔力将不断触发您的功能,并根据工作量自动扩展基础架构。

价钱 (Pricing)

Cloud functions in general are very cost-effective. It’s difficult to compare pricing of cloud providers, but I can say that based on my experience, Firebase Cloud Functions have been ridiculously cheap. It’s hard to believe Google is making any money out of this.

通常,云功能具有很高的成本效益。 很难比较云提供商的价格,但是我可以说,根据我的经验,Firebase Cloud Functions便宜得可笑 。 很难相信Google会从中赚钱。

易于使用 (Easy to use)

As usual with Firebase and Google, the docs are great and you won’t be making mental acrobatics to get it. There are also to get you started. Deployment auth is handled by the Firebase CLI, so getting a hello world up and running is literally:

像往常一样,用火力地堡和谷歌的文档是伟大的,你会不会做心理杂技得到它 。 上还有可以帮助您入门。 部署身份验证由Firebase CLI处理,因此从字面上看,您好可以开始建立世界:

firebase init functionsfirebase deploy

I think the simplicity of using Firebase and Google Cloud in general is just awesome, specially compared to the competition.

我认为,与竞争对手相比,总体而言使用Firebase和Google Cloud的简便性真是太棒了。

灵活 (Flexible)

Like I wrote before, these functions can be triggered by all sorts of events. I bet you will not run out of ideas on how to integrate them with your Firebase project or even the rest of your stack.

就像我之前写的那样,这些函数可以由各种事件触发。 我敢打赌,关于如何将它们与Firebase项目甚至堆栈的其余部分集成的想法,您将用完。

Here are some of problems we’ve solved using Firebase Cloud Functions:

以下是我们使用Firebase Cloud Functions解决的一些问题:

  • Generate PDFs for a online invoicing service using Phantom.js, and sign these invoices with some government service

    使用Phantom.js为在线发票服务生成PDF,并通过一些政府服务在这些发票上签名
  • Connect a Go service with a third party SOAP provider (ugh)

    将Go服务与第三方SOAP提供程序连接(UGH)
  • Send emails via HTTP from anywhere in our stack

    通过HTTP从我们堆栈中的任何地方发送电子邮件

h (The Meh)

冷启动 (Cold starts)

Scalability is great, but run time can fluctuate wildly. A simple hello world function can take 3ms to do its job, or a 100ms.

可伸缩性很好,但是运行时间会剧烈波动。 一个简单的hello world函数可能需要3毫秒或100毫秒才能完成工作。

functions.https.onRequest((request, response) => {    response.send(“Hello from Firebase!”);});

These fluctuations are caused by virtual server boot times. If the virtual server that is running your function is awake, the function will trigger instantly. But if the server has to be brought up from limbo, it will obviously need more time to start working. In the cloud functions lingo, this is referred to as warm and cold starts.

这些波动是由虚拟服务器启动时间引起的。 如果运行您的功能的虚拟服务器处于唤醒状态,则该功能将立即触发。 但是,如果必须将服务器从Hibernate状态中恢复过来,显然它将需要更多时间才能开始工作。 在云功能术语中,这称为冷启动。

In practice, you can’t rely on consistent response times unless you are caching your data, as described in the previous video, or use hacks to keep your functions warm.

在实践中,除非您按照上一个视频所述缓存数据,否则您将不能依靠一致的响应时间,或者使用黑客来使您的功能保持温暖。

Unfortunately cold starts are an unavoidable aspect of dealing with cloud functions (from any provider). You will have to take that into account when deciding to use a cloud function to solve something.

不幸的是,冷启动是处理云功能(来自任何提供商)不可避免的方面。 在决定使用云功能来解决问题时,必须考虑到这一点。

没有调度程序(cron) (No scheduler (cron))

Cloud functions are perfect for doing low traffic tasks like generating reports or doing periodic backups at 2am, but with Firebase or Google Cloud there is no easy way to trigger your functions based on a schedule.

云功能非常适合执行低流量任务,例如生成报告或在凌晨2点进行定期备份,但是对于Firebase或Google Cloud,没有一种简单的方法可以根据时间表触发功能。

The creating an App Engine project to orchestrate these triggers. The service really begs for something like the .

创建一个App Engine项目来协调这些触发器。 该服务实际上是针对 。

仅JavaScript (JavaScript only)

Eh, I’m ok with JavaScript, but both Azure and AWS support many more languages. It’s ironic that Google doesn’t support Go in its cloud function service, but AWS does.

恩,我可以使用JavaScript,但是Azure和AWS都支持更多语言。 具有讽刺意味的是,Google在其云功能服务中不支持Go,但AWS则支持。

节点6 (Node 6)

Again, the competition is doing better. Both AWS Lambda and Azure Functions are already running on Node 8. The biggest drawback here is going back to promises without async/await or having to configure Babel on your project.

再次,竞争表现更好。 AWS Lambda和Azure Functions都已在节点8上运行。最大的缺点是可以回到没有异步/等待或无需在项目上配置Babel的承诺。

丑陋的 (The Ugly)

开发工作流程 (Dev workflow)

With the exception of , you can’t run your functions locally. Functions triggered by a Firebase service have to be deployed to the cloud.

除了 ,您无法在本地运行函数。 Firebase服务触发的功能必须部署到云中。

This has many ugly implications:

这具有许多丑陋的含义:

  • Little mistakes end up costing a lot of time, since new functions take a couple of minutes to start working.

    小错误最终会花费大量时间,因为新功能需要花费几分钟才能开始工作。
  • Deployed functions have no obvious versions. All logs of the same function appear to be from the same version. It’s never clear when the new functions are actually working, so your only choice is to manually trigger the functions and See-What-Happens™.

    部署的功能没有明显的版本。 具有相同功能的所有日志似乎来自相同版本。 目前尚不清楚新功能何时真正起作用,因此您唯一的选择是手动触发功能和See-What-Happens™。
  • No rollbacks

    没有回滚

环境环境 (Environments)

On top of the previous points, managing environments is… complicated.

除了上述几点以外,管理环境还很复杂。

You can add environment variables to your functions projects using the but, like other aspects of Firebase, this is a naive approach that doesn’t scale well.

您可以使用将环境变量添加到函数项目中,但是与其他方面一样,这是一种天真的方法,无法很好地扩展。

You will need credentials to access pretty much anything outside of the Firebase sandbox. For other Google Cloud services, these credentials come in the form of .json files. Multiply that by every environment (dev, production, staging) and you can end up with a royal mess.

您将需要凭据才能访问Firebase沙箱之外的几乎所有内容。 对于其他Google Cloud服务,这些凭据以.json文件的形式出现。 将其乘以每个环境(开发,生产,暂存),最终可能会陷入混乱。

I ended up up manually renaming credential files before deploying, or worse, deploying all credentials and selecting the appropriate one at runtime. Please, let me know in the comments if you’ve found a way around this.

我最终在部署之前手动重命名了凭据文件,或更糟糕的是,在部署所有凭据并在运行时选择了合适的凭据之前。 如果您已找到解决方法,请在评论中让我知道。

I’d love to see an Environment tab in the Firebase Console where I could easily manage these settings for the whole Firebase project. Switching between environments should be as easy as firebase use production.

我很想在Firebase控制台中看到“ 环境”选项卡,可以在其中轻松管理整个Firebase项目的这些设置。 在环境之间切换应像firebase use production一样容易。

结论 (Conclusion)

Other than some friction during dev phase, my experience with Firebase Cloud Functions has been positive. Once deployed, these things are reliable and require zero maintenance as promised. So yes, Firebase is finally completely serverless. Hurrah!

除了在开发阶段遇到一些麻烦之外,我在Firebase Cloud Functions方面的经验非常积极。 一旦部署,这些东西就可靠了,并且如所承诺的那样需要零维护。 是的,Firebase最终完全没有服务器。 欢呼!

If you are already using Firebase, it’s really a no brainer. Firebase Cloud Functions are a great complement for your project, even if the service is still in beta.

如果您已经在使用Firebase,那真是不费吹灰之力。 即使该服务仍处于测试阶段,Firebase Cloud Functions还是该项目的绝佳补充。

On the other hand, it’s fair to say the competition has a more mature product. If you are not invested in Firebase or Google Cloud, and are considering using cloud functions in your stack, you should probably be looking into what AWS or Azure have to offer as well.

另一方面,可以说竞争产品更加成熟。 如果您不投资Firebase或Google Cloud,并且正在考虑在堆栈中使用云功能,那么您可能应该研究一下AWS或Azure还必须提供什么。

To be completely honest, I’m a bit concerned that the service is still in beta. It’s been over a year since it was announced and progress feels painfully slow. The competition seems far more committed to its cloud products, even if, according the Diane Greene, CEO for Google’s cloud businesses, Google Cloud is the .

老实说,我有点担心该服务仍处于测试阶段。 自宣布以来已经一年多了,进展缓慢。 竞争似乎更加致力于其云产品,即使Google云业务首席执行官Diane Greene认为Google Cloud是 。

That is all.

就这些。

Note: In a previous version of this article I claimed that it wasn’t possible to write tests for non HTTP functions. This is wrong, and on how to do that.

注意:在本文的先前版本中,我声称不可能为非HTTP函数编写测试。 这是错误的,这是有关如何执行 。

翻译自:

fcm和firebase

转载地址:http://sikzd.baihongyu.com/

你可能感兴趣的文章
springboot:session集中存储到redis
查看>>
《Python编程快速上手+让繁琐工作自动化》第12章实践项目:空行插入程序
查看>>
POJ 2986 A Triangle and a Circle(三角形和圆形求交)
查看>>
css3最新技术教程
查看>>
【tool】测试驱动开发全攻略
查看>>
VIM命令图---可定制版
查看>>
《坐热板凳》第八次团队作业:Alpha冲刺(第三天)
查看>>
关于wxWidgets
查看>>
codevs 1160 蛇形矩阵
查看>>
在outlook中查找Skype的聊天记录
查看>>
netsh命令
查看>>
nginx set变量后lua无法改值
查看>>
baseAdapter
查看>>
别让你妈知道!
查看>>
JAVA设计模式之迭代子模式
查看>>
Java程序生成exe可执行文件
查看>>
什么是blob,mysql blob大小配置介绍
查看>>
模运算的规则
查看>>
CSS样式布局入门介绍,非常详尽
查看>>
android app崩溃日志收集以及上传
查看>>