SpringBoot multiple environment configuration profiles

1. What are Profiles?
Profile allows Spring to provide different configuration functions for different environments, and can quickly switch environments by activating, specifying parameters, etc.

2. There are 4 storage paths for SpringBoot's main configuration file application.yml

  1. file:config/
  2. file:/
  3. classpath:config/
  4. classpath:/
    Priority from high to bottom
    Insert picture description here

2
During the actual development, the multi-profile file format is divided into local environment, test environment and production environment. This requires multiple configuration files, such as port numbers, and so on. Of course, we can change the configuration every time we change an environment. But very cumbersome

At this time, you can set several more configuration files, the file name format can be application-{profile}.properties/yml, but the main configuration file application.properties used by default

We can switch to other configuration files at any time in the main configuration file. For example, I created three configuration files

application.properties: main configuration file
application-dev.properties: development environment configuration file
application-test.properties: test environment configuration file
application.prop-properties: production environment
configuration file Set a different port number for each configuration file:

application-dev.properties
development environment server.port=8082

application-test.properties
test environment server.port=8083

application.prop-properties
production environment server.port=8084

At this time, you need to switch to the development environment inside, you can use the following instructions in the main configuration file

Local environment server.port=8081
switch to development environment spring.profiles.active=dev

  1. yml multi-document block mode
    In the xxx.yml configuration file, each used — segmentation means that it is divided into a document block, which can be configured in different document blocks, and the configuration can be switched in the first document block

Configure a multi-document block yml file

server:
    port: 8081
spring:
    profiles:
        active: test	# 切换配置
---
# 开发环境
server:
    port: 8082
spring:
    profiles: dev
---
# 测试环境
server:
    port: 8083
spring:
    profiles: test
---
# 生产环境
server:
    port: 8084
spring:
    profiles: prop

At this time, you need to switch the configuration in the first multi-document block. When you switch to a certain configuration, all the configurations under that configuration (document block) can take effect

4Activate the specified profile
①. Activate in the configuration file
For configuration in the form of xxx.properties, use it in the main configuration file

# 切换到xxx配置
spring.profiles.active=xxx

Corresponding to the configuration in the form of xxx.yml, used in the first code block

# 切换到xxx配置
spring:
    profiles:
        active: xxx

②. Use the command line to activate
We can set in IDEA's Program arguments--spring-profiles.active=dev
Insert picture description here
We can also configure JVM parameters, specifically in IDEA's VM options setting -Dspring.profiles.active=prop
Insert picture description here

Guess you like

Origin blog.csdn.net/mmmmmlj/article/details/108955284
Recommended