我正在编写Grails 3.1.8应用程序。我的dataSource在application.groovy文件中编写。
我想从外部文件加载数据源配置,例如用户名,密码,db。有什么方法可以在3个以上的Grails中进行。
这是我的dataSource配置。
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 3.0
首先需要添加以下依赖性:
compile'org.grails.plugins:external-config:1.1.2'
然后需要创建外部配置凹槽文件,例如:
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.0.47/lib/db/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文件。
您可以使用以下实现从文件系统加载外部配置文件。
此示例为每个环境定义(开发/生产/测试)的外部配置文件的单独路径。
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
}
您可以使用此解决方案(对我有用,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
您可以使用外部config Grails插件并在外部配置文件中定义配置。
grails.config.locations = [
"file:///etc/app/myconfig.groovy"
]
然后在myconfig.groovy
我正在使用 Grails 3.3.11 ,这是我为从外部文件/源加载的DB配置所做的工作:
application.yaml:
将所有值保持为空,以便将它们覆盖为外部源
dataSource:
pooled: true
driverClassName: "com.microsoft.sqlserver.jdbc.SQLServerDriver"
url:
username:
password:
loggingSql: true
现在有两种运行应用程序的方法
- bootrun(以下配置)
- 可运行的jar或war,java -jar -spring.config.location =文件: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 文件应具有与覆盖应用程序完全相同的配置。yamlvalues
dataSource.url=
dataSource.username=
dataSource.password=
就是这样。享受。