开始使用 Google Cloud Functions 和 MongoDB





0/5 (0投票)
本文将引导您使用 Node.js 设置一个连接到 MongoDB 的 Google Cloud Function。
无服务器架构正变得越来越流行,这是有充分理由的。以我的经验来看,基于容器的编排框架具有陡峭的学习曲线,并且对于大多数面向消费者的公司来说是过度的。对于 FaaS 架构,例如 AWS Lambda 和 Azure Functions,理论上您唯一需要的 DevOps 就是打包和上传您的应用程序。
本文将引导您使用 Node.js 设置一个连接到 MongoDB 的 Google Cloud Function。然而,无状态函数的一个主要限制是每次无状态函数运行时都需要建立单独的数据库连接,这会带来主要的性能损失。不幸的是,我还没有弄清楚如何在 Google Cloud Functions 中重用数据库连接,这适用于 IBM Cloud、Azure Functions 和 AWS Lambda 的技巧对于 Google Cloud Functions 不起作用。
Google Cloud Functions 中的 “Hello, World”
转到 Google Cloud Functions 登陆页面,然后单击“免费试用”。
单击左上角的汉堡菜单图标,然后在侧边栏中找到“Cloud Functions”链接,然后单击“创建函数”。
将您的函数命名为“hello-world”,并将“创建函数”表单中的其余选项保持不变。将“要执行的函数”保留为“helloWorld
”,因为它需要与您的代码导出的函数名称匹配。下面是您应该输入的代码,以便您可以确认您的函数运行的 Node.js 版本。
exports.helloWorld = (req, res) => {
res.send('Hello from Node.js ' + process.version);
};
单击“创建”并等待 Google 部署您的云函数。部署函数后,单击它以显示该函数的详细信息。
单击“触发器”选项卡以查找您的云函数的 URL。
复制该 URL 并使用 curl 运行您的云函数。
$ curl https://us-central1-test21-201718.cloudfunctions.net/hello-world Hello from Node.js v6.11.5 $
Google Cloud Functions 不允许您控制您运行的 Node.js 版本,它们当前运行 Node.js v6.11.5。在撰写本文时,您不能在 Google Cloud Functions 上本地使用 async/await。
连接到 MongoDB Atlas
单击函数详细信息中的“源”选项卡,然后单击“编辑”按钮。您会注意到您的源代码中有 2 个文件,其中一个是 package.json
。编辑 package.json
以匹配下面的代码。
{ "name": "sample-http", "version": "0.0.1", "dependencies": { "co": "4.6.0", "mongodb": "3.x" } }
重新部署后,Google Cloud 会自动为您安装 npm 依赖项。现在,更改您的 index.js
以匹配下面的代码,并将 uri
替换为您的 MongoDB Atlas URI。
const co = require('co'); const mongodb = require('mongodb'); const uri = 'ATLAS_URI_HERE'; exports.helloWorld = (req, res) => { co(function*() { const client = yield mongodb.MongoClient.connect(uri); const docs = yield client.db('test').collection('tests').find().toArray(); res.send('Result: ' + JSON.stringify(docs)); }).catch(error => { res.send('Error: ' + error.toString()); }); };
单击“保存”以部署您的函数。部署完成后,您应该能够 curl 您的云函数并从 MongoDB Atlas 获取一个文档。
$ curl https://us-central1-test21-201718.cloudfunctions.net/hello-world Result: [{"_id":"5a7b7df69d07887542605888","name":"Hello!","__v":0}] $
此时,您可以尝试使用与 AWS Lambda 和其他云函数提供程序相同的全局状态技巧来重用数据库连接。
const co = require('co'); const mongodb = require('mongodb'); const uri = 'ATLAS_URI_HERE'; // Other cloud providers would retain this, but not Google Cloud let client = null; exports.helloWorld = (req, res) => { co(function*() { const reusedConnection = client != null; if (client == null) { client = yield mongodb.MongoClient.connect(uri); } const docs = yield client.db('test').collection('tests').find().toArray(); res.send('Result: ' + JSON.stringify(docs) + ', Reused: ' + reusedConnection); }).catch(error => { res.send('Error: ' + error.toString()); }) };
不幸的是,全局状态技巧在 Google Cloud 中似乎不起作用。
$ curl https://us-central1-test21-201718.cloudfunctions.net/hello-world Result: [{"_id":"5a7b7df69d07887542605888","name":"Hello!","__v":0}], Reused: false $ $ curl https://us-central1-test21-201718.cloudfunctions.net/hello-world Result: [{"_id":"5a7b7df69d07887542605888","name":"Hello!","__v":0}], Reused: false $
继续
FaaS 是一种强大的范例,但纯粹的无状态函数在处理数据库时会受到性能限制,因为建立新的数据库连接的成本很高。大多数云函数提供程序都有一种在函数调用之间保留数据库连接的机制,但显然 Google Cloud Functions 没有。这严重限制了 Google Cloud Functions 作为传统 Web 服务器替代品的能力。