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--
In this article, the editor introduces in detail "what is the principle of SpringCloud Config configuration center and environment switching mode". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the principle of SpringCloud Config configuration center and environment switching mode" can help you solve your doubts.
The principle of Config configuration Center and Environment switching
SpringCloud config project, used to provide integrated external configuration support for distributed micro-service systems, divided into client and server
The official introduction of spring is as follows:
Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.
In short: centralized external configuration management is provided for all environments and applications through configuration Service (Config Server). These concepts are abstracted by spring's Environment and PropertySource, so it can be applied to all kinds of Spring applications, and it can also switch and migrate the configuration of application development environment, test environment and generation environment.
Introduction of principle
The git server will pull the configuration file from the remote git and save it to the local git file library. When the remote git is not available, it will pull the configuration information from the local git file library.
1. Config Server introduces dependency org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-eureka
Access the configuration center, so that the client can discover the server, start the class with @ EnableConfigServer,@EnableDiscoveryClient annotation, and name chu-config
Configuration central server will find configuration data according to spring.cloud.config.server.git.uri (it can be location of git repository or local file). This is necessary before Config server can configure new application.yml and chu-user.yml configuration files in remote code cloud repository from remote Git service pull resource.
II. Config client
In the project, basically all the basic micro services are config client, and they all do external configuration, centralized management and dynamic environment switching through config server.
The default pull rules for the client are as follows:
/ {name}-{profile} .yml / {label}-{name}-{profile} .yml
Name: that is spring.application.name
Profile: active section
Label: git branch. Default is master.
For example, set up a chu-user service here:
Introduce client dependency
Org.springframework.cloud spring-cloud-config-client
Register the discovery configuration center server through spring.cloud.config.discovery.enabled=true and spring.cloud.config.discovery.service-id=chu-config in the configuration file
Test:
Above, we introduced the pull rules of the configuration center.
There is a test:123 attribute on the remote code cloud chu-user.yml, which can be obtained through @ Value ("${test}") annotation on the chu-user service. If there is also a test:456 attribute in the configuration file on the chu-user service, by default, Config Server takes precedence.
After testing, we have proved that config client can successfully pull the configuration file of the remote server, so how to do the configuration switch pull in different environments?
1. Modify the chu-user.yml configuration file on the remote code cloud as follows:
-spring: profiles: devisDev: true-spring: profiles: proisDev: false
Restart config server and type localhost:8888/chu-user-dev.yml in the browser to successfully pull the configuration information.
two。 The client specifies the environment configuration to be pulled through spring.cloud.config.profile=pro/dev
Test: start config server and config client and see it in the chu-user service console
Pull the application#pro environment and chu-user#pro environment information respectively, and the program reads the configuration value as false through @ Value ("${isDev}").
Each resource can also choose to store the configuration file in a subdirectory, querying the defined directory through the keyword searchPaths, such as
Spring: cloud: config: server: git: uri: https://gitee.com/China_shenzhen_git/one-config-server search-paths: / config,' {application}'
The configuration file will be queried from the config directory and a directory with the same name as application
If Config server wants the client to be authorized to access the configuration center library, it can introduce security configuration and dependency.
Org.springframework.boot spring-boot-starter-securitysecurity: basic: enabled: true user: name: config-repo password: 123456
Then the client needs to add spring.cloud.config.username=config-repo and password=123456 configuration to authorize access to the configuration central server.
Be careful
Spring profiles separates and switches the configuration of different environment versions. Through spring.profiles.active=dev,mysql, if the configuration file is based on a file, the server will first create an Environment object according to {applicationName} .yml according to application.yml. If there is a specified spring profiles in these yml files, then these profiles will have a higher priority.
Spring.profile.avtive is the environment in which the specified spring boot is running, while spring.cloud.config.profile is the profile configuration in which the client specifies to pull the repository. If there are multiple profiles, the last one works.
Principle and process description of simple configuration Center
The principle and process of the simple configuration center will be described in detail below.
Principle
After startup, it takes precedence over the spring default configuration scanner to add the required configuration items, and spring will read the first value to make it effective.
Detailed explanation of source code
Friends who know about spring know that spring is written in the spring.factories file under the META-INF folder for the default components or some configurations, and the default configuration items for spring are also configured here.
There is a configuration-related configuration in the springMushbootMuth1.5.9.RELEASE.jarfuxamMETARAINFhand spring.faceting file
# Application Listeners listens to org.springframework.context.ApplicationListener=\ org.springframework.boot.context.config.ConfigFileApplicationListener# EnvironmentPost Processors environment to process org.springframework.boot.env.EnvironmentPostProcessor=\ org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\ org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor
This listening class is the key class of the configuration file.
Public class ConfigFileApplicationListener implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {}
As you can see, this snooping implements EnvironmentPostProcessor,SmartApplicationListener and Ordered.
/ / listen to the callback function @ Overridepublic void onApplicationEvent (ApplicationEvent event) {if (event instanceof ApplicationEnvironmentPreparedEvent) {/ / initialize the configuration item onApplicationEnvironmentPreparedEvent ((ApplicationEnvironmentPreparedEvent) event);} if (event instanceof ApplicationPreparedEvent) {onApplicationPreparedEvent (event) }} / / configuration file initialization read private void onApplicationEnvironmentPreparedEvent (ApplicationEnvironmentPreparedEvent event) {/ / load all EnvironmentPostProcessor type bean. At this time, the scan has not yet started, and the EnvironmentPost Processors configured in the spring.factories mentioned above must be obtained. List postProcessors = loadPostProcessors (); / / add your own postProcessors.add (this); / / sort AnnotationAwareOrderComparator.sort (postProcessors); for (EnvironmentPostProcessor postProcessor: postProcessors) {/ / execute postProcessor.postProcessEnvironment (event.getEnvironment (), event.getSpringApplication ()) }} / / load application.yml and specify profile file @ Overridepublic void postProcessEnvironment (ConfigurableEnvironment environment, SpringApplication application) {/ / load addPropertySources (environment, application.getResourceLoader ()); configureIgnoreBeanInfo (environment); bindToSpringApplication (environment, application);}
Configuration center idea
The loading process and priority are clear, so you can start ~
Since the scan has not started at this point, the use of annotations is invalid. Then create a new / META-INF/spring.factories file and add the corresponding configuration, which can be executed before ConfigFileApplicationListener.
Simple construction example
The old rules, will not write detailed steps, but also hope that you can understand the principle, draw examples
Take the modification of the tomcat port as an example, because of the time constraint, only the configuration items are simulated in the code.
Create a new configuration class PropertiesConfigPostProcessor
Public class PropertiesConfigPostProcessor implements EnvironmentPostProcessor, Ordered {/ / takes precedence over ConfigFileApplicationListener public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 9; @ Override public void postProcessEnvironment (ConfigurableEnvironment environment, SpringApplication application) {/ / there is no application configuration item in PropertySources at this time. We have added a configuration with a server.port of 8999. This step can be obtained by itself if it is a real configuration center. Environment.getPropertySources () .addLast (new MapPropertySource ("configServerInitProperties", new QuickHashMap () .quickPut ("server.port", 8999));} @ Override public int getOrder () {return DEFAULT_ORDER;}}
Create a new / META-INF/spring.factories file with the
Org.springframework.boot.env.EnvironmentPostProcessor=\ top.wboost.example.PropertiesConfigPostProcessor
-application.yml configuration
Server: port: 8000
After startup, the log shows that the port is 8999.
We see that the configuration items that exist in the system are as follows (omitting other configuration items)
{"data": {"applicationConfigurationProperties": {"server.port,class org.springframework.boot.context.config.ConfigFileApplicationListener$ConfigurationPropertySources": 8000}, "configServerInitProperties,class org.springframework.core.env.MapPropertySource": {"server.port": 8999}}, "info": {"code": 10906, "message": "execution successful" "systemCode": "DO_OK"}, "status": 0} so far This article "what is the principle of SpringCloud Config configuration center and environment switching mode" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.
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.