Cloud Foundry MongoDB 错误 ECONNREFUSED
Cloud Foundry MongoDB Error ECONNREFUSED
我想在 Cloud Foundry 上部署一个 Node.JS 应用程序。
我按照以下步骤操作:
在package.json
中添加引擎部分
{
"name": "vsapc",
"version": "1.0.0",
"description": "Application Name",
"main": "server/app.js",
"scripts": {
"start": "node server/app.js",
"backup": "node backup.js",
"restore": "node restore.js",
"seed": "node server/seed/Seed.js",
"postinstall": "node install.js"
},
"directories": {
"test": "test"
},
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.15.2",
"cfenv": "^1.0.3",
"express": "^4.14.0",
"jsonwebtoken": "^7.1.9",
"mongodb": "^2.2.5",
"mongoose": "^4.6.3",
"mongoose-seed": "^0.3.1",
"morgan": "^1.7.0",
"promise": "^7.1.1",
"prompt": "^1.0.0",
"winston": "^2.2.0",
"winston-daily-rotate-file": "^1.4.0"
},
"engines": {
"node": "6.11.*",
"npm": "5.*"
},
"author": "",
"license": "ISC"
}
我创建了manifest.yml
---
applications:
- name: Policy_Studio
memory: 2048MB
env:
NODE_ENV: production
- 我在 install.js 中使用了以下连接:
const vcapServices = JSON.parse(process.env.VCAP_SERVICES);
let mongoUrl = '';
mongoUrl = vcapServices.mongodb[0].credentials.uri;
mongoose.connect(mongoUrl,{useMongoClient: true}, function (err){
if (err) {
console.log("Database connection responded with: " + err.message);
console.log("Is your server.config.json up to date?");
process.exit(1);
return
}
console.log("Connected to database.");
- 以及 app.js 中的以下内容:
Server.prototype.connectDatabase = function (url) {
mongoose.Promise = Promise;
const vcapServices = JSON.parse(process.env.VCAP_SERVICES);
let mongoUrl = '';
mongoUrl = vcapServices.mongodb[0].credentials.uri;
mongoose.connect(mongoUrl,{useMongoClient: true});
mongoose.connection.on("error", function (err) {
log.error(err)
});
mongoose.connection.once("openUri", function () {
log.info("Connected to DB")
})
};
通过命令行连接到 SCAPP 并使用 cf push
推送应用程序
因为我在云端没有 MongoDB 我有一个错误
我在云端搭建了一个MOngoDB服务,通过web界面直接绑定app
- 在 gui 上,我为我的应用程序单击了 restage 按钮
- 我有错误
Database connection responded with: failed to connect to server
[2xtorvw9ys7tg9pc.service.consul:49642] on first connect [MongoError:
connect ECONNREFUSED 10.98.250.54:49642]
- 我在清单中添加服务 mongoDB 并 cf 推送我的应用程序
还是和第9点一样的错误
我尝试更改 install.js
中的连接
感谢您的帮助
虽然您对 VCAP_SERVICES
的解析似乎有效(您得到一个包含主机名和端口的 URL),但我强烈建议为进一步的项目利用现有库之一:
https://www.npmjs.com/package/cfenv
不过,请确保您的 mongo 凭据解析正常(cf e ${app_name}
,查找 VCAP_SERVICES,手动比较)
如果您想使用独立代码测试您的服务,这里是我快速拼凑的示例应用程序,用于测试绑定到它的所有 mongodb 服务:
package.json:
{
"name": "mongo-tester",
"version": "1.0.0",
"description": "tests all mongodbs via VCAP_SERVICES",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Michael Erne",
"license": "MIT",
"dependencies": {
"async": "^2.5.0",
"cfenv": "^1.0.4",
"lodash": "^4.17.4",
"mongodb": "^2.2.31"
}
}
server.js:
var cfenv = require('cfenv'),
_ = require('lodash'),
http = require('http'),
async = require('async'),
MongoClient = require('mongodb').MongoClient
var appEnv = cfenv.getAppEnv();
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Return something for CF Health Check\n');
}).listen(appEnv.port);
var services = _.values(appEnv.getServices());
var mongodbs = _.filter(services, { label: 'mongodb' });
async.eachLimit(mongodbs, 1, function (m, callback) {
MongoClient.connect(m.credentials.database_uri, function (err, db) {
if (err) {
return callback(err);
}
db.collection("debug").insertOne({"test": true}, function(err, res) {
if (err) return callback(err);
console.log("document inserted successfully into " + m.credentials.database_uri);
db.close();
return callback(null);
});
});
}, function (err) {
if (err) {
console.log(err.stack || err);
console.log('---> mongodb connection failed <---');
return;
}
console.log('---> connection to all BOUND mongodb successful <---');
});
如果它可以连接到任何绑定的 mongodb 服务,它应该在其日志中打印如下内容:
document inserted successfully into mongodb://xxx:yyy@zzz.service.consul:1337/databaseName
---> connection to all BOUND mongodb successful <---
如果失败并出现类似错误,则服务实例似乎已损坏(报告错误 url/port)。在这种情况下,我会重新创建服务实例并重试。
终于找到问题所在了。 Cloud Foundry 不允许在安装后阶段访问 MongoDB 服务。所以我们将其更改为预启动并且它起作用了。
感谢您的帮助
我想在 Cloud Foundry 上部署一个 Node.JS 应用程序。 我按照以下步骤操作:
在package.json
中添加引擎部分{ "name": "vsapc", "version": "1.0.0", "description": "Application Name", "main": "server/app.js", "scripts": { "start": "node server/app.js", "backup": "node backup.js", "restore": "node restore.js", "seed": "node server/seed/Seed.js", "postinstall": "node install.js" }, "directories": { "test": "test" }, "dependencies": { "bcrypt-nodejs": "0.0.3", "body-parser": "^1.15.2", "cfenv": "^1.0.3", "express": "^4.14.0", "jsonwebtoken": "^7.1.9", "mongodb": "^2.2.5", "mongoose": "^4.6.3", "mongoose-seed": "^0.3.1", "morgan": "^1.7.0", "promise": "^7.1.1", "prompt": "^1.0.0", "winston": "^2.2.0", "winston-daily-rotate-file": "^1.4.0" }, "engines": { "node": "6.11.*", "npm": "5.*" }, "author": "", "license": "ISC" }
我创建了manifest.yml
--- applications: - name: Policy_Studio memory: 2048MB env: NODE_ENV: production
- 我在 install.js 中使用了以下连接:
const vcapServices = JSON.parse(process.env.VCAP_SERVICES); let mongoUrl = ''; mongoUrl = vcapServices.mongodb[0].credentials.uri; mongoose.connect(mongoUrl,{useMongoClient: true}, function (err){ if (err) { console.log("Database connection responded with: " + err.message); console.log("Is your server.config.json up to date?"); process.exit(1); return } console.log("Connected to database.");
- 以及 app.js 中的以下内容:
Server.prototype.connectDatabase = function (url) { mongoose.Promise = Promise; const vcapServices = JSON.parse(process.env.VCAP_SERVICES); let mongoUrl = ''; mongoUrl = vcapServices.mongodb[0].credentials.uri; mongoose.connect(mongoUrl,{useMongoClient: true}); mongoose.connection.on("error", function (err) { log.error(err) }); mongoose.connection.once("openUri", function () { log.info("Connected to DB") }) };
通过命令行连接到 SCAPP 并使用 cf push
推送应用程序
因为我在云端没有 MongoDB 我有一个错误
我在云端搭建了一个MOngoDB服务,通过web界面直接绑定app
- 在 gui 上,我为我的应用程序单击了 restage 按钮
- 我有错误
Database connection responded with: failed to connect to server [2xtorvw9ys7tg9pc.service.consul:49642] on first connect [MongoError: connect ECONNREFUSED 10.98.250.54:49642]
- 我在清单中添加服务 mongoDB 并 cf 推送我的应用程序
还是和第9点一样的错误
我尝试更改 install.js
中的连接
感谢您的帮助
虽然您对 VCAP_SERVICES
的解析似乎有效(您得到一个包含主机名和端口的 URL),但我强烈建议为进一步的项目利用现有库之一:
https://www.npmjs.com/package/cfenv
不过,请确保您的 mongo 凭据解析正常(cf e ${app_name}
,查找 VCAP_SERVICES,手动比较)
如果您想使用独立代码测试您的服务,这里是我快速拼凑的示例应用程序,用于测试绑定到它的所有 mongodb 服务:
package.json:
{
"name": "mongo-tester",
"version": "1.0.0",
"description": "tests all mongodbs via VCAP_SERVICES",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Michael Erne",
"license": "MIT",
"dependencies": {
"async": "^2.5.0",
"cfenv": "^1.0.4",
"lodash": "^4.17.4",
"mongodb": "^2.2.31"
}
}
server.js:
var cfenv = require('cfenv'),
_ = require('lodash'),
http = require('http'),
async = require('async'),
MongoClient = require('mongodb').MongoClient
var appEnv = cfenv.getAppEnv();
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Return something for CF Health Check\n');
}).listen(appEnv.port);
var services = _.values(appEnv.getServices());
var mongodbs = _.filter(services, { label: 'mongodb' });
async.eachLimit(mongodbs, 1, function (m, callback) {
MongoClient.connect(m.credentials.database_uri, function (err, db) {
if (err) {
return callback(err);
}
db.collection("debug").insertOne({"test": true}, function(err, res) {
if (err) return callback(err);
console.log("document inserted successfully into " + m.credentials.database_uri);
db.close();
return callback(null);
});
});
}, function (err) {
if (err) {
console.log(err.stack || err);
console.log('---> mongodb connection failed <---');
return;
}
console.log('---> connection to all BOUND mongodb successful <---');
});
如果它可以连接到任何绑定的 mongodb 服务,它应该在其日志中打印如下内容:
document inserted successfully into mongodb://xxx:yyy@zzz.service.consul:1337/databaseName
---> connection to all BOUND mongodb successful <---
如果失败并出现类似错误,则服务实例似乎已损坏(报告错误 url/port)。在这种情况下,我会重新创建服务实例并重试。
终于找到问题所在了。 Cloud Foundry 不允许在安装后阶段访问 MongoDB 服务。所以我们将其更改为预启动并且它起作用了。 感谢您的帮助