[Spring Boot] System configuration of Spring Boot - actual combat: realize system multi-environment configuration

Actual combat: Realizing multi-environment configuration of the system

In the process of actual project development, it is necessary to face different operating environments, such as development environment, test environment, production environment, etc. The database and Redis server configurations of each operating environment are different. Relevant system configurations need to be modified manually. This method is particularly troublesome, time-consuming and labor-intensive, and the probability of error is extremely high. Fortunately, Spring Boot provides us with a simpler and more convenient configuration solution to solve the multi-environment configuration problem. Let's demonstrate how the Spring Boot system implements multi-environment configuration.

1. Multi-environment configuration

Usually the application system may run in the development environment (dev), test environment (test), and production environment (prod), so how to achieve flexible configuration and fast switching of multiple operating environments? Spring Boot provides a minimal solution. Only simple configuration is required, and the application system can flexibly switch the configuration of the operating environment.

1.1 Create a multi-environment configuration file

When creating a multi-environment configuration file, you need to follow the naming convention allowed by Spring Boot. The format is application-{profile}.properties, where {profile} is the corresponding environment identifier. Create three configuration files application-dev.properties, application-test.properties and application-prod.properties in the project resources directory, corresponding to the development environment, test environment and production environment.

According to the three common operating environments in the application system, it is divided into multiple different configuration files, and the configuration items of each of the above operating environments are independently configured. Specifically as follows:

  • application.properties is the main configuration file of the project, which contains all the public configurations required by the project.
  • application-dev.properties is the development environment configuration file, which contains the individual configuration required by the project.
  • application-test.properties is the test environment configuration file.
  • application-prod.properties is the production environment configuration file.

1.2 Modify the configuration file

Usually, the databases used in the development environment, test environment, and production environment are different, so the configuration of different environments in different environments is used as an example to demonstrate the configuration of multiple environments.

First, modify application.properties to configure the startup port of the system:

# 服务器端口配置
server.port=8088

In the above example, application.properties contains all the public configurations required by the project, where the startup port of the system is configured, and the startup port of all environments is 8088.

Then, modify the configuration of the application-dev.properties development environment and add the connection configuration of the database. The code example is as follows:

# 指定数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 数据库jdbc连接urL地址
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/myapp_dev
# 数库账号
spring.datasource.username=root
spring.datasource.password=root

Configure the relevant properties of the database connection, we see that the database configured in the development environment is myapp_dev.

The configuration files of other environments can modify the corresponding configuration connections, and the multi-environment configuration of the above projects is completed. Next, we will demonstrate switching the project operating environment.

2. Switching between multiple environments

We talked about how to configure multiple environments, so how to switch the system operating environment during the actual test and operation? This is also very simple, activate the corresponding operating environment by modifying the spring.profiles.active configuration item in the application.properties configuration file. If no profile configuration file is specified, Spring Boot will start application-default.properties (default environment) by default.

There are three ways to specify the startup environment of the project:

(1) The configuration file specifies the project startup environment

Spring Boot supports configuring the project startup environment through spring.profiles.active, and adding the following configuration items to the application.properties configuration file to specify the corresponding environment directory:

# 系统运行环境
spring.profiles.active=dev

In the above example, the operating environment of the system is configured by setting the configuration item spring.profiles.active in the application.properties configuration file. The dev development environment is configured here.

(2) The IDEA compiler specifies the project startup environment

Generally, when IDEA starts, configure the project startup environment directly on the Run/debug Configuration page of IDEA.

insert image description here

When the project is debugged and running, the IDEA compiler can set the startup mode through three parameters: VM options, Program arguments, and Active profiles.

(3) Start the specified project environment from the command line

When starting the project through the java -jar command on the command line, you need to specify the startup environment as follows:

java -jar xxx.jar --spring.profiles.active=dev

As shown above, after the program is packaged, it can be started on the command line using java -jar, and the startup parameter spring.profiles.active=dev is set, with the development environment as the default configuration. After the configuration is completed in application-{profile}.properties, when starting the project, you can see which environment configuration file is loaded in the system startup log, as shown in the figure.

insert image description here

In the startup log above, you can see that the system is currently starting the dev development environment. If you implement the relevant database testing methods, you can verify whether the corresponding database operations are in effect.

Guess you like

Origin blog.csdn.net/weixin_45627039/article/details/131766269