Spring为各种远程访问技术的集成提供了工具类。Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程访问功能的服务变得相当容易。目前,Spring支持四种远程技术:
1.远程方法调用(RMI)。通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,Spring同时支持传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的透明远程调用(支持任何Java接口)。
2.Spring的HTTP调用器。Spring提供了一种特殊的允许通过HTTP进行Java串行化的远程调用策略,支持任意Java接口(就像RMI调用器)。相对应的支持类是 HttpInvokerProxyFactoryBean和 HttpInvokerServiceExporter。
3.Hessian。通过 HessianProxyFactoryBean 和 HessianServiceExporter,可以使用Caucho提供的基于HTTP的轻量级二进制协议来透明地暴露服务。
4.Burlap。 Burlap是Caucho的另外一个子项目,可以作为Hessian基于XML的替代方案。Spring提供了诸如 BurlapProxyFactoryBean 和 BurlapServiceExporter 的支持类。
5.JAX RPC。Spring通过JAX-RPC为远程Web服务提供支持,以Web Services的形式发布。常见开源的项目有Axis,CXF,Xfire等
1.远程方法调用(RMI)。通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,Spring同时支持传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的透明远程调用(支持任何Java接口)。
2.Spring的HTTP调用器。Spring提供了一种特殊的允许通过HTTP进行Java串行化的远程调用策略,支持任意Java接口(就像RMI调用器)。相对应的支持类是 HttpInvokerProxyFactoryBean和 HttpInvokerServiceExporter。
3.Hessian。通过 HessianProxyFactoryBean 和 HessianServiceExporter,可以使用Caucho提供的基于HTTP的轻量级二进制协议来透明地暴露服务。
4.Burlap。 Burlap是Caucho的另外一个子项目,可以作为Hessian基于XML的替代方案。Spring提供了诸如 BurlapProxyFactoryBean 和 BurlapServiceExporter 的支持类。
5.JAX RPC。Spring通过JAX-RPC为远程Web服务提供支持,以Web Services的形式发布。常见开源的项目有Axis,CXF,Xfire等
6.JMS(待实现)。
上一节介绍了HttpInvoker的使用,Burlap、RIM和Hessian的使用与其类似。下面以Burlap为例测试。
Burlap是一个轻量级的XML RPC协议,这使得他可以移植到任何可以解析XML的语言中。比起Hessian,它的可读性更强。和其他基于Xml的远程技术(如SOAP和XML-RPC)不同,它的消息结构尽可能简单,不需要额外的外部定义语言(如WSDL或IDL)。Hessian,Burlap是Caucho提供的两种基于Http的轻量级远程服务, Burlap现在已经集成到hessian.jar中,不作为一个单独的项目了。
代码示例如下:
1.添加依赖jar包
2.接口、实现类
对象User.Java
接口:UserService.java
实现类:UserServiceImpl.java
3.服务端配置
applicationContext.xml
applicationContext-burlap-exporter.xml
web.xml
4.客户端配置
applicationContext-burlap.xml
5.测试运行类
UserServiceClientTest.java
通过http请求抓包可以看见协议定义
请求:
相应: