当前位置:网站首页>Application configuration management, basic principle analysis
Application configuration management, basic principle analysis
2022-06-24 06:55:00 【A cicada smiles】
The project can be a little messy , But the configuration cannot be ambiguous ;
One 、 Configuration architecture
In the code engineering of microservices , Configuration management is a complex thing , That is, it is necessary to take configuration isolation measures for each environment , You also need to ensure that the configuration of the production environment is secure ; If enough microservices are divided , Also consider the efficiency of configuring updates ;

Under normal circumstances , In the system of configuration management , There are four main environments : Development 、 test 、 Grayscale 、 production ; Generally speaking, in addition to the O & M team , Other personnel do not have permission to view grayscale and production configuration , To ensure the security of the configuration ; Two sets of services will be set up in the configuration center : R & D and production are deployed independently .
Two 、 Configuration mode
There are many configurations involved in the project , There are many types , But from the big structure, it can be divided into : Engineering level 、 Application level 、 Component level three block ; In fact, this is just a simple view of configuration from the perspective of code engineering , If continuous integration 、 If automation related modules are also taken into account , Configuration management will be more complex ;

From a development perspective , It mainly manages the application level configuration , In the framework of microservices , There are many different services with the same configuration , There are also various differentiated user-defined parameters , Therefore, there is a certain complexity in maintenance ;

In a single service project , Only one... Will exist in the application bootstrap.yml The configuration file , The configuration content is basically the service name , And configuration center address , Other complex configurations are closed for maintenance , Avoid security problems caused by core content leakage ;

