IceBox实践

 

IceBox实践总结(理论部分参考自 Ice-man)

 

一. IceBox简介

IceBox是对Ice服务(Server)进行配置管理的超级Ice服务组件(super Server),Ice服务可以在

IceBox中被当做组件动态的加载.

                   IceBox组件通过配置properties对它加载和管理的Ice服务进行本地或远程管理。使用IceBox

的好处:

a.        被同一IceBox加载的Ice服务可以通过配置来获得Ice性能优化的好处,举个例子:如果一个应用是另外一个Ice服务的Client,同时clientserver都在同一个IceBox里被配置,那么客户端和服务端之间的执行可以获得优化;

b.  应用的不同服务(Service)之间的组合是通过IceBox的配置来完成,而不是通过硬编码(Compiling and Linking,这样可以对Server中的服务(Service)进行解耦,允许服务(Service)本身根据需要组合或者分离;

c.  应用组件可以用动态链接库的形式发布而不是一个进程的形式发布。这就减轻了系统的负载。例如,你可以在一个JVM中运行若干个应用组件而不是有多个进程,每一个进程都有自己的JVM;

d.  IceBox中配置的Ice服务需要继承实现IceBox.Service接口,这样就为开发者提供了一种通用的开发框架和一个集中的管理设备;

                   e.  IceBox支持在IceGridthe server activation and deployment service)中的集成;

 

IceBox Service的开发(In Java)

 

                   写一个IceBox的服务需要继承实现IceBox::Service的接口,IceBox::Service接口的Slice组件形式如下:

                   module IceBox {

local interface Service {

void start(string name, Ice::Communicator communicator, Ice::StringSeq args);     void stop();

};

};

                   很容易看出来, start方法是在服务(Service)被加载的时候执行,提供了一个初始化对象的机会;stop方法是在IceBox被关闭的时候执行,提供了一个销毁对象,回收资源的机会。

                   start方法执行时,行为一般包括创建一个object adapter对象,和向adapter中注册一个提供服务的具体实现(servants)对象。参数name(java.lang.String)args(java.lang.String[])提供了一些Service的配置信息(后面配置部分详细介绍),communicator参数的类型是Ice::Communicator(java中对应Ice.Communicator,个人理解,可以认为是该Service的上下文环境,其中包括Service的配置参数等,配置参数信息可以通过ic.getProperties得到), communicator对象是IceBox ServerService创建的对象。

需要注意的是,通过serviceIceBox中的配置,communicator对象可能会和其他IceBox Server中的Service共享,所以,请确保object adapters被赋予唯一的name名称(参数name提供,name一般由IceBox的配置来赋值);

stop方法中回收所有被service使用的资源;一般在stopservice会使一个object adapter对象无效(deactivates),不同也有可能对object adapter执行一个wartForDeactivate的方法来确保所有悬挂中的请求在资源清理前得到处理。而IceBox Server会负责将start中传递的communicator对象销毁;

