springboot Multi-environment (dev, test, prod) configuration2017-07-17 10:33 1290 People read comments (0) favorite reports Classification:Spring Boot (6)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Directory (?) [+]
Propertiest Configuration Format
在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:
Application-dev.properties: Development environment
Application-test.properties: Test environment
Application-prod.properties: Production environment
As to which specific configuration file will be loaded, it needs to be set in the Application.properties file by the Spring.profiles.active property, whose value corresponds to the {profile} value.
such as: Spring.profiles.active=dev will load the contents of the Application-dev.properties configuration file
Below, a sample experiment is performed with different service ports configured in different environments.
Create different profiles for each environment application-dev.properties, application-test.properties, application-prod.properties
All three files are set with different server.port properties, such as: Dev environment set to 8001,test environment set to 8002,prod environment set to 8003
Set Spring.profiles.active=dev in Application.properties, which means that the dev environment is set by default
Test loading of different configurations
执行java -jar xxx.jar,可以观察到服务端口被设置为8001,也就是默认的开发环境(dev) 执行java -jar xxx.jar --spring.profiles.active=test,可以观察到服务端口被设置为8002,也就是测试环境的配置(test) 执行java -jar xxx.jar --spring.profiles.active=prod,可以观察到服务端口被设置为8003,也就是生产环境的配置(prod)
According to the above experiment, we can summarize the multi-environment configuration ideas as follows:
Configure common content in application.properties and set Spring.profiles.active=dev to the default configuration of the development environment
Configure different environments in Application-{profile}.properties
Activating configuration of different environments by command-line mode
Springboot Multi-environment configuration