In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the profile function of Spring boot to achieve automatic switching of multi-environment configuration, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Usually, server-side application development needs to go through the following processes:
Development-> Test-> RC Verification-> launch
This involves four different environments, the development environment, the test environment, the RC environment and the production environment. In order to avoid mutual interference between different environments, it is usually necessary to deploy databases and cache servers independently, so the application configuration should be adjusted accordingly.
In order to solve the problem of configuration switching in different environments, what many people do is to put the configuration files into different directories or files according to different environments, package them through gradle or maven, and specify which environment package to hit through command line parameters. This allows you to generate different packages for different environments. However, this approach has the following problems:
Gradle or maven packaging script files need to repeat the logic of "select file" packaging, which increases the success of a lot of repetitive work.
In the integrated environment such as jenkins, we need to make corresponding settings for each application and different environment.
You need to manage the cost of packages in different environments; when deploying, you need to pay attention to whether the packages are consistent with the running environment
If you are running in docker, you need to build corresponding images for different environments because of different packages.
At this time, some people may say, take the configuration out of the package and put it in the configuration center, but the address of the configuration center is also different in different environments.
What is Profile in Spring?
The Profile function in Spring actually came out as early as the version of Spring 3.1. it can be understood as the logical group name of the Bean that we defined in the Spring container. Only when these Profile are activated will the corresponding Bean in the Profile be registered with the Spring container.
To take a more specific example, Bean, as we defined before, when the Spring container starts, it loads all this information to complete the creation of the Bean. After using Profile, it will divide the definition of Bean into more fine-grained, and divide these defined Bean into several different groups. When the Spring container loads configuration information, it first looks for the active Profile, and then only loads the Bean information defined in the activated group, while the Bean definition information defined in the Profile that is not activated will not be loaded for creating Bean.
In order to use different environments, we first define the corresponding profile names for different environments.
For example, the profile of the development environment is: the profile of the dev; test environment is: the profile of the test;RC environment is: the profile of the rc; production environment is: prod.
Here is an example of how to use different configurations in different dubbo environments:
In the above example, when the corresponding profile is activated, the corresponding configuration file is imported.
For example, import dubbo-dev.properties when profile is dev.
Note: all spring xml schema versions must be above 4.0, such as http://www.springframework.org/schema/util/spring-util-4.3.xsd. The default profile of spring is default. If profile is not specified, it will be defaulted to default.
The above configuration can be simpler if we use the configuration center. We will discuss it after the configuration center is put into production.
Use profile to switch configurations in Spring boot
The default configuration file loaded in Spring boot is: application.properties or application.yml. When profile is activated (we'll discuss how to activate profile later), you can automatically select the loaded application- {profile} .properties or application- {profile} .yml configuration files via profile.
For example, when profile is dev, the configuration of application-dev.properties or application-dev.yml will be loaded in addition to application.properties or application.yml.
In addition, if Spring cloud is introduced, the startup configuration bootstrap.properties or bootstrap.yml and bootstrap- {profile} .properties or bootstrap- {profile} .yml will also be loaded.
So write the common configuration of each environment in application.properties or application.yml. Write configurations for different environments in application- {profile} .properties or application- {profile} .yml.
Use of @ Profile annotations
When using java for configuration, you can use configuration policies in different environments through the @ Profile annotation. For example, swagger is widely used now, but it has certain security problems. If swagger is also exposed in the production environment, the risk is relatively high. It is recommended to enable it only in the development environment and test environment. Configuration examples are as follows:
Save the above code to the logback-spring.xml file instead of logback.xml.
The use of profile in logback
In the development environment or test environment, to facilitate troubleshooting, we will use DEBUG or even TRACE-level logs, while in the production environment, to avoid excessive log growth, try to output only ERROR-level logs. This requires log configuration and the ability to use different configuration policies according to different environments.
Logback in spring boot can meet such requirements, as shown in the following example:
Save the above code to the logback-spring.xml file instead of logback.xml.
Several ways of activating profile by Spring boot
Specify directly in the configuration file
Spring.profiles.active=test
This method is very inflexible and can not be used in the actual development department.
Use placeholders
Replace when packaging, take mavne as an example:
First add to the configuration file:
Spring.profiles.active=@package.target@
Add the configuration of packaging for different environments in pom.xml:
Execute the packaging command:
Mvn package-Ptest
Cons: specify profile for each package
JVM parameter mode
The java command line specifies:
Java-jar app.jar-- spring.profiles.active=dev
Add JAVA_OPS to catalina.bat in tomcat ("set" is not used in .sh). Select a different profile by setting active:
Set JAVA_OPTS= "- Dspring.profiles.active=test"
Start tomcat in eclipse. Right click in run as-> run configuration- > Arguments- > VM arguments to add.
-Dspring.profiles.active= "dev"
In the era of micro-service, will it be a little troublesome?
Web.xml mode
Spring.profiles.active
Prod
Annotation mode (junit unit test is very useful)
@ ActiveProfiles ({"dev"})
ENV mode (recommended)
Set the system environment variable: SPRING_PROFILES_ACTIVE (Note: uppercase)
For example, the method of setting environment variables in the mac development environment:
Vi / .bash_profile
Add the following to ~ / .bash_profile:
Export SPRING_PROFILES_ACTIVE=dev
Note: mac eclipse is the solution for not getting environment variables.
The above things about profile can basically meet the needs of the job. After using profile, you can reduce the configuration management and packaging work caused by configuration differences in different environments.
Try to use environment variables to activate profile, if it is an executable package, you can also use the java command line to specify, other ways are not recommended.
After using profile, it makes it easier for applications to access the configuration center and use docker container technology, so it is very meaningful.
About Spring boot's profile function how to achieve multi-environment configuration automatic switching is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.