如何在AppFog / CloudFoundry上使用MongoDB部署Grails?

时间:2012-11-14 23:47:47

标签: mongodb grails cloudfoundry appfog grails-2.1

Grails在本地使用mongodb插件很好用,但我在AppFog上遇到了问题。

对于除MongoDB之外的数据库,在AppFog上部署Grails的说明非常明确。文档似乎暗示了MongoDB的自动配置,但是如果我将配置设置为以下内容,它就不起作用。

//DataSource.groovy

grails {
  mongo {
    host = "localhost"
    port = 27017
    databaseName = "dbname"
  }
}

自动配置似乎不会用正确的主机名替换localhost。好像我需要设置VCAP_SERVICES中的值。

2 个答案:

答案 0 :(得分:2)

联系支持后,答案是使用VCAP_SERVICE环境变量。在网上挖掘之后,我想出了如何在配置中检索和使用VCAP_SERVICES。

grails {
    def vcap = System.env.VCAP_SERVICES
    def credentials = vcap ? grails.converters.JSON.parse(vcap)["mongodb-1.8"][0]["credentials"] : null
    mongo {
        host = credentials ? credentials.hostname : "localhost"
        port = credentials ? credentials.port : "27017"
        username = credentials ? credentials.username : null
        password = credentials ? credentials.password : null
        databaseName = credentials ? credentials.db : "dbname"
    }
}

我还创建了a github gist this configuration

答案 1 :(得分:2)

您的Grails项目中是否安装了CloudFoundry plugin?该插件在Grails应用程序中对MongoDB和其他数据源进行自动重新配置。

如果由于某种原因你不能或不想使用Grails CloudFoundry插件,那么使用上面显示的环境变量的另一种方法是使用cloudfoundry-runtime Java API。通过此API,您可以访问与环境变量中存储的信息相同的信息,但它比直接解析环境变量要简单得多。