我想为生产环境加载三个 application-xxx.yml 中的单个 application-xxx.yml ,这由环境变量的值决定(就我而言,是)国家。
说明:
我已将 Spring Boot 微服务 (REST api) 部署在不同国家/地区的不同 AWS 区域中。
我在资源文件夹下添加了 3 个不同的 yml/properties 文件,例如:
对于美国> application-usa.yml。
对于德国 > application-ger.yml 。
对于印度 > application-ind.yml 。
(所有三个 yml 文件都包含相似的属性,但值不同)
所以可以说,如果 env.变量 country == USA 则应加载 application-usa.yml 文件,但不加载其余提到的 application-xxx.yaml 文件 等等......
所以最后,根据环境变量 country 的值,应该加载上述 3 个 yaml 文件中的一个
那么我们如何才能实现这一目标呢?
最佳答案
这是使用 gradle 构建工具的一种可能的解决方案。您必须在 resources 目录中创建 usa、Germany 和 India 等三个目录,并将这些 yml 文件保存在每个目录下。在应用程序构建时,您必须将特定的 yml 文件从国家(美国、德国或印度)目录复制到资源目录。要在构建时识别特定国家/地区,您必须将国家/地区名称作为参数传递,如下所示:
./gradlew my-project:bootRun -Pcountry=india
下面是一个示例 gradle 代码,用于将特定的 yml 文件从国家目录复制到资源目录:
def country = project.property('country') //Get the country name
//Copy yml files from country directory to resources directory.
//You have to replace file_path with your projects file path.
task copyYmlFile(type: Copy) {
from "file_path/resources/${country}"
into "file_path/resources"
}
bootRun {
dependsOn 'copyYmlFile' //Call the yml copy task in the project build time
//Rest of the codes
}
关于java - 如何在 Spring Boot 中从其他 application-xxx.yml 加载自定义 application-xxx.yml 文件而不使用配置文件注释解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59958651/