如何在grails 3.1.8中从外部文件加载数据源配置? [英] How to load datasource configuration from external file in grails 3.1.8?

查看:124
本文介绍了如何在grails 3.1.8中从外部文件加载数据源配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个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
方言='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 //安全默认值
abandonWhenPercentageFull = 100 //设置仅在池满时激活
removeAbandonedTimeout = 120
removeAbandoned = true
logAbandoned = false //导致堆栈跟踪记录开销,仅用于调试
}
}

environments {
development {
dataSource {
dbCreate ='update'
url =jdbc:postgresql:// localhost:5432 / testdb
logSql = true
}
}
测试{
dataSource {
dbCreate ='update'
url =jdbc:postgresql:// localhost:5432 / testdb
logSql = true
}
}
生产{
dataSource {
dbCreate ='update'
url =jdbc:postgresql:// localhost:5432 / testdb
logSql = true
}
}
}


解决方案

这对我有用,你可以试试。



这个解决方案适用于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 如下:

 >环境{
>开发{
> grails.config.locations = ['file:/// $ {catalina.home} /lib/db-config.groovy']
> }
>生产{
> grails.config.locations = ['file:/// $ {catalina.home} /lib/db-config.groovy']
> ]
> }
> }

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

 > dataSource {
>用户名=< DB_USER_NAME>
>密码=< DB_PASSWORD>
> dbCreate ='update'
> url =< DB_URL>
> logSql = true
> }

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


I am writing a grails 3.1.8 application. My datasource written in application.groovy file.

I want to load datasource configuration like username,password,DB from an external file. Is there any way to do it in grails 3+ versions.

Here is my datasource configuration in 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
        }
    }
}

解决方案

Here is the solution that worked for me, you can try.

This solution will work for grails 3.0+

First of all need to add following dependency:

compile 'org.grails.plugins:external-config:1.1.2'

then need to create external configuration groovy file for example:

db-config.groovy

then need to place that config file into outside of the application directory or tomcat library. for example:

D:\apache-tomcat-8.0.47\lib

Then need to read config file from the application.groovy . In application.groovy need to place following line of code for each environment:

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

or

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

You can use ${catalina.home} if you set the environment variable is CATALINA_HOME in your system. Other than you have to use direct path that I showed.

So your application.groovy will be following:

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

and your db-config.groovy file will contain following lines:

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

You can use different db-config.groovy file for each environment.

这篇关于如何在grails 3.1.8中从外部文件加载数据源配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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