在 Spring Boot 的 application.properties 中使用 env 变量 [英] Using env variable in Spring Boot's application.properties

查看:36
本文介绍了在 Spring Boot 的 application.properties 中使用 env 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个 Spring Boot Web 应用程序,我们使用的数据库是 MySQL;

We are working on a Spring Boot web application, and the database we are using is MySQL;

  • 我们的设置是我们首先在本地测试它(意味着我们需要在我们的 PC 上安装 MySQL);

  • the setup we have is we first test it locally (means we need to install MySQL on our PC);

然后我们推送到 Bitbucket;

then we push to Bitbucket;

Jenkins 自动检测到 Bitbucket 的新推送并在其上进行构建(为了让 Jenkins mvn 构建通过,我们还需要在运行 Jenkins 的虚拟机上安装 MySQL).

Jenkins automatically detects the new push to Bitbucket and does a build on it (for Jenkins mvn build to pass we also need to install MySQL on the virtual machines that is running Jenkins).

如果 Jenkins 构建通过,我们会将代码推送到 OpenShift 上的应用程序(使用 Jenkins 上的 Openshift 部署插件).

if Jenkins build passes we push the code to our application on OpenShift (using the Openshift deployment plugin on Jenkins).

我们遇到的问题,正如您可能已经发现的那样:

The problem we have, as you may have already figured it out, is that:

  • application.properties中,我们不能硬编码MySQL信息.由于我们的项目将在 3 个不同的地方运行(localJenkinsOpenShift),我们需要在 code>application.properties(我们知道有不同的方法,但我们现在正在研究这个解决方案).

  • in application.properties we can not hard code the MySQL info. Since our project will be running in 3 different places (local, Jenkins, and OpenShift), we need to make the datasource field dynamic in application.properties (we know there are different ways of doing it but we are working on this solution for now).

  spring.datasource.url = 
  spring.datasource.username = 
  spring.datasource.password = 

我们提出的解决方案是我们在本地和 Jenkins VM 中创建系统环境变量(以 OpenShift 的方式命名它们),并分别为它们分配正确的值:

The solution we came up with is we create system environment variables locally and in the Jenkins VM (naming them the same way OpenShift names them), and assigning them the right values respectively:

export OPENSHIFT_MYSQL_DB_HOST="jdbc:mysql://localhost"
export OPENSHIFT_MYSQL_DB_PORT="3306"
export OPENSHIFT_MYSQL_DB_USERNAME="root"
export OPENSHIFT_MYSQL_DB_PASSWORD="123asd"

我们已经这样做了,并且有效.我们还检查了 Map;env = System.getenv(); 环境变量可以变成这样的java变量:

We have done this and it works. We have also checked with Map<String, String> env = System.getenv(); that the environment variables can be made into java variables as such:

String password = env.get("OPENSHIFT_MYSQL_DB_PASSWORD");   
String userName = env.get("OPENSHIFT_MYSQL_DB_USERNAME");   
String sqlURL = env.get("OPENSHIFT_MYSQL_DB_HOST"); 
String sqlPort = env.get("OPENSHIFT_MYSQL_DB_PORT");

现在唯一剩下的就是我们需要在 application.properties 中使用这些 java 变量,而这正是我们遇到的问题.

Now the only thing left is we need to use these java variables in our application.properties, and that is what we are having trouble with.

我们需要在哪个文件夹以及如何分配passworduserNamesqlURLsqlPortapplication.properties 的 code> 变量,以便能够看到它们,我们如何将它们包含在 application.properties 中?

In which folder, and how, do we need to assign the password, userName, sqlURL, and sqlPort variables for application.properties to be able to see them and how do we include them in application.properties?

我们尝试了很多事情,其中​​之一是:

We have tried many things one of them being:

spring.datasource.url = ${sqlURL}:${sqlPort}/"nameofDB"
spring.datasource.username = ${userName}
spring.datasource.password = ${password}

到目前为止运气不佳.我们可能没有将这些环境变量放在正确的类/文件夹中,或者在 application.properties 中错误地使用了它们.

No luck so far. We are probably not putting these environment variables in the right class/folder or are using them incorrectly in application.properties.

推荐答案

你不需要使用 java 变量.要包含系统环境变量,请将以下内容添加到您的 application.properties 文件中:

You don't need to use java variables. To include system env variables add the following to your application.properties file:

spring.datasource.url = ${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/"nameofDB"
spring.datasource.username = ${OPENSHIFT_MYSQL_DB_USERNAME}
spring.datasource.password = ${OPENSHIFT_MYSQL_DB_PASSWORD}

但是 @Stefan Isele 建议的方式更可取,因为在这种情况下,您只需声明一个环境变量:spring.profiles.active.Spring 将通过 application-{profile-name}.properties 模板自动读取相应的属性文件.

But the way suggested by @Stefan Isele is more preferable, because in this case you have to declare just one env variable: spring.profiles.active. Spring will read the appropriate property file automatically by application-{profile-name}.properties template.

这篇关于在 Spring Boot 的 application.properties 中使用 env 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