「连载」边缘计算(十三)02-01:边缘部分源码(源码分析篇)

(接上篇)

配置模块初始化

配置模块初始化具体如下所示。

|-----------------------------------------------------------------|
| err := archaius.Init() ... CONFIG = archaius.GetConfigFactory() |

(3) 获取内存配置源

获取内存配置源具体如下所示。

|------------------------------------------------------------------------------|
| ms := memoryconfigsource.NewMemoryConfigurationSource() CONFIG.AddSource(ms) |

(4)获取命令行配置源

获取命令行配置源具体如下所示。

|-----------------------------------------------------------------------------------------|
| cmdSource := commandlinesource.NewCommandlineConfigSource() CONFIG.AddSource(cmdSource) |

(5)获取环境变量配置源

获取环境变量配置源具体如下所示。

|--------------------------------------------------------------------------------------|
| envSource := envconfigsource.NewEnvConfigurationSource() CONFIG.AddSource(envSource) |

(6)根据配置文件路径获取配置源

根据配置文件路径获取配置源具体如下所示。

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| confLocation := getConfigDirectory() + "/conf" _, err = os.Stat(confLocation) if !os.IsExist(err) { os.Mkdir(confLocation, os.ModePerm) } err = filepath.Walk(confLocation, func(location string, f os.FileInfo, err error) error { if f == nil { return err } if f.IsDir() { return nil } ext := strings.ToLower(path.Ext(location)) if ext == ".yml" || ext == ".yaml" { archaius.AddFile(location) } return nil }) ... }) |

目前,KubeEdge所用的读取配置文件的方式是根据配置文件路径获取配置源。下面深入剖析该配置文件读取方式,首先确定配置所在路径。getConfigDirectory()函数定义具体如下所示。

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/beehive/pkg/common/config/config.go |
| //constants to define config paths const ( ParameterConfigPath = "config-path" EnvironmentalConfigPath = "GOARCHAIUS_CONFIG_PATH" ) ... // get the configuration file path func getConfigDirectory() string { if config, err := CONFIG.GetValue(ParameterConfigPath).ToString(); err == nil { return config } if config, err := CONFIG.GetValue(EnvironmentalConfigPath).ToString(); err == nil { return config } return util.GetCurrentDirectory() } |

getConfigDirectory()函数中获取配置文件所在目录的方式有如下3种。

1)根据ParameterConfigPath获取,具体如下所示。

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| // get the configuration file path func getConfigDirectory() string { if config, err := CONFIG.GetValue(ParameterConfigPath).ToString(); err == nil { return config } |

2)根据EnvironmentalConfigPath获取,具体如下所示。

|-----------------------------------------------------------------------------------------------------|
| if config, err := CONFIG.GetValue(EnvironmentalConfigPath).ToString(); err == nil { return config } |

3)根据可执行文件运行所在的当前目录下获取,具体如下所示。

|-----------------------------------|
| return util.GetCurrentDirectory() |

接着分析怎么读入配置文件,具体读入方法如下所示。

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/beehive/pkg/common/config/config.go |
| confLocation := getConfigDirectory() + "/conf" _, err = os.Stat(confLocation) if !os.IsExist(err) { os.Mkdir(confLocation, os.ModePerm) } err = filepath.Walk(confLocation, func(location string, f os.FileInfo, err error) error { if f == nil { return err } if f.IsDir() { return nil } ext := strings.ToLower(path.Ext(location)) if ext == ".yml" || ext == ".yaml" { archaius.AddFile(location) } return nil }) ... }) |

在获取了配置文件所在的目录confLocation后,接着进行如下操作。

1)判断目录是否存在,如果不存在创建该目录,具体如下。

|-------------------------------------------------------------------------------------------------------------------------------------------|
| confLocation := getConfigDirectory() + "/conf" _, err = os.Stat(confLocation) if !os.IsExist(err) { os.Mkdir(confLocation, os.ModePerm) } |

2)遍历配置文件目录下符合条件的文件,加入配置信息源,具体如下。

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| err = filepath.Walk(confLocation, func(location string, f os.FileInfo, err error) error { if f == nil { return err } if f.IsDir() { return nil } ext := strings.ToLower(path.Ext(location)) if ext == ".yml" || ext == ".yaml" { archaius.AddFile(location) } return nil }) ... }) |

到此 ,config.CONFIG就获取了KubeEdge所需配置文件内容。

通过上面的分析,我们知道KubeEdge是通过KubeEdge/beehive/pkg/common/config来读取所需的配置文件的,而KubeEdge/beehive/pkg/common/config封装了archaius(https://GitHub.com/Netflix/archaius)。

未完待续......

点击下方标题可阅读技术文章

「连载」边缘计算(一)01-16:边缘计算系统逻辑架构(原理篇)
「连载」边缘计算(二)01-17:边缘计算系统逻辑架构(原理篇)
「连载」边缘计算(三)01-18:边缘部分原理解析(原理篇)
「连载」边缘计算(四)01-19:边缘部分原理解析(原理篇)
「连载」边缘计算(五)01-22:边缘部分原理解析(原理篇)
「连载」边缘计算(六)01-23:边缘部分原理解析(原理篇)
「连载」边缘计算(七)01-24:边缘部分原理解析(原理篇)
「连载」边缘计算(八)01-25:边缘部分源码(源码分析篇)
「连载」边缘计算(九)01-26:边缘部分源码(源码分析篇)

「连载」边缘计算(十)01-29:边缘部分源码(源码分析篇)
「连载」边缘计算(十一)01-30:边缘部分源码(源码分析篇)
「连载」边缘计算(十二)01-31:边缘部分源码(源码分析篇)

相关推荐
大数据新鸟15 分钟前
操作系统之虚拟内存
java·服务器·网络
Tong Z17 分钟前
常见的限流算法和实现原理
java·开发语言
凭君语未可20 分钟前
Java 中的实现类是什么
java·开发语言
He少年22 分钟前
【基础知识、Skill、Rules和MCP案例介绍】
java·前端·python
克里斯蒂亚诺更新32 分钟前
myeclipse的pojie
java·ide·myeclipse
迷藏4941 小时前
**eBPF实战进阶:从零构建网络流量监控与过滤系统**在现代云原生架构中,**网络可观测性**和**安全隔离**已成为
java·网络·python·云原生·架构
迷藏4941 小时前
**发散创新:基于Solid协议的Web3.0去中心化身份认证系统实战解析**在Web3.
java·python·web3·去中心化·区块链
qq_433502181 小时前
Codex cli 飞书文档创建进阶实用命令 + Skill 创建&使用 小白完整教程
java·前端·飞书
IT_陈寒1 小时前
为什么我的Vite热更新老是重新加载整个页面?
前端·人工智能·后端
safestar20121 小时前
ES批量写入性能调优:BulkProcessor 参数详解与实战案例
java·大数据·运维·jenkins