我在应用程序引擎上已经有了一个使用Mongo Atlas数据库的express服务器,一切都很好。
我想启动一个使用相同数据库并处理pubsub事件的云函数。但是,在部署函数时,我无法连接到数据库。
我尝试使用私有URI和公共URI。对于公共,我有一段时间设法连接,但这并不是所有的时间(我必须使集群上的所有IP都变白(。
这是功能代码(简化(
const path = require('path')
require('dotenv').config({ path: path.join(__dirname, '/.env') })
const config = require('./libs/config/config.js')
const mongoose = require('mongoose')
const sanitize = require('mongo-sanitize')
const Utils = require('./libs/services/utils')
// Mongo connection string
const mongo = config.get('db.protocol') + '://' + config.get('db.user') + ':' + config.get('db.password') + '@' + config.get('db.host') + '/' + config.get('db.name') + '?retryWrites=true&w=majority'
// Database connexion
mongoose.connect(mongo, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true }).then(() => {
console.log('Connected to mongoDB')
}).catch(e => {
console.log('Error while DB connecting')
console.log(e)
})
/**
* Background Cloud Function to be triggered by Pub/Sub.
* This function is exported by index.js, and executed when
* the trigger topic receives a message.
*
* @param {object} data The event payload.
* @param {object} context The event metadata.
*/
exports.scenarioManagerPushPubSub = (gcpEvent, context) => {
const event = Utils.parseEntryInfo(gcpEvent, context)
console.log('scenarioManager : ', event, event.constructor.name)
// my Function
}
我在GAE上的Express应用程序上使用了相同的连接字符串。
我激活了VPC无服务器,并将Mongo Atlas上的IP范围列入了白名单。Mongo Atlas和GCP之间的对等连接已经启动并运行。
我得到的错误:
A 2020-08-18T09:16:03.547Z scenarioManager-staging Error while DB connecting scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging { MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/ scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at NativeConnection.Connection.openUri (/workspace/node_modules/mongoose/lib/connection.js:830:32) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Mongoose.connect (/workspace/node_modules/mongoose/lib/index.js:335:15) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Object.<anonymous> (/workspace/smsGateway.js:16:10) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Module._compile (internal/modules/cjs/loader.js:778:30) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Module.load (internal/modules/cjs/loader.js:653:32) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at tryModuleLoad (internal/modules/cjs/loader.js:593:12) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Function.Module._load (internal/modules/cjs/loader.js:585:3) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Module.require (internal/modules/cjs/loader.js:692:17) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at require (internal/modules/cjs/helpers.js:25:18) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Object.<anonymous> (/workspace/index.js:1:20) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Module._compile (internal/modules/cjs/loader.js:778:30) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Module.load (internal/modules/cjs/loader.js:653:32) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at tryModuleLoad (internal/modules/cjs/loader.js:593:12) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging at Function.Module._load (internal/modules/cjs/loader.js:585:3) scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging message: scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging 'Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/', scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging reason: scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging TopologyDescription { scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging type: 'ReplicaSetNoPrimary', scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging setName: null, scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging maxSetVersion: null, scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging maxElectionId: null, scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging servers: scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging Map { scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging '*-shard-00-00-pri.xgtnk.gcp.mongodb.net:27017' => [ServerDescription], scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging '*-shard-00-01-pri.xgtnk.gcp.mongodb.net:27017' => [ServerDescription], scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging '*-shard-00-02-pri.xgtnk.gcp.mongodb.net:27017' => [ServerDescription] }, scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging stale: false, scenarioManager-staging
A 2020-08-18T09:16:03.547Z scenarioManager-staging compatible: true, scenarioManager-staging
A 2020-08-18T09:16:03.548Z scenarioManager-staging compatibilityError: null, scenarioManager-staging
A 2020-08-18T09:16:03.548Z scenarioManager-staging logicalSessionTimeoutMinutes: null, scenarioManager-staging
A 2020-08-18T09:16:03.548Z scenarioManager-staging heartbeatFrequencyMS: 10000, scenarioManager-staging
A 2020-08-18T09:16:03.548Z scenarioManager-staging localThresholdMS: 15, scenarioManager-staging
A 2020-08-18T09:16:03.548Z scenarioManager-staging commonWireVersion: null } } scenarioManager-staging
我如何部署我的功能:
gcloud functions deploy scenarioManager-staging --entry-point scenarioManagerPushPubSub --runtime nodejs10 --trigger-topic scenarioManager-staging --region europe-west1 --set-env-vars NODE_ENV=staging --vpc-connector=default-serverless-vpc
你能帮我吗?
[编辑]我在我默认的VPC上添加了一个防火墙条目,以允许27015-2017出站。我在服务帐户上添加了访问网络的权限。..但是仍然在私有URI上被KO。在公共网站上,有一个完整的白名单,我有时会联系,但并不总是这样。
尝试在本地运行该函数并确保其正常工作。
如果它在本地工作,而不是在部署后工作,请确保您的firebase项目升级到Blaze
计划。
在研究它时,我发现了这里报告的类似问题[1]。使用谷歌DNS为用户解决了问题。如果有帮助的话,我建议你查看这个链接。
[1] 无法连接到MongoDB图集数据库
我使用完整的本地配置(pubsub+functions+数据库(进行开发。我刚从电脑上测试了Mongo Atlas的连接,它运行得很好。
$ functions-framework --target=scenarioManagerPushPubSub --signature-type=event --source=./scenarioManager.js
Serving function...
Function: scenarioManagerPushPubSub
URL: http://localhost:8080/
Connected to mongoDB
scenarioManager : { call: '5f3a39ec79c84c000a063802' } Object
Call 5f3a39ec79c84c000a063802 found
Nothing to do for this scenario 5f353c0319947a000afe9bde