使用特定的 application.properties 文件将 Spring Boot 应用程序部署到 Heroku

lor*_*eru 5 deployment spring heroku spring-boot

我想application.properties为每个环境定义不同的文件。在本地工作时,我想定义一个 H2 数据库。对于测试环境(Heroku),我有一个 MySQL 数据库配置。因此,我想为此类工作案例定义完全不同的文件。

目前我有application.properties本地海豚,并application-tst.properties在 Heroku 中使用。但是我不知道在部署时如何选择合适的。

我的目标是为在 Heroku 中运行的应用程序与在本地机器上运行的应用程序具有不同的配置。

Mis*_*orp 15

您可以使用 spring.profiles.active property (documentation). On Heroku you can set this using config vars either via the cli, the dashboard or the platform API

CLI

For setting the tst profile using the cli, try

$ heroku config:set SPRING_PROFILES_ACTIVE=tst
Run Code Online (Sandbox Code Playgroud)

Dashboard

导航到 settings tab and set key as SPRING_PROFILES_ACTIVE and value as tst, then click save.

Platform API

您可以使用多种工具来实现相同的结果,但按照 Platform API 文档,您可以使用 curl

$ curl -n -X PATCH https://api.heroku.com/apps/$APP_ID_OR_NAME/config-vars \
  -d '{ "SPRING_PROFILES_ACTIVE": "tst" }' \
  -H "Content-Type: application/json" \
  -H "Accept: application/vnd.heroku+json; version=3"
Run Code Online (Sandbox Code Playgroud)

请注意,将spring.profiles.active属性设置为配置变量会影响整个应用程序。