首页 > 代码库 > spring实现远程方法调用

spring实现远程方法调用

spring in action一书的读书笔记

使用spring实现远程方法调用可以极大简化开发

将一个spring bean包装成为RMI service

[java]  view plain copy
  1. <bean class="org.springframework.remoting.rmi.RmiServiceExporter"  
  2.     p:service-ref="spitterService"  
  3.     p:serviceName="SpitterService"  
  4.     p:serviceInterface="com.habuma.spitter.service.SpitterService"  
  5.     p:registryHost="rmi.spitter.com"  
  6.     p:registryPort="1199"/>  

调用发布的RMI service
[java]  view plain copy
  1. String serviceUrl = "rmi:/spitter/SpitterService";  
  2. SpitterService spitterService = (SpitterService)Naming.lookup(serviceUrl);  

我们也可以使用spring进行配置
[html]  view plain copy
  1. <bean id="spitterService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" p:serviceUrl="rmi://localhost/SpitterService"  
  2. p:serviceInterface="com.habuma.spitter.service.SpitterService"/>  


RMI有一个限制就是对防火墙不太友好,不容易穿过防火墙,这在局域网可以不用考虑,但是广域网就需要考虑这一点

Hessian,Burlap是Caucho科技提供的两种方法

Hessian基于二进制消息进行通信

Burlap基于xml格式消息进行通信


发布Hessian service

[html]  view plain copy
  1. <bean id="hessianSpitterService" class="org.springframework.remoting.caucho.HessianServiceExporter"  
  2.     p:service-ref="spitterService"  
  3.     p:serviceInterface="com.habuma.spitter.service.SpitterService"/>  
[html]  view plain copy
  1. HessianServiceExporter在spring中是作为一个controller存在的,所以需要在web.xml(意味着必须是web项目)配置DispatcherServlet,将对Hessian service bean的请求映射到hessianSpitterService  
  2. <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  3.     <property name="mappings">  
  4.         <value>  
  5.             /spitter.service=hessianSpitterService  
  6.         </value>  
  7.     </property>  
  8. </bean>  
调用发布的Hessian service
[html]  view plain copy
  1. <bean id="spitterService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"  
  2.     p:serviceUrl="http://localhost:8080/Spitter/spitter.service"  
  3.     p:serviceInterface="com.habuma.spitter.service.SpitterService"/>  

发布Burlap service
[html]  view plain copy
  1. <bean id="burlapSpitterService" class="org.springframework.remoting.caucho.BurlapServiceExporter"  
  2.     p:service-ref="spitterService"  
  3.     p:serviceInterface="com.habuma.spitter.service.SpitterService"/>  

调用 Burlap service
[html]  view plain copy
  1. <bean id="spitterService" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean"  
  2.     p:serviceUrl="http://localhost:8080/Spitter/spitter.service"  
  3.     p:serviceInterface="com.habuma.spitter.service.SpitterService"/>  


RMI使用jdk中的类库,但是对防火墙不太友好,burlap和hessian对防火墙友好但是使用了第三方的类库,Spring‘s HttpInvoker解决了这两个矛盾。使用spring‘s httpInvoker双发必须都是用spring

[html]  view plain copy
  1. <bean class="org.spring.framework.remoting.httpinvoker.HttpInvokerServiceExporter"  
  2.     p:service-ref="spitterService"  
  3.     p:serviceInterface="com.habuma.spitter.service.SpitterService"/>  
  4. HttpInvokerServiceExporter在spring中也是作为一个controller  
  5.   
  6. 调用HttpInvoker service  
  7. <bean id="spitterService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"  
  8.     p:serviceUrl="http://localhost:8080/Spitter/spitter.service"  
  9.     p:serviceInterface="com.habuma.spitter.service.SpitterService"/>  

技术分享

技术分享


几种方式使用方法大体相同,只是使用的spring中的类不同

spring实现远程方法调用