目前,我正在使用Bluemix部署和托管使用PencilBlue的网站。我的问题在于连接存储服务" compose-for-mongo"和铅笔蓝框架。 此连接通过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"
]
}
]
}
据我所知,"撰写为mongodb"是集群而不是服务器。我们需要在PencilBlue和这个" cluster"之间建立连接。通过VCAP JSON。目前,我们正试图在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"存储服务。
PencilBlue版本:0.8.0
环境描述:Bluemix中的Node.js SDK和连接的compose-for-mongodb服务
答案 0 :(得分:1)
我已经将Pencilblue框架与NoSQL数据源(例如Compose-for-mongodb)连接起来的经验和成功。在这里' sa sample repo有一些方便的部署到bluemix按钮,为你设置一切 - 如果你想要一个交钥匙方法(警告:需要一些耐心:-)管道脚本包括一些硬编码延迟(约8分钟)以适应服务实例化的不确定性)。对于您的特定问题,存储库config.js应该有所帮助。最后,此repo还使用了this repo内使用Bluemix对象存储服务的媒体提供程序插件。
这是一段摘录:
[....]
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
}
},
[....]