tars源码漫谈第7篇------tc_config.h/tc_config.cpp(易用的配置类)

      配置文件能实现配置参数与代码逻辑的分离, 如果需要修改配置参数, 仅仅改配置文件即可。 发布的时候, 风险更小, 也更灵活。

 

      在tc_config中, 实现了配置类。在看代码之前, 可想一想, 如果我们自己写这个配置类, 该怎么写?

      最简单的办法是: 让配置文件长成这样:

key1=value1
key2=value2
key3=value3
...

       然后在代码中定义:

map<string, string> g_map_config;

       这样就实现了配置文件与内存中变量g_map_config的关联。 当然, 如果装逼一点, 可以这么写:

struct MY_CONFIG
{
	map<string, string> m_map_config;
	...
};

       说白了, 配置类的作用就是实现:  外存--->内存。

 

       上面的配置文件足够简单清晰, 但是,每行一个key/value,  显得不够有层次感和逻辑感。 当配置文件复杂后, 可能需要解析xml类似的格式, 此时就需要一个相对复杂的Config类。

       看一下tars中常用的配置文件格式:

<tars>
    <application>
        enableset=n
        setdivision=NULL
        <server>
            node=tars.tarsnode.ServerObj@tcp -h 192.168.2.131 -p 19386 -t 60000
            app=tars
            server=tarsconfig
            localip=192.168.2.131
            local=tcp -h 127.0.0.1 -p 10001 -t 3000
            basepath=/usr/local/app/tars/bin
            datapath=/usr/local/app/tars/tarsconfig/data
            logpath=/usr/local/app/tars/app_log
            logsize=100000000
            config=tars.tarsconfig.ConfigObj
            notify=tars.tarsnotify.NotifyObj
            deactivating-timeout=3000
            logLevel=DEBUG
            <tars.tarsconfig.ConfigObjAdapter>
                allow
                endpoint=tcp -h 192.168.2.131 -p 10001 -t 60000
                handlegroup=tars.tarsconfig.ConfigObjAdapter
                maxconns=10240
                protocol=tars
                queuecap=10000
                queuetimeout=60000
                servant=tars.tarsconfig.ConfigObj
                shmcap=0
                shmkey=0
                threads=10
            </tars.tarsconfig.ConfigObjAdapter>
        </server>
        <client>
            locator=tars.tarsregistry.QueryObj@tcp -h registry.tars.com -p 17890
            sync-invoke-timeout=3000
            async-invoke-timeout=5000
            refresh-endpoint-interval=60000
            report-interval=60000
            sample-rate=100000
            max-sample-count=50
            asyncthread=3
            stat=tars.tarsstat.StatObj
            property=tars.tarsproperty.PropertyObj
            modulename=tars.tarsconfig
        </client>
    </application>
    <db>
        charset=utf8
        dbhost=db.tars.com
        dbname=db_tars
        dbpass=tars2015
        dbport=3306
        dbuser=tars
    </db>
</tars>

      很有层次感, 如果让我们自己去解析, 会比较麻烦, 而且容易出错, tc_config帮我们做了一切。

      

       可以看到_conf就是一个配置类的对象:

Application.h:425:    static TC_Config           _conf;

       现在把配置文件和_conf关联起来:

Application.cpp:410:        _conf.parseFile(ServerConfig::ConfigFile);

       至此, _conf中已经有了配置文件的一切信息, 数据从外存转到了内存。

 

       很显然: _conf.get("/tars/application/server<config>")  的值就是"tars.tarsconfig.ConfigObj",   更多使用示例如:

_conf.get("/tars/application/server<log>");
_conf.get("/tars/application/server<config>");
_conf.get("/tars/application/server<notify>");
toDefault(_conf.get("/tars/application/server<app>"),
_conf.get("/tars/application/server<localip>");
toDefault(_conf.get("/tars/application/server<server>"),
toDefault(_conf.get("/tars/application/server<basepath>"),
toDefault(_conf.get("/tars/application/server<datapath>"),
toDefault(_conf.get("/tars/application/server<logpath>"),
TC_Common::toSize(toDefault(_conf.get("/tars/application/server<logsize>"),
TC_Common::strto<int>(toDefault(_conf.get("/tars/application/server<lognum>"),
_conf.get("/tars/application/server<localip>");
_conf.get("/tars/application/server<local>");
_conf.get("/tars/application/server<node>");
_conf.get("/tars/application/server<log>");
_conf.get("/tars/application/server<config>");
_conf.get("/tars/application/server<notify>");
_conf.get("/tars/application/server<reportflow>")=="0"?0:1;
_conf.get("/tars/application/server<checkset>","1")=="0"?0:1;
TC_Common::strto<bool>(toDefault(_conf.get("/tars/application/server<opencoroutine>"),
TC_Common::toSize(toDefault(_conf.get("/tars/application/server<coroutinememsize>"),
TC_Common::toSize(toDefault(_conf.get("/tars/application/server<coroutinestack>"),
_conf.get("/tars/application/server<logLevel>","DEBUG");
_conf.get("/tars/application/server<logLevel>","DEBUG");

       取数据多么简单, 岁月静好, 是因为tc_config中的类在为你负重前行。

 

 

       再举个例子, 有个配置文件是这样的(设路径为/usr/local/service/a.conf):

<main>

<db>
dbhost=xxx.mdb.mig
dbname=yyyy
dbuser=user
dbpass=passwd
charset=utf8
dbport=123456
</db>

</main>

       可以通过objTcConfi.getDomainMap("/main/db/")来获取到db的参数, 并存于map中.

 

       至于tc_config的代码, 其实可以不用太在意, 看了一下, 无非就是各种字符串的复杂解析、遍历和处理。 突然想起来, 那一年, 面试的时候, 面试官直接让我写html parse,  我还是硬着头皮在那里写, 写, 写。

       业界的html parse, xml parse已经很成熟了, tc_config中的TC_ConfigDomain和TC_Config也是一路货色。

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值