【sylar】框架篇-Chapter3-配置模块

本文介绍了从零开始重写的sylar C++高性能分布式服务器框架,使用YAML作为配置文件,支持STL容器和自定义类型。配置变量基类ConfigVarBase包含配置名称和描述,提供了toString()和fromString()方法。此外,文章还展示了如何实现不同数据类型与YAML字符串之间的转换,以及Config类如何管理配置项,包括加载配置文件和监听配置变更的功能。

站在巨人的肩膀上

C++高性能分布式服务器框架

从零开始重写sylar C++高性能分布式服务器框架

概述

  • 从配置文件中加载用户配置,灵活实现用户自己按需配置。
  • 其实最简单的配置文件方式还是 .ini 那种,极其简单。YAML 的好处是支持复杂的数据类型。
  • 这里使用 YAML 作为配置文件,配置名称大小写不敏感,并且支持级别格式的数据类型。
  • 这里支持 STL 容器(vector, list, set, map 等等),支持自定义类型(但需要实现序列化和反序列化方法,也就是两个仿函数)。
  • YAML 语法快速简单入门:https://www.runoob.com/w3cnote/yaml-intro.html

ConfigVarBase

  • 配置变量的基类。
  • 虚基类。包含配置名称和配置描述两个属性。提供 toString() 和 fromString() 两个纯虚函数将参数值转换成 YAML String 和从 YAML String 转成参数的值。

LexicalCast

  • template<class F, class T>。
  • 类型转换模板类(F 源类型, T 目标类型)。
  • 有如下类型转换模块类(用于基本内置类型):
    template<class F, class T>
    class LexicalCast {
    public:
    	T operator()(const F& v) {
        	return boost::lexical_cast<T>(v);
    	}
    };
    
  • 有如下的偏特化:
    • template class LexicalCast<std::string, std::vector >
    • template class LexicalCast<std::vector , std::string>
    • template class LexicalCast<std::string, std::list >
    • template class LexicalCast<std::list , std::string>
    • template class LexicalCast<std::string, std::set >
    • template class LexicalCast<std::set , std::string>
    • template class LexicalCast<std::string, std::unordered_set >
    • template class LexicalCast<std::unordered_set , std::string>
    • template class LexicalCast<std::string, std::map <std::string, T> >
    • template class LexicalCast<std::map <std::string, T>, std::string>
    • template class LexicalCast<std::string, std::unordered_map <std::string, T> >
    • template class LexicalCast<std::unordered_map <std::string, T>, std::string>
  • 该模块最烦琐的部分,以上偏特化的实现均为仿函数。
  • 该实现的精巧之处(模板编程的精巧之处):比如 LexicalCast<std::string, std::vector >,则先调用 LexicalCast<std::string, std::vector > 版本,在处理每个元素 T 时(这里假设 T 是内置基本数据类型),则调用 boost::lexical_cast(v) 那个最基础的版本。就算 T 是 std::vector 类型或 std::list 等已经实现偏特化的类型,也可以做到层层嵌套去解析。

ConfigVar

  • template<class T, class FromStr = LexicalCast<std::string, T>, class ToStr = LexicalCast<T, std::string> >。其中,T 是配置项参数,FromStr 是将 YAML String 转成参数的值的仿函数,ToStr 是将参数值转换成 YAML String 的仿函数。
  • 对于每种类型的配置,在对应的 ConfigVar 模板类实例化时都要提供其 FromStr 和 ToStr 两个仿函数,用于实现该类型和 YAML 字符串的相互转换。
  • 配置参数模板子类。继承自 ConfigVarBase。
  • 保存对应类型 T 的参数值。
  • 根据不同的非内置基本类型 T,FromStr 和 ToStr 具有不同的模板偏特化实现。
  • 内含一个 std::map,存放配置变更的回调函数。
  • 提供 setValue() 和 getValue() 函数,其中在 setValue() 函数中,会调用 std::map 中存放的所有回调函数进行通知配置变更。
  • 关于回调函数,提供 addListener()、delListener()、getListener()、clearListener() 四个函数给外界使用。

Config

  • ConfigVar 的管理类。
  • 管理所有的配置项,通过静态函数 GetDatas() 获取缓存的 std::unordered_map<std::string, ConfigVarBase::ptr>。
  • 提供 Lookup(const std::string& name, const T& default_value, const std::string& description = “”) 函数给外界获取/创建对应参数名的配置参数。
  • 提供 Lookup(const std::string& name) 函数给外界查找配置参数。
  • 提供 LoadFromYaml(const YAML::Node& root) 函数给外界使用YAML::Node初始化配置模块。
  • 提供 LoadFromConfDir(const std::string& path, bool force = false) 给外界加载path文件夹里面的配置文件。
  • 值得注意的是 Config 所有函数和变量均为静态的。

大概用法

sylar::ConfigVar<int>::ptr g_int_value_config = sylar::Config::Lookup("system.port", (int)8080, "system port");
  				|
				|
			   \|/
SYLAR_LOG_INFO(SYLAR_LOG_ROOT()) << "before: " << g_int_value_config->getValue();
  				|
				|
			   \|/
YAML::Node root = YAML::LoadFile("/home/sylar/workspace/sylar/bin/conf/test.yml");
sylar::Config::LoadFromYaml(root);
				|
				|
			   \|/
SYLA
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值