Use Spring Boot Profile to achieve multi-environment configuration

1 Introduction

In the back-end development of Java applications, with the continuous development of the project and the continuous refinement and addition of application requirements, the code in the engineering project is more and more, the project structure is more and more complex, and the project progress will encounter various problems :

  • The code of different aspects is coupled with each other. At this time, once the application has a problem, it is difficult to locate the cause of the problem. Even if the problem is located, it is difficult to fix the problem. It may introduce more problems when fixing the problem;
  • The multi-faceted code is concentrated in a whole structure. It is difficult for new developers to join the team to have an intuitive experience of the overall project, which increases the cost of novice involvement in development. A developer familiar with the entire project needs to maintain the structure of the entire project This is usually difficult to do when the project is large and the development time is long).
  • The boundary of code that developers are responsible for themselves or others is very vague. This is the most easily encountered in complex projects. The result is that developers can easily modify the code that other people are responsible for and the code owner does not know it. .

Therefore, splitting a complex project into multiple modules is an important method to solve the above problems. The division of multiple modules can reduce the coupling between codes (from class-level coupling to jar-level coupling), and each The functional positioning of the module is relatively clear (through the module name or module documentation). At the same time, the module also regulates the division of the code boundary. It is easy for developers to determine their own responsibility through the functional module.

However, some problems will be introduced through the project's multi-module split. There are many related configuration files related to application configuration in the project. The application may have different configurations in different environments, such as database connection, log level, etc. Testing and production may have different configurations in each environment, which will bring a lot of trouble to the system operation and maintenance.

With the emergence and wide application of the Spring Boot development framework, Spring Boot is supported and applied by the majority of developers due to its ability to support developers' rapid development and configuration. Spring Boot's Profile can realize configuration switching in multiple scenarios, which is convenient for testing and deploying production environments during development. The following will briefly introduce how to use the Spring Boot Profile configuration file to configure configuration files for different environments.

2. Spring Boot configuration steps

  1. Add configuration item application.yml in application.yml or application.properties under Spring Boot project
spring:
  profiles:
    active: dev

application.properties

spring.profiles.active: dev

spring.profiles.active: dev means that the default configuration is the configuration of the development environment. If dev is replaced by test, the properties of the test environment will be loaded, and so on.

Note: If spring.profiles.active does not specify a value, then only the value that does not specify the spring.profiles file will be used, that is, only the common configuration will be loaded, that is, Spring Boot will only load the common application.yml or application.properties Configuration.

  1. Create configuration files in different environments

For example, the environment is divided into development environment, test environment and production environment, create the following files:

  • Development environment: application-dev.yml or application-dev.properties
  • Test environment: application-test.yml or application-test.properties
  • Production environment: application-prod.yml or application-prod.properties
    application.yml file is divided into four parts, use — as a delimiter, the first part of the common configuration part, which represents the common properties of the three environments, specified with spring.profiles A value (development for dev, test for test, and production for prod) is displayed. This value indicates in which profile the configuration should be used.

For example, we configure different database information according to different environments in a project:

  • Development environment
spring:
  datasource:
     name: druidDataSource
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.1.2:3306/myDB?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&useSSL=false
      username: user1
      password: 123456
      filters: stat,wall,config
      max-active: 50
      initial-size: 1
      max-wait: 6000
      min-idle: 1
      time-between-eviction-runs-millis: 6000
      min-evictable-idle-time-millis: 30000
      validation-query: select 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-open-prepared-statements: 50
      max-pool-prepared-statement-per-connection-size: 20
  • test environment
spring:
  datasource:
     name: druidDataSource
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.20.2:3306/myTestDB?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&useSSL=false
      username: user1
      password: 123456
      filters: stat,wall,config
      max-active: 150
      initial-size: 1
      max-wait: 10000
      min-idle: 1
      time-between-eviction-runs-millis: 20000
      min-evictable-idle-time-millis: 100000
      validation-query: select 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-open-prepared-statements: 100
      max-pool-prepared-statement-per-connection-size: 20

Production Environment

spring:
  datasource:
     name: druidDataSource
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://172.1.16.2:3306/myProdDB?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&useSSL=false
      username: prod1
      password: prod1234!@#
      filters: stat,wall,config
      max-active: 100
      initial-size: 1
      max-wait: 60000
      min-idle: 1
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: select 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-open-prepared-statements: 50
      max-pool-prepared-statement-per-connection-size: 20
  1. After packaging the application, start the application

If it is deployed to the server, we normally pack it into a jar package. When starting the application, Spring Boot loads the configuration information of the relevant environment through the spring.profiles.active configuration item in the application.yml or application.properties file . In addition, we can control which environment configuration is loaded by –spring.profiles.active = xxx , the reference command is as follows:

# 表示使用开发环境的配置
java -jar xxx.jar --spring.profiles.active=dev 

# 表示使用测试环境的配置
java -jar xxx.jar --spring.profiles.active=test 

# 表示使用生产环境的配置
java -jar xxx.jar --spring.profiles.active=prod 
  1. Add extended profile information

In complex projects, we may need to add some additional extended configuration information. Spring Boot supports the project to add extended configuration files. Suppose we configure a configuration file application-ldap that requires LDAP authentication in a function module hurricane -ldap. yml , we can modify the spring.profiles.active configuration item in the application.yml or application.properties file as follows:

 spring:
     profiles:
        active: dev,ldap

3. Multi-environment configuration packaging through Mavan

  1. Add multi-environment configuration in pom.xml file
<!-- Application Environment Setting -->
<profiles>
     <profile>
         <id>dev</id>
         <activation>
             <!-- Default Active Without Assign Parameter -->
             <activeByDefault>true</activeByDefault>
         </activation>
         <properties>
             <profileActive>dev</profileActive>
         </properties>
     </profile>
     <profile>
         <id>test</id>
         <properties>
             <profileActive>test</profileActive>
         </properties>
     </profile>
     <profile>
         <id>prod</id>
         <properties>
             <profileActive>prod</profileActive>
         </properties>
     </profile>
</profiles>

Note: In the configuration file, add the configuration of the three environments of development, test and production, which should pay attention to the profileActive custom configuration item. This configuration item indicates the name of the application configuration file. This configuration item will be applied in application.yml or application.properties .

  1. Modify applcation.yml or application.properties configuration items

Modify applcation.yml or application.properties configuration items to replace spring.profies.active configuration items as follows:

spring:
    profiles:
       active: @profileActive@,ldap

Note: @ profileActive @ is a custom configuration item configured in the pom.xml file in the previous step. This parameter can be named and configured according to the developer's own habits.

Use maven command to package into an application package of the corresponding environment

#生产环境
mvn clean package -Pprod -U  
# 或者
mvn clean package -DprofileActive=prod -U
#########################################
#测试环境
mvn clean package -Ptest -U  
# 或者
mvn clean package -DprofileActive=test -U
#########################################
#开发环境
mvn clean package -Pdev -U  
# 或者
mvn clean package -DprofileActive=dev -U

Author: garyond
link: https: //www.jianshu.com/p/07bd7720b0c4
Source: Jane books
are copyrighted by the author. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.

Published 17 original articles · Likes0 · Visits 210

Guess you like

Origin blog.csdn.net/neheqi/article/details/105495316