SpringBoot multi-environment startup

multi-environment startup

Multi-environment startup basic format

In our development and post-launch environments, the values ​​of some configuration files are different, but we must not modify the configuration files after the project goes online, so we need to configure different configurations for different environments

For example, as follows, in different environments, the connection information of the database is different

insert image description here

We need to configure multi-environment development, so that the project can switch different configuration information in different environments

For easy understanding, here I set different port numbers for the three environments. If the port number changes, it means that the switching environment is successful :

In the application.yml file, three environments are set, and the environments ---are separated by using

server:
  port: 80

---

server:
  port: 81

---

server:
  port: 82

Through the profiles attribute under spring, set the production environment, development environment and test environment respectively

# 开发环境
spring:
  profiles: dev
server:
  port: 80

---
# 生产环境
spring:
  profiles: pro
server:
  port: 81

---
#测试环境
spring:
  profiles: test
server:
  port: 82

Set the current operating environment through the active attribute under the profiles under spring

# 设置启用的环境
spring:
  profiles:
    active: dev

---
# 开发环境
spring:
  profiles: dev
server:
  port: 80

---
# 生产环境
spring:
  profiles: pro
server:
  port: 81

---
#测试环境
spring:
  profiles: test
server:
  port: 82

We have completed the configuration for multi-environment development, but we found that the profiles are struck out, indicating that it is an outdated format

insert image description here

The currently recommended format is as follows

# 设置启用的环境
spring:
  profiles:
    active: test

---
# 开发环境
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 80

---
# 生产环境
spring:
  config:
    activate:
      on-profile: pro
server:
  port: 81

---
#测试环境
spring:
  config:
    activate:
      on-profile: test
server:
  port: 82

The above configuration method is applicable to yml and yaml files, so how to configure the properties file

First set the enabled environment in application.properties

# application.properties

# 设置启用的环境
spring.profiles.active=dev

Create application.dev.properties, application.pro.properties, application.test.properties files respectively

# application.dev.properties

# 开发环境dev
server.port=80
# application.pro.properties

# 生产环境pro
server.port=81
# application.test.properties

# 测试环境test
server.port=82

Multi-environment startup command format

We can package the project into a jar package, and switch and start the environment by command

Before packaging the project into a jar package, we first solve the problem of garbled characters in some configuration files, and set the encoding of the configuration file to UTF-8

insert image description here

In order to avoid affecting our test this time last time, it is recommended to double-click clear

insert image description here

Double-click the package to package

insert image description here

After the packaging is complete, we start the packaged jar package through the command line

It can be found that port 80 is currently started, indicating that the development environment we configured is used

insert image description here

Switch the environment and start the project, for example, switch to the test environment:

  • Start SpringBoot with parameters:java –jar 文件名称.jar –-spring.profiles.active=test

insert image description here

We can also pass parameters to temporarily modify the port:java –jar 文件名称.jar –-server.port=99

insert image description here

Multi-environment startup compatibility

Multi-environment is set in Maven, and multi-environment is also set in SpringBoot. We should let SpringBoot refer to the properties in Maven and let Maven play a leading role

Set up Maven multi-environment in pom.xml file

<profiles>
    <!--开发环境-->
    <profile>
        <id>dev</id>
        <properties>
            <profile.active>dev</profile.active>
        </properties>
    </profile>
    <!--生产环境-->
    <profile>
        <id>pro</id>
        <properties>
            <profile.active>pro</profile.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <!--测试环境-->
    <profile>
        <id>test</id>
        <properties>
            <profile.active>test</profile.active>
        </properties>
    </profile>
</profiles>

Reference Maven properties in SpringBoot

# 设置启用的环境
spring:
  profiles:
    active: ${
    
    profile.active}

---
# 开发环境
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 80

---
# 生产环境
spring:
  config:
    activate:
      on-profile: pro
server:
  port: 81

---
#测试环境
spring:
  config:
    activate:
      on-profile: test
server:
  port: 82

Install the plug-in in the pom.xml file to parse the content in the resource file ( parse the ${} placeholder )

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.2.0</version>
  <configuration>
    <encoding>utf-8</encoding>
    <useDefaultDelimiters>true</useDefaultDelimiters>
  </configuration>
</plugin>

Use maven to package and start the command line

insert image description here

Guess you like

Origin blog.csdn.net/m0_71485750/article/details/128064529