In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use profile to achieve multi-environment configuration in springcloud". In the daily operation, I believe that many people have doubts about how to use profile to achieve multi-environment configuration in springcloud. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use profile to achieve multi-environment configuration in springcloud". Next, please follow the editor to study!
A basic introduction to using profile to realize Multi-environment configuration
In the process of development, our project will have different running environments, such as development environment, test environment and production environment, while some configurations of our project may be different in different environments. for example, data source configuration, log file configuration, and some basic configurations in the running process of the software, so every time we deploy the software to a different environment, we need to modify the corresponding configuration file. If you modify it back and forth in this way, it is easy to make mistakes and a waste of labor.
The profile name that springcloud will access by default is application.properties
If we want to create a multi-environment configuration file, the file name format should be: application- {profile} .properties
{profile} is used to identify different environments, such as application-native.properties files can be used to configure the local environment, application-prod.properties files can be used to configure the production environment.
{profile} is specified in springcloud through the "spring.profiles.active" attribute, such as spring.profiles.active=native, the application-native.properties configuration file is used.
Because the multi-environment configuration of springcloud configuration center and springboot did not get through.
So using the java-jar xxxx.jar-- spring.profiles.active=prod command is only valid for configurations in the springboot project
It is not possible to get different environment configurations from the configuration center, and more work needs to be done to achieve the goal.
Project configuration
Configure the configuration center in the bootstrap.yml file, as follows
Separate different environments with three dashes so that multiple environment configurations can be completed in one file
Spring: profiles: active: dev---spring: profiles: dev cloud: bootstrap: enabled: false config: uri: http://localhost:8888 name: webclient profile: dev---spring: profiles: test cloud: bootstrap: enabled: false config: uri: http://localhost:8888 name: webclient profile: test---spring: profiles: prod cloud: bootstrap: Enabled: false config: uri: http://localhost:8888 name: webclient profile: prod
The configuration in the top-level pom is as follows:
Spring profile multi-environment configuration management
Configuration of different environment files such as local, test, development, product, etc.
Phenomenon
If you do some database testing during development, you want to link to a tested database to avoid the impact on the development database.
Some configurations at development time, such as the level of log4j logs, are different from the production environment.
Various such requirements make me hope that there is a simple and good way to switch development environments.
Solve
Now spring3.1 also brings us profile, which can easily and quickly switch between environments.
It is also very convenient to use. Just add the following to the applicationContext.xml, and that's fine.
The definition of profile must be at the bottom of the document, otherwise there will be an exception. The structure of the whole xml is roughly like this to activate profile
Spring provides us with a large number of methods to activate profile, which can be activated through code, or through system environment variables, JVM parameters, servlet context parameters to define spring.profiles.active parameters to activate profile, here we define JVM parameters.
1. ENV method:
ConfigurableEnvironment.setActiveProfiles ("test")
2. JVM parameter method:
Add JAVA_OPS to catalina.bat in tomcat ("set" is not used in .sh). Select a different profile by setting active
Start tomcat in the set JAVA_OPTS= "- Dspring.profiles.active=test" eclipse. Right click in run as-> run configuration- > Arguments- > VM arguments to add. Local configuration files do not have to upload git tracking management-Dspring.profiles.active= "local"
3. Web.xml method:
Spring.profiles.active production
4. Marking method (junit unit test is very practical):
@ ActiveProfiles ({"unittest", "productprofile"}) at this point, the study on "how to use profile in springcloud to achieve multi-environment configuration" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.