分享

Spring 远程http调用

 木有银 2011-07-20
核心提示:Spring HTTP invoker简介 Spring HTTP invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用(意味着可以通过防火墙),并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,这有点类似于we

Spring HTTP invoker简介
Spring HTTP invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用(意味着可以通过防火墙),并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,这有点类似于webservice,但又不同于webservice,区别如下:

 

webservice

HTTP invoker

跨平台,跨语言

只支持java语言

支持SOAP,提供wsdl

不支持

结构庞大,依赖特定的webservice实现,如xfire

结构简单,只依赖于spring框架本身

 
项目中使用哪种远程调用机制取决于项目本身的要求。

 

²        HTTP invoker 服务模式

 HTTP invoker 服务模式

 说明:

1.       服务器端:通过HTTP invoker服务将服务接口的某个实现类提供为远程服务

2.       客户端:通过HTTP invoker代理向服务器端发送请求,远程调用服务接口的方法

3.       服务器端与客户端通信的数据需要序列化

 

配置服务器端和客户端的步骤

配置服务器端
1.       添加springJAR文件

建议使用spring2+.jar版本

2.       创建服务接口

3.       创建服务接口的具体实现类

4.       公开服务

 

配置客户端
1.  添加springJAR文件

建议使用spring2+.jar版本

2.       创建服务接口

3.       访问服务

 

实例讲解

服务器端

1.服务接口:UcService.java

它提供两项服务,查询用户信息和记录日志,如下:

  1. public interface UcService {   
  2.   
  3.            public UserInfo getUserInfobyName(String userName);   
  4.   
  5.            public int recordLog(String username, String point, String operate, String desc);   
  6.   
  7. }   

说明:举这个列子是因为其比较有代表性,它将展示普通数据类型(int,long等)和复杂数据类型(DTO等)的远程调用方式。UserInfo是一个普通的DTO,代码如下:

  1. public class UserInfo implements Serializable {   
  2.   
  3.     private static final long serialVersionUID = -6970967506712260305L;   
  4.   
  5.     /**用户名*/  
  6.   
  7.     private String userName;   
  8.   
  9.     /**电子邮箱*/  
  10.   
  11.     private String email;   
  12.   
  13.     /**注册日期*/  
  14.   
  15.     private Date registDate;   
  16.   
  17.     
  18.   
  19.     public String getUserName() {   
  20.   
  21.         return userName;   
  22.   
  23.     }   
  24.   
  25.     public void setUserName(String userName) {   
  26.   
  27.         this.userName = userName;   
  28.   
  29.     }   
  30.   
  31.     public String getEmail() {   
  32.   
  33.         return email;   
  34.   
  35.     }   
  36.   
  37.     public void setEmail(String email) {   
  38.   
  39.         this.email = email;   
  40.   
  41.     }   
  42.   
  43.      public Date getRegistDate() {   
  44.   
  45.         return registDate;   
  46.   
  47.     }   
  48.   
  49.     public void setRegistDate(Date registDate) {   
  50.   
  51.         this.registDate = registDate;   
  52.   
  53.     }   
  54.   
  55. }   
  56.   

注意:因为是在网络间传输对象,所以需要将UserInfo实现Serializable接口,并指定一个serialVersionUID(任意值即可,同时客户端也要有这个类,否则在客户端接收对象时会因为serialVersionUID不匹配而出现异常)

 回到UcService.java,它提供了两个服务(在这里一个方法代表一个服务功能),我们需要具体的实现类来实现真正的服务

 

2.实现类是UCServiceImpl.java

  1. public class UCServiceImpl implements UcService {   
  2.   
  3.     private static Logger pointrecordlog = Logger.getLogger("pointrecordlog");   
  4.   
  5.     private static Logger logger = Logger.getLogger(UCServiceImpl.class);   
  6.   
  7.     private UcFacade ucFacade;   
  8.   
  9.     public void setUcFacade(UcFacade ucFacade) {   
  10.   
  11.         this.ucFacade = ucFacade;   
  12.   
  13.     }   
  14.   
  15.     public UserInfo getUserInfobyName(String userName) {   
  16.   
  17.         UserInfo user = null;   
  18.   
  19.         try {   
  20.   
  21.             user = ucFacade.getUserInfoDetail(userName);   
  22.   
  23.             logger.debug("get userinfo success by  username:" + userName);   
  24.   
  25.         } catch (Throwable t) {   
  26.   
  27.             logger.error("get userinfo fail by  username:" + userName, t);   
  28.   
  29.         }   
  30.   
  31.         return user;   
  32.   
  33.     }   
  34.   
  35.     public int recordLog(String username, String point, String operate, String desc) {   
  36.   
  37.         int result = 0;   
  38.   
  39.         try {   
  40.   
  41.             pointrecordlog.info(username + " - " + point + " - " + operate + " - " + desc);   
  42.   
  43.         } catch (Throwable t) {   
  44.   
  45.             result = -1;   
  46.   
  47.             logger.error(t);   
  48.   
  49.         }   
  50.   
  51.         return result;   
  52.   
  53.     }   
  54.   
  55. }   
  56.   

说明:ucFacade是通过spring注入的一个数据查询类,因为它与http invoker没有直接关系,所以不进行介绍。

3.公开服务UcService.java

²        WEB-INF/application-context.xml:将接口声明为HTTP invoker服务

  1. <bean id="httpService"  
  2.   
  3.         class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">  
  4.   
  5.         <property name="service">  
  6.   
  7.             <ref bean="ucService" />  
  8.   
  9.         </property>  
  10.   
  11.         <property name="serviceInterface"  
  12.   
  13.             value="com.netqin.baike.service.UcService">  
  14.   
  15.         </property>  
  16.   
  17.     </bean>  
  18.   
  19.     <bean id="ucService" class="com.netqin.baike.service.impl.UCServiceImpl"/>  
  20.   

