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

时间:2017-09-28 04:11:05

标签: grails groovy datasource properties-file grails-3.0

我正在写一个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
        }
    }
}

5 个答案:

答案 0 :(得分: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文件。

答案 1 :(得分: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
}

答案 2 :(得分: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

答案 3 :(得分:0)

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

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

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

答案 4 :(得分:0)

我正在使用 grails 3.3.11 ,这是我从外部文件/源加载数据库配置的操作:


application.yaml:

将所有值保留为空,以便可以被外部源覆盖

dataSource:
  pooled: true
  driverClassName: "com.microsoft.sqlserver.jdbc.SQLServerDriver"
  url:
  username:
  password:
  loggingSql: true

现在有两种运行应用程序的方法,

  1. bootRun(下面的配置)
  2. 可运行的jar或war,java -jar --spring.config.location = file:external.properties

bootRun(build.gradle)

def Properties localBootRunProperties() {
    Properties p = new Properties()
    p.load(new FileInputStream("path to external.properties"))
    return p
}
bootRun {
    doFirst {
        bootRun.systemProperties = localBootRunProperties()
    }
    ...
}

现在, external.properties 文件应具有完全相同的配置,以覆盖application.yaml值

dataSource.url=
dataSource.username=
dataSource.password=

就是这样。享受。

相关问题