【发布时间】:2020-03-04 17:39:34
【问题描述】:
我在开发的时候可以连接cosmosdb没问题,但是当我部署到azure app服务时,app服务的日志流说cosmosdb连接失败。我正在使用环境变量,所以我认为这是问题,尤其是因为 azure 没有将 .env 文件复制到 site/wwwroot 所以我转到应用程序设置并设置连接所需的每个 env 变量的值,但它仍然失败。
作为一项实验,我还尝试使用硬编码的连接字符串连接到本地工作的 mongodb 图集,但在部署到应用服务时也失败了。也许问题出在 server.js 中的端口。第一次部署到云端,所以我是菜鸟。感谢您对此提供的任何帮助!
020-02-20T23:37:00.887948933Z: [INFO] 2020-02-20T23:37:00: PM2 log: Use `pm2 show <id|name>` to get more details about an app
2020-02-20T23:37:00.888703339Z: [INFO] 2020-02-20T23:37:00: PM2 log: [--no-daemon] Continue to stream logs
2020-02-20T23:37:00.897860809Z: [INFO] 2020-02-20T23:37:00: PM2 log: [--no-daemon] Exit on target PM2 exit pid=58
2020-02-20T23:37:06.517883906Z: [INFO] 23:37:06 0|server | BLOB/home/site/wwwroot
2020-02-20T23:37:07.340932229Z: [INFO] 23:37:07 0|server | (node:69) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
2020-02-20T23:37:07.529169475Z: [INFO] 23:37:07 0|server | (node:69) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
2020-02-20T23:37:07.912008216Z: [INFO] 23:37:07 0|server | Connection on Failed
server.js
const app = require("./app");
const debug = require("debug")("node-angular");
const http = require("http");
const mongoose = require("mongoose");
var redis = require("redis");
var env = require("dotenv").config();
const normalizePort = val => {
var port = parseInt(val, 10);
if (isNaN(port)) {
e;
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
};
const onError = error => {
if (error.syscall !== "listen") {
throw error;
}
const bind = typeof port === "string" ? "pipe " + port : "port " + port;
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
};
mongoose
.connect(
"mongodb://" +
process.env.COSMOSDB_HOST +
":" +
process.env.COSMOSDB_PORT +
"/" +
process.env.COSMOSDB_DBNAME +
"?ssl=true&replicaSet=globaldb",
{
auth: {
user: process.env.COSMOSDB_USER,
password: process.env.COSMOSDB_PASSWORD
}
}
)
.then(() => console.log("Connection to CosmosDB successful"))
.catch(err => console.error(err));
const onListening = () => {
const addr = server.address();
const bind = typeof port === "string" ? "pipe " + port : "port " + port;
debug("Listening on " + bind);
};
const port = normalizePort(process.env.PORT || "3000");
app.set("port", process.env.PORT || port);
var server = app.listen(app.get("port"), function() {
debug("Express server listening on port " + server.address().port);
});
【问题讨论】:
-
我的猜测是网络级别的某些东西阻塞了应用程序中 mongo 客户端使用的端口,因为 Cosmos 或 Atlas 会发生这种情况。也许检查你的网络应用程序的网络设置那里的东西。
标签: node.js azure azure-web-app-service azure-cosmosdb azure-cosmosdb-mongoapi