说明:HttpInvokerServiceExporter实际上是一个spring mvc控制器,它处理客户端的请求并调用服务实现。

²         WEB-INF/service-servlet.xml:HttpInvokerServiceExporter实际上是一个spring mvc控制器,所以需要为其提供spring URL 处理器,这里我们使用SimpleUrlHandlerMapping

  1. <bean  
  2.   
  3.         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  4.   
  5.          <property name="mappings">  
  6.   
  7.             <props>  
  8.   
  9.                   <prop key="/httpService">httpService</prop>  
  10.   
  11.             </props>  
  12.   
  13.         </property>  
  14.   
  15.     </bean>  
  16.   

²         WEB-INF/web.xml:配置spring监听及DispatcherServlet

  1. <context-param>  
  2.   
  3.         <param-name>contextConfigLocation</param-name>  
  4.   
  5.         <param-value>  
  6.   
  7.             /WEB-INF/application-context.xml   
  8.   
  9.         </param-value>  
  10.   
  11.     </context-param>  
  12.   
  13.     
  14.   
  15.     <listener>  
  16.   
  17.         <listener-class>  
  18.   
  19.             org.springframework.web.context.ContextLoaderListener   
  20.   
  21.         </listener-class>  
  22.   
  23.     </listener>  
  24.   
  25.     
  26.   
  27. <servlet>  
  28.   
  29.         <servlet-name>service</servlet-name>  
  30.   
  31.         <servlet-class>  
  32.   
  33.             org.springframework.web.servlet.DispatcherServlet   
  34.   
  35.         </servlet-class>  
  36.   
  37.         <load-on-startup>1</load-on-startup>  
  38.   
  39.     </servlet>  
  40.   
  41.     
  42.   
  43.     <servlet-mapping>  
  44.   
  45.         <servlet-name>service</servlet-name>  
  46.   
  47.         <url-pattern>/service/*</url-pattern>  
  48.   
  49.     </servlet-mapping>  
  50.   

说明:不了解为什么这么配置的可以去看看spring mvc方面的资料。

好了,经过以上配置,一个基于spring HTTP invoker的远程服务就完成了,服务的地址为:

http://${serviceName}:${port}/${contextPath}/service/httpService

 

客户端

1.       创建服务接口及网络间传输的DTO类

为了方便,可以将服务器端创建好的的UcService.java和UserInfo.java拷贝到客户端,或打个jar包放到lib下。

2.       配置访问服务

²       WEB-INF/application-context.xml:如果项目中已经存在spring配置文件,则不需要创建该文件,需要配置HTTP invoker的代理

  1. <bean id="httpService"  
  2.   
  3. class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
  4.   
  5.         <property name="serviceUrl">  
  6.   
  7.             <value> http://${serviceName}:${port}/${contextPath}/service/httpService   
  8.   
  9. </value>  
  10.   
  11.         </property>  
  12.   
  13.         <property name="serviceInterface"  
  14.   
  15.             value="com.netqin.baike.service.UcService">  
  16.   
  17.         </property>  
  18.   
  19. </bean>  
  20.   

说明:客户端使用HttpInvokerProxyFactoryBean代理客户端向服务器端发送请求,请求接口为UcService的服务

注意:需要修改serviceUrl为实际的服务器地址

²         WEB-INF/web.xml:配置spring监听

如果项目没有spring环境,则需要在web.xml中加入对spring的支持

  1. <context-param>  
  2.   
  3.         <param-name>contextConfigLocation</param-name>  
  4.   
  5.         <param-value>  
  6.   
  7.             /WEB-INF/application-context.xml   
  8.   
  9.         </param-value>  
  10.   
  11.     </context-param>  
  12.   
  13.     
  14.   
  15.     <listener>  
  16.   
  17.         <listener-class>  
  18.   
  19.             org.springframework.web.context.ContextLoaderListener   
  20.   
  21.         </listener-class>  
  22.   
  23. </listener>  
  24.   

 

3.       访问服务方法

u         读取spring上下文,以远程调用getUserInfobyName方法为例

²         在jsp,servlet,action等等文件中

  1. UcService service = (UcService) WebApplicationContextUtils   
  2.   
  3.         .getRequiredWebApplicationContext(   
  4.   
  5.             request.getSession().getServletContext()).getBean(   
  6.   
  7.             "httpService");   
  8.   
  9. UserInfo user = service.getUserInfobyName("hanqunfeng");   
  10.   

 ²         如果不想配置spring运行环境,可以使用如下方式:

  1. ApplicationContext applicationContext    
  2.   
  3. new FileSystemXmlApplicationContext("classpath:application-context.xml");   
  4.   
  5. service = (UcService) applicationContext.getBean("httpService");   

u         依赖注入,远程调用recordLog方法为例

²         在WEB-INF/application-context.xml中加入如下配置:

  1. <bean id="abc" class="com.netqin.test.abc">  
  2.   
  3.         <property name="service">  
  4.   
  5.             <ref bean="httpService" />  
  6.   
  7.         </property>  
  8.   
  9. </bean>  
  10.   

²         为com.netqin.test.abc中加入对service的set方法:

  1. private UcService service;   
  2.   
  3.     public void setService(UcService service){   
  4.   
  5.         this.service = service;   
  6.   
  7.     }   
  8.   
  9.     
  10.   
  11.     public String recordUserLog(String username,String point,String operate,String desc){   
  12.   
  13.         String result = service.recordLog(username, point, operate, desc);   
  14.   
  15.         return result;   
  16.   
  17. }   
  18.   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多