Springboot Idea development and testing production multi-environment configuration and packaging

Our project has different configurations in development environment, production environment, and different environments. For example, access to the service port, database configuration, etc. If you change these configuration values ​​every time you switch environments or releases, it is not only troublesome but also easy to make mistakes.
What should I do?
Can you load different configurations through different environments?

Solution:

Method 1: Add multiple configuration files, read different configurations in different environments

1. According to the environment, add different configuration files, as shown in the figure below:

Insert picture description here
The specific configuration content is similar to application.yml, according to different environments, set up different servers, ports, etc.

2. Delete the content of application.yml and add environment configuration

spring:
  profiles:
    active: dev

In this way, when we switch environments and package, we only need to modify application.yml.

But every time you still need to change application.yml, it is still a bit troublesome, we can deal with it through the following advanced version

Method 2: Dynamically configure the environment through profiles

1. According to the environment, add different configuration files, as shown in the figure below:
Insert picture description here

2. Modify pom.xml and set configuration environment profiles (for multi-module projects, modify in the pom of the startup module), which is equal to bulid

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!-- 环境标识,需要与配置文件的名称相对应 -->
                <activatedProperties>dev</activatedProperties>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <activatedProperties>test</activatedProperties>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
            </properties>
        </profile>
    </profiles>

3. Set the content of application.yml and dynamically specify the startup environment

spring:
  profiles:
    active: @activatedProperties@

Note: @activatedProperties@ must be consistent with the profile=>properties=> activatedProperties node

4. Select the startup environment

Insert picture description here
5. Specify environment packaging

Specify the packaging environment to package through commands

clean package -P prod

After packaging, you can check whether application.yml in target\classes is the specified environment

spring:
  profiles:
    active: prod

Guess you like

Origin blog.csdn.net/weixin_41003771/article/details/115114750