使用kbmMWConfiguration 让 kbmmw smartservice 更聪明

以前的文章介绍了 很多 kbmmw smartservice 的使用,尤其是 rest 服务,所有的的配置都是通过

开发时写编码实现的,这样就可能导致,针对不同的应用环境,我们要重新编译代码,当然也可以

自己通过配置文件来实现一些配置,那么我们就看看如果通过kbmmw 只带的配置功能让smartservice

更聪明,更灵活。

  首先简要说一下 kbmMWConfiguration,这个是kbmmw 官方提供的简单方便的保存,访问。

配置信息可以保存到注册表,也可以保存到ini,xml,json等文件中,并可以通过类似 a.b.c.d 路径方式

访问节点,非常方便。具体用法可以看官方自带的demo。其默认保存方式为XML,这个方式比较方便的

查看路径。

首先我们先看一下 xml 的存储格式

<?xml version="1.0" encoding="utf-8" ?>
<config>
  <httptransport>
    <url>http://+:80/</url>
    <maxthreads>16</maxthreads>
    <queuelength>2000</queuelength>
  </httptransport>

 <database>
   <server>127.0.0.1</server>
   <database>dbdemos.db3</database>
   <username>xalion</username>
   <password>123456</password>
  </database>
   
 <service>
   <xalionrest>
      <minInstances>20</minInstances> 
      <maxInstances>100</maxInstances>    
     <helloworld>  
        <method >get</method>
        <path>hello</path>
     </helloworld>    
   </xalionrest>
  </service>
</config>

内容很明确,就不做解释了。

kbmmw 内置一个kbmMWConfiguration 对象,我们不需要单独建立,当然你非要自己建立一个单独的配置文件,也是可以的。

我们只需添加kbmMWConfiguration 单元,就可以使用内置的 config 对象。

首先我们在程序开始时 ,初始化一下存储格式及文件名。

 reportmemoryleaksonshutdown:=True;
  Application.Initialize;

  config.Storage:=TkbmMWxmlConfigurationStorage.Create('httpconfig.xml');

就是指定我们的配置文件名,文件默认与执行程序在同一目录。

后面我们就可以使用了

  url_s.Text:=config.AsDefString('httptransport.url', 'http://+:80/');
     maxt_s.Text:=config.AsDefString('httptransport.maxthreads', '20');
     ql_s.text:=config.AsDefString('httptransport.queuelength', '500');


     servername_s.text:=config.AsDefString('database.server', '');
     database_s.Text:=config.AsDefString('database.database', '');
     username_s.Text:=config.AsDefString('database.username', '');
     password_s.Text:=config.AsDefString('database.password', '');

经过这些操作,我们就可以通过配置文件定制我们的服务配置了。

非常方便,当然也可以通过cnfig 来保存我们手工的设置到配置文件。

接下来我们就看看如果通过配置文件来控制我们的 smartservice 了

首先我们来看一下我们一下写的smartservice 的控制

[kbmMW_Service('name:xalionrest,flags:[listed],minInstances:32,maxInstances:128')]

  [kbmMW_Rest('path:/xalionrest')]

这里面我们设置了服务 实例的 最大值和最小值。

我们可以通过运行,看看这个是否有效

但是在实际生产环境中,这个肯定是不一样的。

那么我们如何通过配置文件来控制这两个参数呢?

在kbmmw 中这个非常简单。

 [kbmMW_Service('name:xalionrest,flags:[listed],minInstances:$(service.xalionrest.minInstances=32),maxInstances:$(service.xalionrest.maxInstances=128)')]

通过$ 来引用配置文件里面的数值。

试一下运行效果。

和我们配置文件中的数值完全一样,这样我们就可以通过配置来修改相关参数了。

当然我们也可以通过配置文件来控制rest 访问的。

[kbmMW_Rest('method:$(service.xalionrest.helloworld.method=post), path:$(service.xalionrest.helloworld.path=helloworld)')]
     [kbmMW_Method]
     function HelloWorld:string;

我们可以通过配置文件来重写一下 url

通过配置文件来改写 URL 的路径,方便定制服务器。

好了,开饭了,就写到这里。

猜你喜欢

转载自www.cnblogs.com/xalion/p/10199021.html
今日推荐