Spring HTTP Invoker

http://zhenggm.javaeye.com/blog/562216

 

概念 
Spring HTTP invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用(意味着可以通过防火墙),并使用java的序列化机制在网络间传递对象。 
效率 
远程调用效率: rmi>spring http invoker>webservice 
优点 
1.spring http invoker 采用java的序列化机制,并采用类似rmi的方式调用,既能够使用http协议轻松穿过防火墙,又能够保证高效的远程调用。 
2.轻量级的远程调用模型,对于采用spring的项目可以无缝嵌入,侵入性小。 
3.非常简单,容易上手。 
缺点 
只支持spring. 
例子 
服务端 
1.web.xml 

  1. <servlet>    
  2.         <servlet-name>service</servlet-name>    
  3.         <servlet-class>    
  4.             org.springframework.web.servlet.DispatcherServlet     
  5.         </servlet-class>    
  6.         <load-on-startup>1</load-on-startup>    
  7. </servlet>  
  8. <servlet-mapping>    
  9.         <servlet-name>service</servlet-name>    
  10.         <url-pattern>/httpservice/*</url-pattern>    
  11. </servlet-mapping>  

2. spring配置文件 

Xml代码  

  1. <beans>  
  2.     <bean id="userInfoServiceProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">  
  3.         <property name="service" ref="userInfoService"/>  
  4.         <property name="serviceInterface" value="cn.gov.zjport.skeleton.sso.UserInfoService"/>  
  5.     </bean>  
  6. </beans>  

其中userInfoService为项目原有的service 
3. 增加一个service-servlet.xml,放在web-inf目录下 

Xml代码  

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd&quot;>  
  3.   
  4. <beans>  
  5.     <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">    
  6.         <property name="mappings">    
  7.             <props>    
  8.                   <prop key="/userInfoService">userInfoServiceProxy</prop>  
  9.             </props>    
  10.         </property>    
  11.     </bean>   
  12. </beans>  

至此服务端发布完毕。 
客户端 
1. spring配置文件 

Xml代码  

  1. <bean id="userInfoService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">    
  2.         <property name="serviceUrl" value="http://<server&gt;:<port>/<domain>/httpservice/userInfoService"/>    
  3.         <property name="serviceInterface" value="cn.gov.zjport.skeleton.sso.UserInfoService"/>    
  4.     </bean>  

2. 调用代码 

Java代码  

  1. String xml=userInfoService.getUserInfo("simpleadmin"true);  

与非远程调用代码一摸一样。 
所以说对于采用spring的项目,只需要简单配置,即可实现分布式部署与远程调用。

This entry was posted in Spring 编程. Bookmark the permalink.

Leave a comment