2017-09-28 66 views
1

我正在写一个grails 3.1.8应用程序。我的数据源写在application.groovy文件中。如何在grails 3.1.8中从外部文件加载数据源配置?

我想从外部文件加载数据源配置,如用户名,密码,数据库。有没有办法在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 
     } 
    } 
} 

回答

3

这里是为我工作的解决方案,你可以试试。

该解决方案将Grails的3.0+

首先的工作,需要添加以下的依赖:

编译 'org.grails.plugins:外部配置:1.1.2'

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

DB-Config.groovy中

则需要在该配置文件放到应用程序目录或Tomcat库之外。例如:

d:\ Apache的Tomcat的8.0.47 \ lib中

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

grails.config.locations = ['文件:/// $ {的catalina.home}/LIB/DB-配置。常规 ']

grails.config.locations = [' 文件:/// 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文件。

+0

很好的答案。这个对我有用。 – Rassel

0

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

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

,然后定义在myconfig.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 
相关问题