运行时设置数据源值 [英] Set datasource values during run time

查看:189
本文介绍了运行时设置数据源值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我试图让我的项目在AppFog上运行。数据源信息存储在一个环境变量中,该变量本质上是JSON。我的目标是获取这些数据并设置我的数据源配置。



这是我尝试过的:

设置数据源配置的代码,这是POGO中的一种方法。 POGO被实例化,并且在DataSource.groovy的开头调用该方法:

pre $ import import appfog.ParseDataSource
new ParseDataSource ().setConfig()

dataSource {
...
}

class ParseDataSource {

void setConfig( ){
String env = java.lang.System.getenv(VCAP_SERVICES)
if(env){
def config = JSON.parse(env)
config = config [mysql-5.1] [0] .credentials
grailsApplication.config.environments.production.dataSource.username = config.username
grailsApplication.config.environments.production.dataSource.password = config.password
grailsApplication.config.environments.production.dataSource.url =jdbc:mysql://+ config.host +:+ config.port +/+ config.name
}


$ / code>

问题在于grailsApplication始终为空。我尝试在resources.groovy中注册spring bean:

$ p $ beans $ {
parseDataSource(appfog.ParseDataSource) {
grailsApplication = ref('grailsApplication')
}
}

class ParseDataSource {
def grailsAPplication
...
}

我也尝试通过持有者获得它:

  GrailsApplication grailsApplication = Holders.grailsApplication 

无论哪种方式它是空的,所以我没有做正确的事情。任何想法?

解决方案

我认为你使这个过于复杂。覆盖grails配置对象,同时仍然在构建它的过程中会导致操作顺序问题,使代码非常脆弱。



直接设置值似乎更直观直接:

Datasource.groovy:

  def configJson = JSON。 parse(java.lang.System.getenv(VCAP_SERVICES))
def mysqlConfig = configJson [mysql-5.1] [0] .credentials

dataSource = {
production = {
username = mysqlConfig.username
// etc.
}
}

如果您为了清晰起见而想继续解析其自己的类,请创建值属性并在 dataSource 块中读取它们,而不是尝试把它们放到grails配置对象中:

config解析:

  class EnvironmentConfigParser {
字符串用户名
字符串密码
字符串url

环境ConfigParser(){
def configJson = JSON.parse(java.lang.System.getenv(VCAP_SERVICES))
def mysqlConfig = configJson [mysql-5.1] [0] .credentials

username = mysqlConfig.username
password = mysqlConfig.password
url =jdbc:mysql:// $ {mysqlConfig.host}:$ {mysqlConfig.port} / $ {mysqlConfig .name}

}

in Datasource.groovy: p>

  def parser = new EnvironmentConfigParser()

dataSource = {
production = {
username = parser.username
// etc
}
}


So essentially I'm trying to get my project up and running on AppFog. The datasource information is stored in an enviornment variable, which is essentially JSON. My goal is to take this data and set my datasource config from it.

Here is what I have tried:

Code to set the datasource config which is a method in a POGO. The POGO is instantiated and the method called at the beginning of DataSource.groovy:

import appfog.ParseDataSource
new ParseDataSource().setConfig()

dataSource {
...
}

class ParseDataSource {

    void setConfig() {
        String env = java.lang.System.getenv("VCAP_SERVICES")
        if (env) {
            def config = JSON.parse(env)
            config = config["mysql-5.1"][0].credentials
            grailsApplication.config.environments.production.dataSource.username = config.username
            grailsApplication.config.environments.production.dataSource.password = config.password
            grailsApplication.config.environments.production.dataSource.url = "jdbc:mysql://" + config.host + ":" + config.port + "/" + config.name
        }
    }
}

The problem is that grailsApplication is always null. I've tried registering a spring bean in resources.groovy:

beans = {
    parseDataSource(appfog.ParseDataSource) {
        grailsApplication = ref('grailsApplication')
    }
}

class ParseDataSource {
    def grailsAPplication
    ...
}

I've also tried getting it via Holders:

    GrailsApplication grailsApplication = Holders.grailsApplication

Either way it is null so I'm not doing something right. Any ideas?

解决方案

I think you are making this overly complex. Overwriting the grails config object while still in the process of building it would cause an order of operations issue that would make the code very fragile.

Simply setting the values directly seems more straightforward:

Datasource.groovy:

def configJson = JSON.parse(java.lang.System.getenv("VCAP_SERVICES"))
def mysqlConfig = configJson["mysql-5.1"][0].credentials

dataSource = {
    production = {
        username = mysqlConfig.username
        // etc.
    }
}

If you wanted to keep parsing in its own class for clarity's sake, make the values properties and read them in the dataSource block rather than trying to put them in the grails config object:

config parsing:

class EnvironmentConfigParser {
    String username
    String password
    String url

    EnvironmentConfigParser() {
        def configJson = JSON.parse(java.lang.System.getenv("VCAP_SERVICES"))
        def mysqlConfig = configJson["mysql-5.1"][0].credentials

        username = mysqlConfig.username
        password = mysqlConfig.password
        url = "jdbc:mysql://${mysqlConfig.host}:${mysqlConfig.port}/${mysqlConfig.name}"
    }
}

in Datasource.groovy:

def parser = new EnvironmentConfigParser()

dataSource = {
    production = {
        username = parser.username
        // etc
    }
}

这篇关于运行时设置数据源值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