当前位置:网站首页>应用配置管理,基础原理分析
应用配置管理,基础原理分析
2022-06-24 06:38:00 【知了一笑】
工程可以有点小乱,但配置不能含糊;
一、配置架构
在微服务的代码工程中,配置管理是一项复杂的事情,即需要做好各个环境的配置隔离措施,还需要确保生产环境的配置安全;如果划分的微服务足够的多,还要考虑配置更新时的效率;

常规情况下,在配置管理的体系中,分为四个主要的环境:开发、测试、灰度、生产;通常来说除了运维团队之外,其他人员没有查看灰度和生产配置的权限,以此来保证配置的安全性;配置中心的服务也会搭建两套:研发与生产各自独立部署。
二、配置方式
在项目中涉及到的配置非常多,类型也很多,但是从大的结构上可以分为:工程级、应用级、组件级三大块;实际上这里只是单纯的从代码工程来看配置,如果把持续集成、自动化相关模块也考虑进来的话,配置的管理会更加复杂;

站在开发的角度来看,主要还是对应用级的配置进行管理,而在微服务的架构下,多个不同的服务既有大量相同的配置,又存在各种差异化的自定义参数,所以在维护上有一定的复杂性;

在单服务的工程中,应用中只会存在一个bootstrap.yml配置文件,配置内容基本就是服务名称,和配置中心地址等常规内容,其他复杂的配置都被封闭维护,避免核心内容泄露引发安全问题;

配置主体通常会被拆分成如下几个层次:环境控制,用来识别灰度和生产;应用基础,管理和加载各个服务的通用配置;服务差异则配置在各自独立的文件内;并且对多个配置进行分类分层管理;以此保证配置的安全和降低维护难度。
三、Nacos配置
首先还是从bootstrap.yml文件作为切入点,以当下常见的Nacos组件为例,围绕基础原理作为思路,来分析服务工程是如何加载Nacos配置中心的参数;
spring: profiles: active: dev,facade cloud: nacos: config: prefix: application server-addr: 127.0.0.1:8848 file-extension: yml
组件配置:配置逻辑中,单个服务端提供自身配置参数的信息,从上篇服务管理的源码中发现,这是一种很常用的手段;需要基于这些信息去Nacos服务中加载配置;
@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 ; }}加载逻辑:服务启动时,先基于相应参数读取Nacos中配置的,然后解析数据并被Spring框架进行加载,加载的过程通过MapPropertySource类进行Key和Value的读取;
public class NacosPropertySourceBuilder { private List<PropertySource<?>> loadNacosData(String dataId, String group, String fileExtension) { // 查询配置 String data = this.configService.getConfig(dataId, group, this.timeout); // 解析配置 return NacosDataParserHandler.getInstance().parseNacosData(dataId, data, fileExtension); }}请求服务:服务端与Nacos中心通过Http请求的方式进行交互,通过Get请求携带参数,调用Nacos中心服务,从而获取相应配置;
public class ClientWorker implements Closeable { public String[] getServerConfig(String dataId, String group, String tenant, long readTimeout) { // 核心参数 Map<String, String> params = new HashMap<String, String>(3); params.put("dataId", dataId); params.put("group", group); params.put("tenant", tenant); // 执行请求 HttpRestResult<String> result = agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), readTimeout); }}四、Spring加载
从源码的结构图来看,配置文件的加载逻辑也是采用事件模型实现的,这在前面任务管理中有详细的描述;配置的核心作用就是在程序启动时进行加载引导,这里关键要理解EnvironmentPostProcessor的接口设计;

public class ConfigFileApplicationListener implements EnvironmentPostProcessor, SmartApplicationListener, Ordered { // 基础配置 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); } // 加载逻辑 void load() { FilteredPropertySource.apply(this.environment, DEFAULT_PROPERTIES, LOAD_FILTERED_PROPERTY, (defaultProperties) -> { }); }}EnvironmentPostProcessor接口:加载应用的自定义环境;
@FunctionalInterfacepublic interface EnvironmentPostProcessor { void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application);}SpringApplication:用于启动和引导应用程序,提供创建程序上下文实例,初始化监听器,容器刷新等核心逻辑,可以围绕run()方法进行调试分析;
ConfigurableEnvironment:环境配置的核心接口,涉及到当前配置文件识别,即profiles.active;以及配置文件的解析能力,即PropertyResolver;在Loader内部类中提供了构造方法和加载逻辑的实现。
五、参考源码
应用仓库:https://gitee.com/cicadasmile/butte-flyer-parent组件封装:https://gitee.com/cicadasmile/butte-frame-parent边栏推荐
- What are the categories of edge computing devices
- How to apply 5g smart pole to smart highway
- Do you know about Statistics?
- Web address domain name IP query method, what is the use of domain name
- leetcode:剑指 Offer 26:判断t1中是否含有t2的全部拓扑结构
- Talk about how to dynamically specify feign call service name according to the environment
- On BOM and DOM (1): overview of BOM and DOM
- 【二叉数学习】—— 树的介绍
- Why does the remote end receive a check-out notice when the TRTC applet turns off audio and video locally
- Centos7 deploying mysql-5.7
猜你喜欢

Rockscache schematic diagram of cache operation
![Command ‘[‘where‘, ‘cl‘]‘ returned non-zero exit status 1.](/img/2c/d04f5dfbacb62de9cf673359791aa9.png)
Command ‘[‘where‘, ‘cl‘]‘ returned non-zero exit status 1.

Localized operation on cloud, the sea going experience of kilimall, the largest e-commerce platform in East Africa
![跳跃游戏II[贪心练习]](/img/e4/f59bb1f5137495ea357462100e2b38.png)
跳跃游戏II[贪心练习]

记录--关于JSP前台传参数到后台出现乱码的问题

Database stored procedure begin end

【二叉数学习】—— 树的介绍

With a goal of 50million days' living, pwnk wants to build a "Disneyland" for the next generation of young people

目标5000万日活,Pwnk欲打造下一代年轻人的“迪士尼乐园”
![[binary number learning] - Introduction to trees](/img/7d/c01bb58bc7ec9c88f857d21ed07a2d.png)
[binary number learning] - Introduction to trees
随机推荐
浅谈如何运营好小红书账号:利用好长尾词理论
Attack and defense enlightenment: chromium component risk analysis and convergence
35 year old crisis? It has become a synonym for programmers
虚拟文件系统
How long will it take for us to have praise for Shopify's market value of 100 billion yuan?
Surveying and mapping principle of GIS coordinate system: geoid / datum / reference ellipsoid /epsg/sri/wkt
How to register the cloud service platform and what are the advantages of cloud server
Why the computer can't start
Wordpress5.8 is coming, and the updated website is faster!
leetcode:剑指 Offer 26:判断t1中是否含有t2的全部拓扑结构
Nine unique skills of Huawei cloud low latency Technology
DHCP server setup
On BOM and DOM (4): dom0/dom2 event handling analysis
Tencent cloud VPC machine, no image when installing monitoring components
Papamelon 11 number of calculation permutation [combinatorics]
数据库 存储过程 begin end
In Tencent, my trial period summary
How to apply 5g smart pole to smart highway
Code scanning | a sharp tool for controlling code quality
Tencent Security jointly established a data security committee