Cloud Foundry MongoDB错误ECONNREFUSED

时间:2017-10-03 08:03:18

标签: node.js mongodb cloudfoundry swisscomdev

我想在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. 我创建了manifest.yml

  3. ---
    applications:
    - name: Policy_Studio
      memory: 2048MB
    
      env:
        NODE_ENV: production
    
    1. 我使用以下内容连接install.js:
    2. 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中的以下内容:
      2. 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. 推送应用程序
        3. 由于我没有云上的MongoDB,我有一个错误

        4. 我在云上构建MOngoDB服务并通过Web GUI直接绑定应用

        5. 点击gui i点击我的应用的停用按钮
        6. 我有错误
        7. 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. 中的错误相同
          4. 我尝试更改install.js

          5. 中的连接

            感谢您的帮助

2 个答案:

答案 0 :(得分:3)

虽然解析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 /端口)。在这种情况下,我只是重新创建服务实例,然后再试一次。

答案 1 :(得分:1)

最后我们找到了问题。云代工厂不允许在安装后阶段访问MongoDB服务。所以我们将它改为预启动并且有效。 谢谢你的帮助