Cloud Foundry MongoDB错误ECONNREFUSED

我想在Cloud Foundry上部署一个Node.JS应用程序。 我遵循以下步骤:

  1. 在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" } 
  2. 我创build了manifest.yml

 --- applications: - name: Policy_Studio memory: 2048MB env: NODE_ENV: production 
  1. 我使用以下连接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."); 
  1. 和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") }) }; 
  1. 通过命令行连接到SCAPP并使用cf push来推送应用程序

  2. 由于我没有在云上的MongoDB,我有一个错误

  3. 我在云上构build一个MOngoDB服务,并通过Web GUI直接绑定应用程序

  4. 在我点击我的应用程序的重新启动button
  5. 我有错误
 Database connection responded with: failed to connect to server [2xtorvw9ys7tg9pc.service.consul:49642] on first connect [MongoError: connect ECONNREFUSED 10.98.250.54:49642] 
  1. 我在我的清单中添加服务mongoDB,并推送我的应用程序
  2. 仍然是在第9点相同的错误

  3. 我试图改变install.js中的连接

感谢您的帮助

虽然你的parsingVCAP_SERVICES似乎工作(你得到一个包含主机名和端口的URL),我强烈build议利用其中一个现有的库进行进一步的项目: https : //www.npmjs.com/package/cfenv

不过,请parsing您的mongo凭证正常工作(请参阅cf e ${app_name} ,查找VCAP_SERVICES,手动比较)

如果你想用独立的代码来testing你的服务,下面是一个示例应用程序,我迅速扔在一起,testing绑定到它的所有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/端口被报告)。 在这种情况下,我只是重新创build服务实例,然后重试。

最后我们发现了这个问题。 云代工厂在postinstall阶段不允许访问MongoDB服务。 所以我们把它改成了prestart并且工作。 感谢您的帮助