通过 BootStrap 或 _Event 脚本设置插头配置值

Set a Plug Configuration Value via BootStrap or _Event script

我有几台服务器,我不想将主机名硬编码到需要它们的插件的 Config.groovy 文件中。

我可以在可以使用 LinkGenerator 的地方通过 BootStrap 动态设置插件配置值吗?

您基本上可以随时在 grails 中覆盖配置。所以在 Bootstrap.groovy 中这样做是可能的。然而,并非每个插件都必须在每次需要时都读取配置,所以你可能会迟到,插件已经拥有旧的配置变量并且不再检查。

通常最好将针对实际环境的更改集成到常规配置中。因此,您可以推出 prod war,但从实际的 server/vm 获取配置。标准 Config.groovy 已经包含读取多个配置文件的示例代码,或者您可以从系统环境变量中读取值。毕竟 Config.groovy 的行为就像一个普通的 groovy 脚本。您还可以从 /etc 读取系统文件或执行命令。

来自默认的 2.4.4 项目:

// locations to search for config files that get merged into the main config;
// config files can be ConfigSlurper scripts, Java properties files, or classes
// in the classpath in ConfigSlurper format

grails.config.locations = [ "classpath:${appName}-config.properties",
                            "classpath:${appName}-config.groovy",
                            "file:${userHome}/.grails/${appName}-config.properties",
                            "file:${userHome}/.grails/${appName}-config.groovy"]

if (System.properties["${appName}.config.location"]) {
   grails.config.locations << "file:" + System.properties["${appName}.config.location"]
}

您也可以直接从环境中设置:

grails.serverURL = System.properties["MYAPP_SERVER_URL"]

或通过hostname获取:

grails.serverURL = "http://${'hostname'.execute().text.trim()}"