java

位置:IT落伍者 >> java >> 浏览文章

Spring2 提供的remote包学习笔记


发布日期:2020年10月29日
 
Spring2 提供的remote包学习笔记

Spring 针对远程访问服务提供的一个remote包其的的是提供一套统一的远程服务发布功能

先来看一下Spring支持那些远程服务功能 RMI服务 Hessian或者Burlap通过HTTP远程调用服务 HTTP调用器暴露服务

下面用一个例子来看一下Spring 是怎样对这些服务进行统一的封装和管理

先看一下服务器端的源代码

publicinterfaceIBookService{

BookgetById(Stringid);

}

publicclassBook{

publicStringname;

publicStringid;

publicStringauthor;

}

publicclassBookServiceimplementsIBookService{

publicBookgetById(Stringid){

returnBookStoregetById(id);

}

}

客户端源代码

publicclassBookQueryService{

privateIBookServicebookService;

publicvoidsetAccountService(IBookServicebookService){

thisbookService=bookService;

}

publicBookgetBookById(Stringid){

returnbookServicegetById(id);

}

}

//客户端调用示例

publicstaticvoidmain(String[]args){

ClassPathXmlApplicationContextcontext;

context=newClassPathXmlApplicationContext(applicationContextxml);

BookQueryServicebookQueryService=(BookQueryService)contextgetBean(bookQueryService);

Bookbook=bookQueryServicegetBookById();

}

使用Spring 发布 RMI服务示例

服务器端配置:

<beanid=bookServiceclass=comxmatthewspringremoteBookService>

</bean>

<beanclass=orgspringframeworkremotingrmiRmiServiceExporter>

<!doesnotnecessarilyhavetobethesamenameasthebeantobeexported>

<propertyname=serviceNamevalue=bookService/>

<propertyname=serviceref=bookService/>

<propertyname=serviceInterfacevalue=comxmatthewspringremoteIBookService/>

<propertyname=registryPortvalue=/>

</bean>

客户端配置:

<beanclass=comxmatthewspringremoteclientBookQueryService>

<propertyname=bookServiceref=bookService/>

</bean>

<beanid=bookServiceclass=orgspringframeworkremotingrmiRmiProxyFactoryBean>

<propertyname=serviceUrlvalue=rmi://localhost:/bookService/>

<propertyname=serviceInterfacevalue=comxmatthewspringremoteIBookService/>

</bean>

使用Spring 发布 基于Http的Hessian服务示例

Hessian提供一种基于HTTP的二进制远程协议它是由Caucho创建的可以在  找到更多有关Hessian的信息

首为使用Hessian需要为其配置Spring 的 DispatcherServlet把下面的配置加入到webxml中

<servlet>

<servletname>remoting</servletname>

<servletclass>orgspringframeworkwebservletDispatcherServlet</servletclass>

<loadonstartup></loadonstartup>

</servlet>

<servletmapping>

<servletname>remoting</servletname>

<urlpattern>/remoting/*</urlpattern>

</servletmapping>

服务器端配置:

<beanid=bookServiceclass=comxmatthewspringremoteBookService>

</bean>

<beanname=/bookServiceclass=orgspringframeworkremotingcauchoHessianServiceExporter>

<propertyname=serviceref=bookService/>

<propertyname=serviceInterfacevalue=comxmatthewspringremoteIBookService/>

</bean>

客户端配置:

<beanclass=comxmatthewspringremoteclientBookQueryService>

<propertyname=bookServiceref=bookService/>

</bean>

<beanid=bookServiceclass=orgspringframeworkremotingcauchoHessianProxyFactoryBean>

<propertyname=serviceUrlvalue=//localhost:/bookService/>

<propertyname=serviceInterfacevalue=comxmatthewspringremoteIBookService/>

</bean>

使用Spring 发布 基于Http的Burlap服务示例

Burlap它是一个基于XML的Hessian替代方案它的配置方法和上述Hessian的一样只要把 Hessian 换成 Burlap 就行了

服务器端使用orgspringframeworkremotingcauchoBurlapServiceExporter 发布服务客户端使用orgspringframeworkremotingcauchoBurlapProxyFactoryBean

使用Spring 发布 基于HTTP调用器暴露服务

和使用自身序列化机制的轻量级协议Burlap和Hessian相反Spring HTTP调用器使用标准Java序列化机制来通过HTTP暴露业务

但其配置与Burlap和Hessian很相近

服务器端配置:

<beanid=bookServiceclass=comxmatthewspringremoteBookService>

</bean>

<beanname=/bookServiceclass=orgspringframeworkremoting>

<propertyname=serviceref=bookService/>

<propertyname=serviceInterfacevalue=comxmatthewspringremoteIBookService/>

</bean>

客户端配置:

<beanclass=comxmatthewspringremoteclientBookQueryService>

<propertyname=bookServiceref=bookService/>

</bean>

<beanid=bookServiceclass=orgspringframeworkremoting>

<propertyname=serviceUrlvalue=//localhost:/bookService/>

<propertyname=serviceInterfacevalue=comxmatthewspringremoteIBookService/>

</bean>

               

上一篇:hibernate的条件查询

下一篇:实例讲解Struts原理与实践