在Google App Engine上连接Mongoose

Tun*_*i_D 6 google-app-engine mongoose mongodb node.js google-compute-engine

我已成功在Google App引擎引擎上部署了自定义Node.js应用程序.但是我在启动应用程序时遇到问题,因为尝试连接时mongoose超时.令人沮丧的是,猫鼬在我的本地机器上与完全相同的参数连接得很好.

我的MongoDb URI的形式如下:

 mongodb://<dbuser>:<dbpassword>@xxxx.mlab.com:<portNumber>/d??b-name
Run Code Online (Sandbox Code Playgroud)

正如URI所暗示的那样,db由mlab托管.它是Google Cloud Platform上的沙箱实例.但是我没有使用Google Compute Engine部署数据库.在mlab上完成设置过程时,我只是选择在GCP上主持.

我在SO上遇到过类似的问题,但大多数都没有接受.只是在评论中不同的话语,没有公认的解决方案.

因此,我的问题是,在尝试连接上述URI时,我的App Engine实例与本地计算机有何不同?我选择让mlab在GCP上托管它的事实是什么?如果我选择在亚马逊AWS上托管它会有什么不同吗?问题的根本原因究竟是什么?

作为参考,以下是我发现的类似问题:

另外,如果有帮助,MongoDB是作为分贝的商业模式中,数据存储的应用程序的快速会话,并从socket.io存储临时数据.

部署后从App Engine发生错误堆栈跟踪:

2017-10-18 02:13:46 default[20171017t215757]  npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
2017-10-18 02:13:46 default[20171017t215757]  npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
2017-10-18 02:13:46 default[20171017t215757]  npm ERR! enoent This is most likely not a problem with npm itself
2017-10-18 02:13:46 default[20171017t215757]  npm ERR! enoent and is related to npm not being able to find a file.
2017-10-18 02:13:46 default[20171017t215757]  npm ERR! enoent
2017-10-18 02:13:46 default[20171017t215757]
2017-10-18 02:13:46 default[20171017t215757]  npm ERR! Please include the following file with any support request:
2017-10-18 02:13:46 default[20171017t215757]  npm ERR!     /app/npm-debug.log
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! Linux 3.16.0-4-amd64
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! argv "/nodejs/bin/node" "/nodejs/bin/npm" "start"
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! node v6.11.3
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! npm  v3.10.10
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! path /app/package.json
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! code ENOENT
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! errno -2
2017-10-18 02:23:41 default[20171017t215757]  npm ERR! syscall open
Run Code Online (Sandbox Code Playgroud)

在Google Cloud Shell中本地运行应用程序时出现堆栈跟踪错误:

/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb/lib/mongo_client.js:421
          throw err
          ^
MongoError: failed to connect to server [ds147454.mlab.com:47454] on first connect [MongoError: connection 1 to ds147454.mlab.com:47454 timed out]
    at Pool.<anonymous> (/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb-core/lib/topologies/server.js:336:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:188:7)
    at Connection.<anonymous> (/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb-core/lib/connection/pool.js:280:12)
    at Connection.g (events.js:292:16)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:191:7)
    at Socket.<anonymous> (/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb-core/lib/connection/connection.js:197:10)
    at Socket.g (events.js:292:16)
    at emitNone (events.js:86:13)
    at Socket.emit (events.js:185:7)
    at Socket._onTimeout (net.js:338:8)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)
Run Code Online (Sandbox Code Playgroud)

Wan*_*iar 6

正如已经指出的那样,您可以按照https://cloud.google.com/nodejs/getting-started/deploy-mongodb 上的说明使用config.json文件配置Google App Engine .(更新:链接不再有效,请尝试:https://cloud.google.com/community/tutorials/nodejs-mongodb-on-appengine)

由于您使用mongoose的模型将决定集合的使用.即save(),update().如果您node.js直接使用,则还可以在代码中轻松更改数据库/集合.

尝试连接上述URI时,我的App Engine实例与本地计算机有何不同?

Google App Engine是一个完全托管的平台.从某种意义上讲,它是无服务器的,您正在利用一组工作人员来扩展/缩小您的应用程序.当请求进入您的应用程序时,池中的实例将处理任务/请求.

与您的本地计算机不同,您的计算机也会处理请求.

您还可以找到有用的Google App Engine常见问题解答.

我选择让mlab在GCP上托管它的事实是什么?

不是很多.正如您所提到的,您目前正在使用GCP进行SandBox(免费)计划.目前,这将在us-central1区域上部署MongoDB实例,并且没有其他区域选择.不同云提供商之间的RAM /存储/价格也略有不同.

您也可以通过在us-central1区域上选择GAE来减少网络延迟.

问题的根本原因究竟是什么?

如果没有适当的堆栈跟踪或错误日志来描述超时问题,很难确定问题是什么.

通常node.js项目包含一个名为JavaScript的文件config.js,其中包含以下行:

  nconf.argv()
       .env()
       .file({ file: path.join(__dirname, 'config.json') })
       .defaults()
Run Code Online (Sandbox Code Playgroud)

其中基本上尝试按以下顺序读取配置:

  • 命令行参数
  • 环境变量
  • 配置文件(指定路径)
  • 默认值

另请参阅Google Cloud Platform:nodejs-getting-started

另外还有GoogleCloudPlatform/nodejs-docs-samples/appengine/mongodb项目.

  • 尽管OP的问题是其他问题,但这个答案非常彻底且经过深思熟虑 - 值得肯定的是值得肯定.谢谢你的帮助! (3认同)
  • 嗨,不是内部.这应该在您的node.js代码中.例如,请参阅[server.js:L25](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/mongodb/server.js#L25)读取的node.js示例文件一个不同的JSON文件配置.如果从GAE连接仍然有问题,请发布您的代码和任何错误堆栈跟踪. (2认同)

Tun*_*i_D 3

好吧,这花了我太长时间,但问题出在我的 Dockerfile 上。

我正在扩展 node.js 运行时,但我的 Dockerfile 缺少复制应用程序目录和安装 npm 的行。

为了创建适当的 Dockerfile,我使用了以下命令:

gcloud beta app gen-config --custom
Run Code Online (Sandbox Code Playgroud)

有关该命令和扩展 Node.js 运行时的更多信息可以在此处找到。

添加所需的依赖项后,我的最终 Dockerfile 如下所示:

注意:我只能在部署后连接到我的 MongoDb 实例。在 Cloud Shell 中本地运行仍然会超时。

# Dockerfile extending the generic Node image with application files for a
# single application.
FROM gcr.io/google_appengine/nodejs
COPY . /app/

RUN apt-get -y update && apt-get install -y libav-tools && apt-get install -y graphicsmagick

# You have to specify "--unsafe-perm" with npm install
# when running as root.  Failing to do this can cause
# install to appear to succeed even if a preinstall
# script fails, and may have other adverse consequences
# as well.
# This command will also cat the npm-debug.log file after the
# build, if it exists.
RUN npm install --unsafe-perm || \
  ((if [ -f npm-debug.log ]; then \
      cat npm-debug.log; \
    fi) && false)
CMD npm start
Run Code Online (Sandbox Code Playgroud)