当前位置:网站首页>Application configuration management, basic principle analysis
Application configuration management, basic principle analysis
2022-06-24 00:40: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 ;
@FunctionalInterface
public 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 .
边栏推荐
- 【机器学习】线性回归预测
- Hackers can gain administrator privileges by invading Microsoft mail server and windows Zero Day vulnerability November 23 global network security hotspot
- [new cos series] | object storage cos data Vientiane CI processing special data processing resource package 4.5 folded into a big gift package!
- Mip-NeRF:抗混叠的多尺度神经辐射场ICCV2021
- Version ` zlib 1.2.9 "not found (required by / lib64 / libpng16.so.16)
- DO280OpenShift访问控制--管理项目和账户
- [FreeRTOS] 07 binary semaphore and count semaphore
- paddle使用指南
- 智能制造时代下,MES管理系统需要解决哪些问题
- version `ZLIB_ 1.2.9‘ not found (required by /lib64/libpng16.so.16)
猜你喜欢

What do NLP engineers do? What is the work content?

Detailed process from CPU fetching to sending control and microprogram control principle

Usage of go in SQL Server

Android Aidl: cross process call service (Aidl service), kotlininvoke function

NLP工程师是干什么的?工作内容是什么?

skywalking 安装部署实践

钟珊珊:被爆锤后的工程师会起飞|OneFlow U

C语言:关于矩阵右移问题

Index principle and filling factor in database

C语言:结构体数组实现找出最低分学生记录
随机推荐
[iccv workshop 2021] small target detection based on density map: coarse-grained density map guided object detection in aerial images
Empty encoded password警告原因
现在网上开股票账户安全吗?选择国有券商,最快8分钟开户成功
Efficient integration of heterogeneous single cell transcriptome with scanorama
Basic usage of setfacl command
Dart series: using generators in dart
Pure JS implementation determines whether the IP is pinged
牛学长周年庆活动:软件大促限时抢,注册码免费送!
What problems need to be solved by MES management system in the era of intelligent manufacturing
纯js实现判断ip是否ping通
WinSCP和PuTTY的安装和使用
Activity 的 36 大难点,你会几个?,安卓面试2020
毕业设计-论文写作笔记【毕设题目类型、论文写作细节、毕设资料】
2022考研经验分享【初试、择校、复试、调剂、校招与社招】
Index principle and filling factor in database
【小程序】相对路径和绝对路径的表示符
When the IOT network card device is connected to easycvr, how can I view the streaming IP and streaming time?
抖音实战~手机号密码一键注册登录流程(限制手机终端登录)
Accompanist组件库中文指南 - Glide篇,劲爆
[SPRS J P & RS 2022] small target detection module: a normalized Gaussian Wasserstein distance for tiny object detection