Multi-environment development in Springboot

Table of contents

Multi-environment development (yaml version)

Multi-environment development (yaml version) multi-configuration file format

1. The main startup configuration file application.yml 

2. Environment classification configuration file application-pro.yml

3. Environment classification configuration file application-dev.yml

4. Environment classification configuration file application-test.yml

Multi-environment development (properties version) multi-configuration file format

Multi-environment group management

include attribute

group attribute

 Control in maven


Multi-environment development (yaml version)

yml configuration file



#启动指定环境
spring:
  profiles:
    active: dev

---
#设置生产环境
spring:
  profiles: pro

server:
  port: 88
---
#设置开发环境
spring:
  profiles: dev

server:
  port: 89
---
#设置测试环境
spring:
  profiles: test

server:
  port: 90

test run 

summary 

 1. Multi-environment development needs to set up a variety of common environments, such as development, production, and test environments

2. Set multi-environment use in yaml format --- distinguish the boundaries of environment settings

3. The difference between each environment is different from the loaded configuration properties

4. To enable a certain environment, it is necessary to specify the environment to be used at startup

Multi-environment development (yaml version) multi-configuration file format

Split into four separate yml configuration files

1. The main startup configuration file application.yml 

spring:
  profiles:
    active: dev

2. Environment classification configuration file application - pro.yml

server:
  port: 80

3. Environment classification configuration file application - dev.yml

server:
  port: 81

4. Environment classification configuration file application - test.yml

server:
  port: 82

 tested

 Multi-environment development configuration file description:

Set common configuration (global properties) in the main configuration file

It is common to set the respective properties (local) in the environment classification configuration file

Separate configuration files facilitate maintenance updates.

Multi-environment development (properties version) multi-configuration file format

It is basically the same as yml, just change the suffix of the yml configuration file to properties and follow the grammatical format

like:

server.port=9090

Note: properties support multi-file configuration, not a single file (that is, development and test runs are placed in a configuration file)

Multi-environment development independent configuration file writing skills

Multi-environment group management

Split the information in the configuration file according to the function and make it into an independent configuration file. The naming rules are as follows

application-devDB.yml

application-devRedis.yml

application-devMVC.yml

include attribute

 in dev

server:
  port: 8010

devDB

server:
  port: 8011

devMVC

server:
  port: 8012
  servlet:
    context-path: /ok

In include , active is the main configuration, and the content is loaded last , and the same attribute loaded later will overwrite the one loaded earlier, and the loading order is also from left to right.

group attribute

After SpringBoot2.4, the group attribute is supported, and the group attribute is used instead of the include attribute, which reduces the amount of configuration writing

Use the group attribute to define the inclusion relationship of various main environments and sub-environments.

spring:
  profiles:
    active: dev
    group:
      "dev": devDB,devMVC
      "pro": proDB,proMVC

operation result: 

 The order of loading is that the main configuration loads the rest of the configuration first from left to right, so there are identical parts to see the rightmost value

 Control in maven

 <profiles>
        <profile>
            <id>env_dev</id>
            <properties>
                <profile.active>dev</profile.active>
            </properties>
            <activation>
                <!--  <activeByDefault>true</activeByDefault>-->
            </activation>
        </profile>

        <profile>
            <id>env_pro</id>
            <properties>
                <profile.active>pro</profile.active>
            </properties>
            <activation>
<!--                默认开启-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

    </profiles>

application.yml 

spring:
  profiles:
    active: @profile.active@
    group:
      "dev": devDB,devMVC
      "pro": proDB

When maven and SpringBoot control multiple environments at the same time, Maven is the main

SpringBoot uses the @..@ placeholder to read the configuration property value corresponding to Maven

Based on the premise that SpringBoot reads the Maven configuration properties, if the Idea test engineer pom.xml needs to be manually compiled every time it is updated, it can take effect

Guess you like

Origin blog.csdn.net/weixin_60719453/article/details/127222719