FS主要用于管理功能插件,并且将所有的功能性请求都转发给对应的功能插件处理,不需要关心用户(由AS管理)、不用关心具体的业务需求(由对应的功能插件处理),所以FS非常的单纯,基本上是与应用无关的,可以在任何应用中复用同一个FS。(但是AS和IRAS就很难复用了,因为不同的应用对AS、IRAS的需求的差别可能非常大)。
实际上,FS的实现自己只包含4个cs文件,绝大多数组件都由ESFramework提供了。下面是FS项目中文件的结构图:
其中,MainForm是主界面,FsConfiguration是FS的配置,下面马上会讲到的是FunctionServer这个FS中的核心类。
我们已经知道,FS通过Tcp发布服务给AS使用,即AS通过与FS之间的Tcp连接池来使用FS提供的服务,所以会使用到ESFramework提供的ITcp组件。
其次,FS需要管理所有的功能插件,所以它需要使用ESFramework提供的IAddinManagement组件。
在理解了这两点的基础上,我们可以定义FunctionServer这个类了,它很简单,所有方法及实现如下所示:
publicvoidLoadAddins()
{
stringdirectory=System.IO.Directory.GetParent(System.Windows.Forms.Application.ExecutablePath).FullName;
this.addinManagement.LoadAllAddins(directory,true);
}
publicvoidStart()
{
this.agileTcp.Start();
this.serverPerformanceMonitor.Start();
this.asRemotingServiceAccesser.FsStarted();
}
publicvoidStop()
{
this.agileTcp.Stop();
this.serverPerformanceMonitor.Stop();
this.asRemotingServiceAccesser.FsToExit();
}
publicvoidReadyToExit()
{
this.Stop();
this.agileTcp.Dispose();
}
{
stringdirectory=System.IO.Directory.GetParent(System.Windows.Forms.Application.ExecutablePath).FullName;
this.addinManagement.LoadAllAddins(directory,true);
}
publicvoidStart()
{
this.agileTcp.Start();
this.serverPerformanceMonitor.Start();
this.asRemotingServiceAccesser.FsStarted();
}
publicvoidStop()
{
this.agileTcp.Stop();
this.serverPerformanceMonitor.Stop();
this.asRemotingServiceAccesser.FsToExit();
}
publicvoidReadyToExit()
{
this.Stop();
this.agileTcp.Dispose();
}
各个方法的含义和成员变量的含义相信大家已经非常的明白了,由必要解释一下serverPerformanceMonitor,它用于监控本服务器的性能状态,并将性能数据通过事件发布。如果你读过前面的文章,你会知道,这些性能数据将被发送给AS,然后AS根据这些性能数据在多个FS上进行负载均衡调度。asRemotingServiceAccesser用于访问AS发布的远程服务,比如FS启动或退出时通知AS。
FS核心的类FunctionServer就这些功能。你一定非常想知道,ITcp、ITcpStreamDispatcher等是如何与FunctionServer装配起来的,谜底马上揭晓。相对于使用4个cs文件就实现了FS来说,FS的组件装配就稍微复杂些(AS则更复杂)。我采用SPring.NET来完成组件装配。
我们先看ITcp组件的装配:
<objectname="agileTcp"type="ESFramework.Network.Tcp.AgileTcp,ESFramework"init-method="Initialize">
<propertyname="Port">
<objecttype="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject,Spring.Core">
<propertyname="TargetObject"ref="fsConfiguration"/>
<propertyname="TargetProperty"value="TcpPort"/>
</object>
</property>
<propertyname="MaxMessageSize"value="2000000"/>
<propertyname="Dispatcher"ref="tcpStreamDispatcher"/>
<propertyname="ContractHelper"ref="contractHelper"/>
<propertyname="BufferPool">
<objecttype="ESFramework.Network.Tcp.SimpleBufferPool,ESFramework"/>
</property>
</object>
<propertyname="Port">
<objecttype="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject,Spring.Core">
<propertyname="TargetObject"ref="fsConfiguration"/>
<propertyname="TargetProperty"value="TcpPort"/>
</object>
</property>
<propertyname="MaxMessageSize"value="2000000"/>
<propertyname="Dispatcher"ref="tcpStreamDispatcher"/>
<propertyname="ContractHelper"ref="contractHelper"/>
<propertyname="BufferPool">
<objecttype="ESFramework.Network.Tcp.SimpleBufferPool,ESFramework"/>
</property>
</object>
如果你研究过AgileTcp组件,上面的配置非常容易理解,最主要的,AgileTcp使用的分派器组件,分配器组件的装配如下:
<objectname="tcpStreamDispatcher"type="ESFramework.Network.Tcp.TcpStreamDispatcher,ESFramework">
<propertyname="ContractHelper"ref="contractHelper"/>
<propertyname="TcpClientsController"ref="agileTcp"/>
<propertyname="RequestDealerFactory">
<objecttype="ESFramework.Network.FunAddinDealerFactory,ESFramework">
<propertyname="AddinManagement"ref="addinManagement"/>
</object>
</property>
</object>
<propertyname="ContractHelper"ref="contractHelper"/>
<propertyname="TcpClientsController"ref="agileTcp"/>
<propertyname="RequestDealerFactory">
<objecttype="ESFramework.Network.FunAddinDealerFactory,ESFramework">
<propertyname="AddinManagement"ref="addinManagement"/>
</object>
</property>
</object>
非常需要说明一点的是,消息分派器使用的处理器工厂是ESFramework.Network.FunAddinDealerFactory,因为它只需要处理功能请求,并且这些功能请求是由功能插件处理的,所以使用ESFramework.Network.FunAddinDealerFactory就可以了,而不需要使用功能全面的EsbRequestDealerFactory,EsbRequestDealerFactory通常由AS使用。
最后一个重要组件的装配--FunctionServer:
<objectname="functionServer"type="FunctionServerSystem.Server.FunctionServer,FunctionServerSystem">
<propertyname="AgileTcp"ref="agileTcp"/>
<propertyname="AddinManagement"ref="addinManagement"/>
<propertyname="ServerPerformanceMonitor"ref="serverPerformanceMonitor"/>
<propertyname="AsRemotingServiceAccesser"ref="asRemotingServiceAccesser"/>
</object>
<propertyname="AgileTcp"ref="agileTcp"/>
<propertyname="AddinManagement"ref="addinManagement"/>
<propertyname="ServerPerformanceMonitor"ref="serverPerformanceMonitor"/>
<propertyname="AsRemotingServiceAccesser"ref="asRemotingServiceAccesser"/>
</object>
毫无秘密可言!
还有就是主界面MainForm的装配,就不罗列了,大家看源码就知道了。这是FS的一个例子,如果你需要看到客户端的例子,AgileIM的源码是一个选择,不过AgileIM稍微复杂了些,过段时间我会给出一个简单的建立在ESFramework上的客户端实现。
下面是FS运行时的截图:
下载FS源码(VS2003)。
转到:ESFramework 可复用的通信框架(序)