是否在stop方法中明确的指明销毁(destory object adapter还取决于一些其他的因素,举个例子:当IceBoxservices使用共享的communicator对象时,尤其是当这个stopservice对象可能最终被重新启动时,你应该明确地指出销毁object adapter(否则将不会自动销毁这个object adapter)。在其它的情况下,service会默认地将它的object adapter作为communicator销毁的一部分destory掉。

关于为什么Service的接口为什么要定义为local,这里直接引用原文:

These interfaces are declared as local for a reason: they represent a contract between the server and the service, and are not intended to be used by remote clients. Any interaction the service has with remote clients is done via servants created by the service.

其中,servantsIce服务中实际提供服务的服务实例(new UserImpl.do

 

 

. IceBoxjava实践:

            21    

创建Ice服务(Service):

服务需要继承实现IceBox.Service接口,如上所述, 需要实现两个方法 start() stop()。参考一中内容

 

demo如下:

public class RecordService implements IceBox.Service {

          /**

              * @param name 配置文件中的service名称,参考2.2

              *@param communicator  communicator对象,参考1)中对communicator的说明,由IceBox.ServiceManager负责创建和销毁。

              * 可能同时被其他服务使用(由配置文件决定),object Adapter的名称必须是唯一的;

              *  @param args  配置文件中的参数列表,参考2.2

              *

         **/

         public void start(String name, Ice.Communicator communicator, String[] args) {

                   //创建objectAdapter,这里和service同名

                   _adapter = communicator.createObjectAdapter(name);

                   // 创建servant

                   Ice.Object object = new RecordImp();

                   _adapter.add(object, communicator.stringToIdentity(args[0]));

 

                   _adapter.activate();

         }

         public void stop() {

                   _adapter.destroy();

         }

         private Ice.ObjectAdapter _adapter;

         /**

          * @param args

          */

         public static void main(String[] args) {

         }

}

         其中对应的配置文件中的信息:

                   config.icebox    IceBox.Service.Record=RecordServer --Ice.Config=config.service  record  h

                   上面startname值即为 Record;  args[] = {“record”,”h”}; communicator是以Ice.Config对应valueconfig.service指定的配置文件中的内容作为配置信息被Ice.ServiceManager生成communicator对象;

 

                  

         下面对上述内容进行进一步说明:

2.2  IceBox中配置Services

定义属性:IceBox.Service.name=entry_point  [--key=value] [args]

name这个属性定义了service的名字,作为start方法的name参数;

entry_point是上面的service的类名(必须可以在classpath中可以找到);

--key=value这种形式将会被作为property属性用于构造communicator;如果Ice. Config=${1},则是以Ice.Config对应value值指定的配置文件中的properties作为配置信息生成communicator传入start方法;

argsstart方法的args参数;

 

 

三.运行ICEBOX(java环境)

 

java  IceBox.Server  --Ice.Config=config

config是上面的config文件名,Ice.Config对应的文件是IceBox的配置文件,其中包括Ice Services的配置信息和ServeManager的配置信息;

其中可以指定classpath路径和jvm环境参数;

 

四.IceBox  Administration

          一个IceBox 服务(server)内部创建一个service manager的对象,这个对象专门负责loadinginitializing那些配置好了的服务(services)。你可以选择性地将这个对象暴露给远程的客户端,譬如IceBox或者IceGridadministrative utilities,这样IceBoxIceGrid就可以执行某些administrative tasks.

        

         Slice Interfaces

                   如下是IceBoxSlice定义:

                   module IceBox {

exception AlreadyStartedException {};

exception AlreadyStoppedException {};

exception NoSuchServiceException {}; 

interface ServiceObserver {

["ami"] void servicesStarted(Ice::StringSeq services);

["ami"] void servicesStopped(Ice::StringSeq services);

}; 

interface ServiceManager {

idempotent Ice::SliceChecksumDict getSliceChecksums();

["ami"] void startService(string service) throws AlreadyStartedException, NoSuchServiceException;

["ami"] void stopService(string service) throws AlreadyStoppedException, NoSuchServiceException;

 ["ami"] void addObserver(ServiceObserver* observer);

void shutdown();

};};

关于每一个接口的详细信息,请查看IceBox的接口api文档或者ice-man43.5.1中的说明;

 

                            4.2)激活Service Manager的服务

                                     IceBox管理的服务默认是被关闭的,你可以通过两种途径激活:

a.   IceBox.ServiceMangerobject adapter定义endpoints;

b.   Satisfy the prerequisites for enabling the Ice administrative facility(man  32.18.)

 

举例,如下property激活IceBox.ServiceManager的对象适配器(object adapter):

         IceBox.ServiceManager.Endpoints = tcp –h 127.0.0.1 –p 10000

 

     同样,Ice administrative facility 需要用 Ice.Admin.EndpointsIce.admin object adapter定义endpoints; 需要注意的是对于IceGrid中部署的IceBox, Ice.Adminobject adapter会被自动地激活;

     

          不管你选择激活哪种object adapter,激活暴露service manager将会存在恶意客户端致使服务端比较脆弱(makes an IceBox server vulnerable to denial-of-service attacks from malicious clients.) , 所以,客户端在选择和操作endpoints时应该小心谨慎。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值