spring實現遠程方法調用

spring in action一書的讀書筆記 html

使用spring實現遠程方法調用能夠極大簡化開發 java

將一個spring bean包裝成爲RMI service web

[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有一個限制就是對防火牆不太友好,不容易穿過防火牆,這在局域網能夠不用考慮,可是廣域網就須要考慮這一點 spring

Hessian,Burlap是Caucho科技提供的兩種方法 app

Hessian基於二進制消息進行通訊 url

Burlap基於xml格式消息進行通訊 spa


發佈Hessian service .net

[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 xml

[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中的類不一樣
相關文章
相關標籤/搜索