This blog will introduce you to multi-environment configuration, Swagger and more.

 

Multi-environment configuration analysis

In real development, we might involve environments such as Local (local development environment), dev (deployment to remote development environment), test (pre-release environment), and Product (production environment). Obviously, different environment, corresponding to different configuration information, such as DB/REDis/MQ, and even different environment, the program needs to do different processing. So how does the SpringBoot project support multiple environment configurations?

First of all, SpringBoot is an executable program that is launched by java-JAR, so we need to tell SpringBoot what the current profile (environment) is when we start it.

java -jar xxx.jar –spring.profiles.active=test

By doing this –spring.profiles. Active =test tells SpringBoot that we set the profile to test.

A common way to configure multiple environments in real development

Multi-environment Configuration

If you set –spring.profiles.active=test, springBoot will support loading application-test.properties files. If we have a directory to store the configuration files of different environments, then we need to obtain the profile first.

How to get the current profile

Get the profile from the entry class

java -jar xxx.jar –spring.profiles.active=test

In plain English, the String “–spring.profiles. Active =test” is passed to the String[] args of the main method of the entry class!

CommandUtils

how to get profile from args

If a profile is found, set the System key/value via system. setProperty (the key here is “profile”). This is to save the profile for later retrieval.

Notice that if profile is not given, it is automatically set to dev.

Take a look at the specific method for parsing profiles in CommandUtils

parseSpringProfile

The Env enumeration type defined

From here, we can load the profile based on the profile, for example:

Reference to the profile variable set by the system

In the previous section, we set system.setProperty to set the current environment information to the profile variable, so we directly reference ${profile}.

Let’s take a look at the contents of the application.properties file:

Set different boot ports for different profiles

Suppose we want to deploy the application in the Test environment:

profile=test

port=7072

Validation:

Whether profiles can be obtained and whether properties files can be loaded in specific directories

Configuration files in different environment directories

Results:

Obtaining profile authentication

Get the mongo configuration in the test environment

Well, here, the configuration of multiple environments to introduce the completion of ~

 

swagger

Swagger, translated into English, is a bit of a drag, cool meaning, live up to the name!

Swagger is easy to integrate with SpringBoot. It is a powerful API framework that not only provides online document lookup (like Java Doc), but also provides online HTTP testing (like Postman, etc.)

How do you integrate?