Springboot switch different configuration files in different environments

The development process is local>test>pre-release>official, so different environments must have different configuration files, so we need to do different configuration switches for different environments.

Let's talk about how springboot switches:

1. Use war in package mode, and deploy the application to tomcat 

Let's take a look at the file structure first,

  file structure

As you can see, here we have 3 properties configuration files, and 1 application.yml file, which has a parameter spring.profiles.active option, which is used to activate the configuration of different environments.

Then the focus is to look at the configuration of the pom.xml below, as follows:

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profileActive>test</profileActive>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <profileActive>pro</profileActive>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application-${profileActive}.properties</include>
                    <include>application.yml</include>
                    <include>logback-spring.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin
        </plugins>
    </build>

 

Then when you build the project, you can write the command mvn clean package -Dmaven.test.skip=true -P dev/test/pro (switching different variable values ​​according to different environments)

 

2. The package method is jar, and the server uses the java -jar command to start

The profiles configuration in pom.xml can be completely deleted, and this configuration in build can also be deleted.

Then activate the configuration by specifying the spring.profiles.active parameter when the project starts

nohup java -jar *.jar -dprocesName=templateDecoration --spring.profiles.active=test > /xxx/xxx/xxx.log &

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325522382&siteId=291194637