当前位置:网站首页>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边栏推荐
- RS485 serial port wiring description of smart lamp post smart gateway
- leetcode:1856. 子数组最小乘积的最大值
- Localized operation on cloud, the sea going experience of kilimall, the largest e-commerce platform in East Africa
- Virtual file system
- Cloudcompare & PCL point cloud clipping (based on clipping box)
- Easy car Interviewer: talk about MySQL memory structure, index, cluster and underlying principle!
- Distributed cache breakdown
- About Stacked Generalization
- Free and easy-to-use screen recording and video cutting tool sharing
- Overview of cloud computing advantages of using cloud computing
猜你喜欢

【JUC系列】Executor框架之CompletionFuture

oracle sql综合运用 习题

缓存操作rockscache原理图

Application of intelligent reservoir management based on 3D GIS system

leetcode:84. 柱状图中最大的矩形

Nine unique skills of Huawei cloud low latency Technology

文件系统笔记

About Stacked Generalization

原神方石机关解密

leetcode:1856. Maximum value of minimum product of subarray
随机推荐
Surveying and mapping principle of GIS coordinate system: geoid / datum / reference ellipsoid /epsg/sri/wkt
Word cannot copy and paste processing method
文件系统笔记
【二叉数学习】—— 树的介绍
Kubernets traifik proxy WS WSS application
How does easyplayer RTSP configure sending heartbeat information to the server?
Introduction to QWidget attribute table in QT Designer
How long will it take for us to have praise for Shopify's market value of 100 billion yuan?
Replacing human eyes -- visual inspection technology
Programmers use personalized Wallpapers
Talk about how to dynamically specify feign call service name according to the environment
Use of SQL join
Overview of new features in mongodb5.0
Asp+access web server reports an error CONN.ASP error 80004005
Authoritative recognition! Tencent cloud data security Zhongtai was selected as the 2021 pioneer practice case
云上本地化运营,东非第一大电商平台Kilimall的出海经
Internet cafe management system and database
Virtual file system
Do you know about Statistics?
线程安全与实现方法