The configuration body is usually split into the following levels : environmental control , Used to identify grayscale and production ; Application basis , Manage and load common configurations for each service ; Service differences are configured in separate files ; And manage multiple configurations by categories and layers ; So as to ensure the safety of configuration and reduce the difficulty of maintenance .
3、 ... and 、Nacos To configure
First of all, from bootstrap.yml File as a starting point , Take the common Nacos Component as an example , Focus on the basic principles , To analyze how the service project is loaded Nacos Configuration center parameters ;
spring: profiles: active: dev,facade cloud: nacos: config: prefix: application server-addr: 127.0.0.1:8848 file-extension: yml
Component configuration : Configuration logic , A single server provides information about its own configuration parameters , From the source code of service management in the previous article , This is a very common method ; Based on this information, we need to Nacos Load the configuration in the service ;
@ConfigurationProperties("spring.cloud.nacos.config")public class NacosConfigProperties { public Properties assembleConfigServiceProperties() { Properties properties = new Properties(); properties.put("serverAddr", Objects.toString(this.serverAddr, "")); properties.put("namespace", Objects.toString(this.namespace, "")); return properties ; }}Load logic : Service startup , First read based on the corresponding parameters Nacos Configured in , The data is then parsed and Spring Frame to load , The loading process passes MapPropertySource classes Key and Value The read ;
public class NacosPropertySourceBuilder { private List<PropertySource<?>> loadNacosData(String dataId, String group, String fileExtension) { // Query configuration String data = this.configService.getConfig(dataId, group, this.timeout); // Parsing configuration return NacosDataParserHandler.getInstance().parseNacosData(dataId, data, fileExtension); }}Request service : The service side and Nacos Center pass Http Request to interact , adopt Get Request to carry parameters , call Nacos Central Services , So as to obtain the corresponding configuration ;
public class ClientWorker implements Closeable { public String[] getServerConfig(String dataId, String group, String tenant, long readTimeout) { // Core parameters Map<String, String> params = new HashMap<String, String>(3); params.put("dataId", dataId); params.put("group", group); params.put("tenant", tenant); // Perform the requested HttpRestResult<String> result = agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), readTimeout); }}Four 、Spring load
From the structure diagram of the source code , The loading logic of the configuration file is also implemented using the event model , This is described in detail in the previous task management ; The core function of configuration is to load and boot when the program starts , The key here is to understand EnvironmentPostProcessor Interface design of ;

public class ConfigFileApplicationListener implements EnvironmentPostProcessor, SmartApplicationListener, Ordered { // Basic configuration private static final String DEFAULT_NAMES = "application"; public static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active"; public static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include"; static { Set<String> filteredProperties = new HashSet<>(); filteredProperties.add("spring.profiles.active"); filteredProperties.add("spring.profiles.include"); LOAD_FILTERED_PROPERTY = Collections.unmodifiableSet(filteredProperties); } // Load logic void load() { FilteredPropertySource.apply(this.environment, DEFAULT_PROPERTIES, LOAD_FILTERED_PROPERTY, (defaultProperties) -> { }); }}EnvironmentPostProcessor Interface : Load the custom environment of the application ;
@FunctionalInterfacepublic interface EnvironmentPostProcessor { void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application);}SpringApplication: Used to start and boot applications , Provides creation of program context instances , Initialize listener , Container refresh and other core logic , Can revolve around run() Method to debug and analyze ;
ConfigurableEnvironment: The core interface of environment configuration , Involves identification of the current configuration file , namely profiles.active; And the ability to parse configuration files , namely PropertyResolver; stay Loader The inner class provides the implementation of construction methods and loading logic .
5、 ... and 、 Reference source code
Application warehouse :https://gitee.com/cicadasmile/butte-flyer-parent Component encapsulation :https://gitee.com/cicadasmile/butte-frame-parent边栏推荐
- Do you know about Statistics?
- Reasons for automatic allocation failure of crawler agent IP
- On BOM and DOM (1): overview of BOM and DOM
- How to choose CMS website system for website construction
- Thread safety and its implementation
- Project demo
- leetcode:84. 柱状图中最大的矩形
- Why does the remote end receive a check-out notice when the TRTC applet turns off audio and video locally
- Easy car Interviewer: talk about MySQL memory structure, index, cluster and underlying principle!
- About Stacked Generalization
猜你喜欢

Record -- about the problem of garbled code when JSP foreground passes parameters to the background

C language student management system - can check the legitimacy of user input, two-way leading circular linked list
![Command ‘[‘where‘, ‘cl‘]‘ returned non-zero exit status 1.](/img/2c/d04f5dfbacb62de9cf673359791aa9.png)
Command ‘[‘where‘, ‘cl‘]‘ returned non-zero exit status 1.

C语言学生管理系统——可检查用户输入合法性,双向带头循环链表

About Stacked Generalization

创客教育给教师发展带来的挑战

Oracle SQL comprehensive application exercises

Rockscache schematic diagram of cache operation

Localized operation on cloud, the sea going experience of kilimall, the largest e-commerce platform in East Africa

File system notes
随机推荐
Localized operation on cloud, the sea going experience of kilimall, the largest e-commerce platform in East Africa
Go current limiting component package rate source code analysis
Innovating the security service mode, deeply convinced that the organization has been equipped with a "continuous online expert group"
Actual combat | how to deploy flask project using wechat cloud hosting
解读AI机器人产业发展的顶层设计
Code scanning | a sharp tool for controlling code quality
How to register the cloud service platform and what are the advantages of cloud server
创客教育给教师发展带来的挑战
The gadgets developed by freshmen are popular. Netizen: my food is good
Do you know about Statistics?
Reasons for automatic allocation failure of crawler agent IP
Why computers often crash
Kubernets traifik proxy WS WSS application
Easyrtc call error `failed to execute'send'on'rtcdatachannel'
Kangaroo cloud: the overall architecture and key technical points of building a real-time computing platform based on Flink
leetcode:84. The largest rectangle in the histogram
Centos7 deploying mysql-5.7
leetcode:85. 最大矩形
数据库 存储过程 begin end
目标5000万日活,Pwnk欲打造下一代年轻人的“迪士尼乐园”