Compose-for-mongodb 与 pencilblue 的 Bluemix 连接

Compose-for-mongodb Bluemix connection with pencilblue

目前,我正在使用 Bluemix 通过 PencilBlue 部署和托管网站。我的问题在于存储服务 "compose-for-mongo" 和 pencilblue 框架的连接。 此连接是通过 VCAP JSON 完成的。 VCAP 具有以下结构;

{
    "compose-for-mongodb": [
        {
            "credentials": {
                "db_type": "mongodb",
                "name": "**************************************",
                "uri_cli": "**************************************",
                "ca_certificate_base64": "*********************************************************************",
                "deployment_id": "*********************************",
                "uri": "******************************************************"
            },
            "syslog_drain_url": null,
            "label": "compose-for-mongodb",
            "provider": null,
            "plan": "***********",
            "name": "************************",
            "tags": [
                "big_data",
                "data_management",
                "ibm_created"
            ]
        }
    ]
}

据我了解,"compose-for-mongodb" 是一个集群而不是服务器。我们需要通过 VCAP JSON 在 PencilBlue 和这个 "cluster" 之间建立连接。目前,我们正在尝试像这样在 config.js 中引导 JSON 解析:

 if (typeof process.env.VCAP_SERVICES === 'string') {
        try {
            var vcapServices = JSON.parse(process.env.VCAP_SERVICES);
            var vcapMongo = vcapServices['compose-for-mongodb'][0].credentials;

            var uri = vcapMongo.uri;

            mongoConfig.servers = [
                uri
            ];

            var userPassword = uri.split('mongodb://')[1].split('@')[0].split(':');

            mongoConfig.authentication = {
                un: userPassword[0],
                pw: userPassword[1]
            };

            var db = uri.split('?uri=')[0].split('/')[1];

            mongoConfig.options.mongos = {
                ssl: true,
                sslValidate: true,
                sslCA: [new Buffer(vcapMongo.ca_certificate_base64, 'base64')],
                poolSize: 1,
                reconnectTries: 1
            };

            mongoConfig.name = db;

我们尝试了多种方法都没有成功。

有人遇到过这个问题吗?有关于这个主题的文档吗?

重现步骤:

创建一个 Bluemix 节点应用程序并将其与 "compose-for-mongodb" 存储服务连接。

铅笔蓝版本:0.8.0

环境描述:Node.js Bluemix 中的 SDK 和 compose-for-mongodb 连接的服务

我有将 pencilblue 框架与 NoSQL 数据源(例如 Compose-for-mongodb 连接起来的经验和成功经验。这是 sample repo with some handy deploy to bluemix buttons that sets everything up for you - if you'd like a turnkey approach (Caveat: Some patience is required :-) The pipeline script includes some hard coded delays (~8 mins) to accommodate uncertainty with service instantiation). For your particular question, the repository's config.js should help. Finally, this repo also utilizes a media provider plugin using the Bluemix Object Storage service within this repo.

摘录如下:

[....]

try {
  var services = JSON.parse(process.env.VCAP_SERVICES);
  // look for a service starting with 'mysql'
  // MySQL is the only one supported by Ghost right now
  for (var svcName in services) {
      if (svcName.match(/^compose-for-mongodb/)) {
          mongoCreds = services[svcName][0]['credentials'];
          var uriObject = mongodbUri.parse(mongoCreds.uri)
          mongoCreds.host = uriObject.hosts[0].host
          mongoCreds.port = uriObject.hosts[0].port
          mongoCreds.user = uriObject.username
          mongoCreds.password = uriObject.password
          mongoCreds.client = 'mongo';
          mongoCreds.userpass = mongoCreds.user + ":" + mongoCreds.password + "@"
          mongoCreds.db = uriObject.database;
      }
  }
}

[....]

"db": {
        "type": mongoCreds.client,
        "servers": [
          "mongodb://" + mongoCreds.userpass + mongoCreds.host + ":" + mongoCreds.port
        ],
        "name": mongoCreds.db,
        "options": {
          "server": {
             "ssl": cloud,
             "sslValidate": cloud,
             "sslCert": Buffer.from(mongoCreds.ca_certificate_base64, 'base64').toString('ascii'),
             "sslCA" : [Buffer.from(mongoCreds.ca_certificate_base64, 'base64').toString('ascii')]
          }
        },
        "writeConcern": "majority",
        "query_logging": false,
        "authentication": {
          "un": mongoCreds.user,
          "pw": mongoCreds.password
        }
    },

[....]