如何从grails 3.1.8中的外部文件加载数据源配置?

问题描述 投票:2回答:4

我正在写一个grails 3.1.8应用程序。我的数据源是用application.groovy文件编写的。

我想从外部文件加载数据源配置,如用户名,密码,数据库。有没有办法在grails 3+版本中做到这一点。

这是application.groovy中的数据源配置: -

hibernate {
    cache {
        queries = false
        use_second_level_cache = true
        use_query_cache = false
        region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory'
    }
}

dataSource {
    pooled = true
    jmxExport = true
    dialect = 'org.hibernate.dialect.PostgreSQLDialect'
    driverClassName = 'org.postgresql.Driver'
    username = 'postgres'
    password = 'postgres'
    properties = {
        jmxEnabled = true
        initialSize = 5
        maxActive = 50
        minIdle = 5
        maxIdle = 25
        maxWait = 10000
        maxAge = 10 * 60000
        timeBetweenEvictionRunsMillis = 5000
        minEvictableIdleTimeMillis = 60000
        validationQuery = "SELECT 1"
        validationQueryTimeout = 3
        validationInterval = 15000
        testOnBorrow = true
        testWhileIdle = true
        testOnReturn = false
        ignoreExceptionOnPreLoad = true
        jdbcInterceptors = "ConnectionState;StatementCache(max=200)"
        defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED // safe default
        abandonWhenPercentageFull = 100 // settings are active only when pool is full
        removeAbandonedTimeout = 120
        removeAbandoned = true
        logAbandoned = false // causes stacktrace recording overhead, use only for debugging
    }
}

environments {
    development {
        dataSource {
            dbCreate = 'update'
            url = "jdbc:postgresql://localhost:5432/testdb"
            logSql = true
        }
    }
    test {
        dataSource {
            dbCreate = 'update'
            url = "jdbc:postgresql://localhost:5432/testdb"
            logSql = true
        }
    }
    production {
        dataSource {
            dbCreate = 'update'
            url = "jdbc:postgresql://localhost:5432/testdb"
            logSql = true
        }
    }
}
grails groovy datasource properties-file grails-3.0
4个回答
3
投票

这是适用于我的解决方案,您可以尝试。

此解决方案适用于grails 3.0+

首先需要添加以下依赖项:

编译'org.grails.plugins:external-config:1.1.2'

然后需要创建外部配置groovy文件,例如:

DB-Config.groovy中

然后需要将该配置文件放在应用程序目录或tomcat库之外。例如:

d:\ Apache的Tomcat的8.0.47 \ lib中

然后需要从application.groovy读取配置文件。在application.groovy中,需要为每个环境放置以下代码行:

grails.config.locations = ['file:/// $ {catalina.home} /lib/db-config.groovy']

要么

grails.config.locations = ['file:/// D:/apache-tomcat-8.0.47/lib/db-config.groovy']

如果在系统中将环境变量设置为CATALINA_HOME,则可以使用$ {catalina.home}。除了你必须使用我展示的直接路径。

所以你的application.groovy将遵循:

> environments {
>      development {
>          grails.config.locations = ['file:///${catalina.home}/lib/db-config.groovy']
>      }
>      production {
>           grails.config.locations = ['file:///${catalina.home}/lib/db-config.groovy']
>           ]
>      }
> }

并且您的db-config.groovy文件将包含以下行:

>     dataSource {
>       username = <DB_USER_NAME>
>       password = <DB_PASSWORD>
>       dbCreate = 'update'
>       url = <DB_URL>
>       logSql = true
>     }

您可以为每个环境使用不同的db-config.groovy文件。


2
投票

您可以使用以下实现从文件系统加载外部配置文件。

此示例为每个环境(开发/生产/测试)定义了一个到外部配置文件的单独路径。

 environments {
     development {
          grails.config.locations = [
                "file:///home/<CONFIG_FILE_LOCATION_DIR>/myconfig_developement.groovy" //for Unix based systems
          ]
     }
     production {
          grails.config.locations = [
                "file:///home/<CONFIG_FILE_LOCATION_DIR>/myconfig_production.groovy" // for Unix based systems
          ]
     }
}

将您的数据库配置放在myconfig_developement.groovy中,如下所示:

dataSource {
    dbCreate = 'update'
    url = "jdbc:postgresql://localhost:5432/testdb"
    logSql = true
}

1
投票

您可以使用此解决方案(适用于我,使用grails 3.1.x)

在grails-app / INIT / Application.groovy:

class Application extends GrailsAutoConfiguration implements EnvironmentAware {
    static void main(String[] args) {
        GrailsApp.run(Application, args)
    }

    @Override
    void setEnvironment(Environment environment) {
        def path = "/etc/grails-app-config.properties"
        def file = new File(path)

        if(file.exists()) {
            def config = new ConfigSlurper().parse(file.text)
            environment.propertySources.addFirst(new MapPropertySource(grails.util.Environment.getCurrent().name, config))
        }
    }
}

您可以使用环境变量作为配置路径:

System.getenv(ENV_CONF_FILE_VAR)

Grails-app-config.properties:

dataSource.dbCreate='update'
dataSource.driverClassName='com.mysql.jdbc.Driver'
dataSource.url='jdbc:mysql://localhost:5432/testdb'
dataSource.username='user'
dataSource.password='pass'
com.test='test'
com.numTest=4

0
投票

您可以使用external-config Grails插件并在外部配置文件中定义配置。

grails.config.locations = [
        "file:///etc/app/myconfig.groovy"
]

然后在myconfig.groovy中定义数据源配置

© www.soinside.com 2019 - 2024. All rights reserved.