Spring Boot multi-environment deployment

Spring Boot allows the preparation of multiple configuration files. You can specify which configuration file to use to override the default application.properties during system deployment to complete multi-environment deployment.

A configuration file of application-{profile}.properties needs to be created under resource, where profile can be any name, for example:

profile illustrate
test Indicates the test environment
prod Indicates the online environment
pre-prod Represents a pre-release environment
demo1.0 Version 1.0 demo environment

These configuration files can add or override properties of the application.properties file.

In the environment variable, spring.profile.active specifies which profile to use, for example:

java -jar -Dspring.profiles.active=prod target/demo-0.0.1.jar
java -jar -Dspring.profiles.active=prod target/demo-0.0.1.jar --server.port=8080
​java -jar target/demo-0.0.1.jar --spring.profiles.active=prod --server.port=8080
#后台运行​
nohup java -jar target/demo-0.0.1.jar --spring.profiles.active=prod > /dev/null 2>&1 &

After the above configuration is successful, Spring Boot will read the resource/application-prod.properties configuration file and override the default application.properties options.

The content of application.properties is as follows:

server.port=8080
...

The content of application-prod.properties is as follows:

server.port=9000
...

If deploying in war mode, adding system properties is a better way. The following takes Tomcat as an example to illustrate.

Edit catalina.sh and add the following at the beginning of the sh file:

JAVA_OPTS="-Dspring.profiles.active=prod"

In multi-environment deployment, there may be no configuration files for the target environment in the resource directory. This is mainly for security reasons. The development environment should not have various configuration information of the online environment. You can put the configuration file in a specific directory, and use spring.config.location to specify the directory of the configuration file:

java -jar -Dspring.config.location=file:env/ -Dspring.profiles.active=test target/demo-0.0.1.jar

The configuration file is located in the env directory of the current directory, and the profile is test, so the application-test.properties configuration file is read.

Note: Regardless of the multi-environment configuration method above, the existing application.properties will always be overwritten.

How Spring Boot finds configuration files:

The Spring Boot application reads the application.properties file by default. In fact, Spring Boot will automatically search for the files in the following directories, the priority is from low to high, and the priority of file:config/ is the highest.

File Directory
classpath:
classpath:/config
file:
file:config/ 

This is the default configuration of the system property spring.config.location. spring.config.name represents the name of the configuration file, the default is application.

 

@Profile annotation

The system property spring.profiles.active can specify which profile to use. If test is specified, application-test.properties will override the default application.properties.

The @Profile annotation can be used in conjunction with @Configuration and @Component to determine whether the configuration class will take effect.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

@Configuration
public class DataSourceConf {
    
    @Bean(name="dataSource")
    @Profile("test")
    public DataSource testDataSource(Environment env) {
    	HikariDataSource test = getDataSource(env);
    	test.setMaximumPoolSize(10);
    	return test;
    }

    @Bean(name="dataSource")
    @Profile("prod")
    public DataSource testDataSource(Environment env) {
        HikariDataSource test = getDataSource(env);
        test.setMaximumPoolSize(200);
        return test;
    }
    
    private HikariDataSource getDataSource(Environment env) {
        HikariDataSource ds = new HikariDataSource();
        // 省略初始化代码
        return ds;
    }
    
}

The @Profile annotation can support the use of multiple profiles, or you can use "!" to exclude specific profiles.

@Profile({"test", "prod"}) Test environment and online environment take effect

@Profile({"test", "!prod"}) Test environment and offline environment take effect

Guess you like

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