【问题标题】:Why is VCAP_SERVICES null?为什么 VCAP_SERVICES 为空?
【发布时间】:2012-08-31 03:14:30
【问题描述】:

我想在我的应用启动期间读取 VCAP_SERVICES 以连接到我的 mongodb 服务,但它为空?

barry-alexanders-MacBook-Pro:~ barryalexander$ vmc create-service mongodb mongodb-relcal RelCal
Creating Service: OK
Binding Service [mongodb-relcal]: OK
Stopping Application 'RelCal': OK
Staging Application 'RelCal': OK                                                
Starting Application 'RelCal': OK                                               

barry-alexanders-MacBook-Pro:~ barryalexander$ vmc apps

+-------------+----+---------+-------------------------+----------------+
| Application | #  | Health  | URLS                    | Services       |
+-------------+----+---------+-------------------------+----------------+
| RelCal      | 1  | RUNNING | relcal.cloudfoundry.com | mongodb-relcal |
| barry       | 1  | STOPPED | barry.cloudfoundry.com  |                |
+-------------+----+---------+-------------------------+----------------+

barry-alexanders-MacBook-Pro:~ barryalexander$ vmc env RelCal
No Environment Variables

【问题讨论】:

    标签: mongodb cloud-foundry


    【解决方案1】:

    使用 vmc 'env' 命令时不会显示 VCAP_SERVICES。但是我们可以通过推送这个简单的节点应用程序看到

    var http = require('http');
    var util = require('util');
    
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.write(util.inspect(process.env.VCAP_SERVICES));
      res.write("\n\n************\n\n");
      res.end(util.inspect(req.headers));
    
    }).listen(3000);
    

    输出显示 VCAP_SERVICES 环境变量,然后是请求的标头,输出如下所示;

    '{"mongodb-2.0":[{"name":"mongo-test","label":"mongodb-2.0","plan":"free","tags":["mongodb","mongodb-1.8","nosql","document"],"credentials":{"hostname":"172.30.48.70","host":"172.30.48.70","port":25137,"username":"7ad80054-bb70-49fa-9aae-6ff5c1b458fc","password":"491bcfe9-e441-4caf-8422-00a81dbf727b","name":"4b354e7e-c39d-4053-89e1-7195b1360fd9","db":"db","url":"mongodb://7ad80054-bb70-49fa-9aae-6ff5c1b458fc:491bcfe9-e441-4caf-8422-00a81dbf727b@172.30.48.70:25137/db"}}]}'
    
    ************
    
    { host: 'node-headers.cloudfoundry.com',
      'x-forwarded-for': '80.175.199.28, 172.30.8.253',
      connection: 'close',
      'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.82 Safari/537.1',
      accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
      'accept-language': 'en-US,en;q=0.8',
      'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
      'x-cluster-client-ip': '80.175.199.28',
      cookie: '__qca=P0-351832036-1339515989739; s_nr=1344955391423; __utma=207604417.1698837494.1342027762.1345020276.1345215879.7; __utmc=207604417; __utmz=207604417.1342027762.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); s_cc=true; s_sq=%5B%5BB%5D%5D',
      'accept-encoding': 'gzip,deflate,sdch' }
    

    如果您希望将其用作参考,您可以在http://node-headers.cloudfoundry.com 看到此应用程序的运行情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-30
      • 2015-06-30
      • 2016-07-11
      • 2012-12-17
      • 2013-09-21
      • 2012-02-15
      • 1970-01-01
      • 2020-08-22
      相关资源
      最近更新 更多