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

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

问题描述

我们正在开发 Spring Boot 网络应用,我们使用的数据库是 MySql ;

We are working on a Spring Boot web app and the database we are using is MySql;


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

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

然后我们推送到 Bitbucket ;

Jenkins 自动检测到对Bitbucket的新推送并对其进行构建(对于Jenkins mvn build,我们还需要在运行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个不同的地方运行(本地 Jenkins OpenShift ),我们需要在<$中使数据源字段动态化c $ c> 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 way's 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 variable locally and in the Jenkins vm (naming them the same way OpenShift names them) and assigning them the right values respectfully:

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< String,String> 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");

现在唯一剩下的就是我们需要在中使用这些java变量application.properties 这就是我们遇到的麻烦。

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.

在哪个文件夹中我们如何分配密码 userName sqlURL sqlPort application.properties 能够看到它们以及如何将它们包含在 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}

到目前为止没有运气。我们可能没有将这些env变量放在正确的类/文件夹中,并在 applicatin.properties 中正确使用它们。

No luck so far. We are probably not putting these env variables in the right class/folder and using them correctly in applicatin.properties.

非常感谢您的帮助!!

Your help is highly appreciated!!

谢谢!

推荐答案

您不需要使用java变量。要包含系统env变量,请将以下内容添加到 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_PORT}

@Stefan Isele 建议的方式更可取,因为在此如果你必须声明一个env vaireable: 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 vaireable: spring.profiles.active. And Spring will read the appropriate property file automatically by application-{profile-name}.properties template.

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

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