SpringMVC整合Hessian远程服务调用

来源:互联网 发布:怎样设置mac的背光键盘 编辑:程序博客网 时间:2024/06/06 02:05


1.1     Hessian简介

     Hessian是一个轻量级的Web服务实现工具,底层基于Http协议,采用二进制数据格式进行传输。它的一个基本原理就是把远程服务对象以二进制的方式进行发送和接收。

1.2     整合

1.2.1 概述

对于Hessian而言,有服务端和客户端,所以我们的整合也需要分服务端的整合和客户端的整合。服务端的整合是通过SpringMVC进行的,而客户端的整合则是通过Spring的bean进行的。

1.2.2 服务端整合

基于客户端要调用服务端的远程服务,所以我们先来谈一下服务端的整合。Hessian的远程服务是基于接口的,所以我们要作为远程服务的实现类必须要实现一个接口。作为示例,这里我们建立一个叫hessianServer的web项目作为远程服务的服务端,在这个项目中我们建立一个叫做UserService的接口:

Java代码  
package com.tiantian.hessianserver.service;     public interface UserService {            public void addUser();                 public void updateUser();                 public void delUser();                 public String findUser(String username);          }  

       然后建立一个它的实现类UserServiceImpl:

Java代码  
package com.tiantian.hessianserver.service.impl;     import com.tiantian.hessianserver.service.UserService;     public class UserServiceImpl implements UserService {            public void addUser() {                System.out.println("-------------invoke addUser()---------------");         }            public void updateUser() {                System.out.println("-------------invoke updateUser()---------------");         }            public void delUser() {                System.out.println("-------------invoke delUser()---------------");         }                 public String findUser(String username) {                System.out.println("-------------invoke findUser---------------");                return "return: " + username;         }     }  


       那么接下来我们要做的就是把UserServiceImpl作为一个远程服务进行发布,以致客户端能够进行访问。

       首先我们需要在web.xml中配置一个SpringMVC的DispatcherServlet用于接收所有的Web服务请求,这里我们这样配置:

Xml代码  
<servlet>         <servlet-name>hessianServer</servlet-name>         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         <load-on-startup>1</load-on-startup>  </servlet>    <servlet-mapping>         <servlet-name>hessianServer</servlet-name>         <url-pattern>/api/service/*</url-pattern>  </servlet-mapping>  

       可以看到我们这个DispatcherServlet会处理url为“/api/service/*”的请求,通配符“*”就对应着我们的处理器映射。

       接下来就是在SpringMVC的配置文件中利用Hessian来定义我们的远程服务了,这是通过Spring提供的HessianServiceExporter来实现的。我们需要在SpringMVC的配置文件中定义一个类型为HessianServiceExporter的bean对象。该bean对象需要接收两个属性,一个是service属性,用于关联真正的service对象,另一个是serviceInterface属性,用于指定当前的服务对应的接口。HessianServiceExporter实现了HttpRequestHandler接口,当我们请求某一个远程服务的时候实际上请求的就是其对应的HessianServiceExporter对象,HessianServiceExporter会把请求的服务以二进制的方式返回给客户端。这里我们在SpringMVC的配置文件中这样定义:

Xml代码
<bean id="userService" class="com.tiantian.hessianserver.service.impl.UserServiceImpl" />  <bean name="/userService"         class="org.springframework.remoting.caucho.HessianServiceExporter">         <property name="service" ref="userService" />         <property name="serviceInterface" value="com.tiantian.hessianserver.service.UserService" />  </bean>  

       注意看我们的HessianServiceExporter对应的bean的name是“/userService”,在SpringMVC的配置文件中,当一个bean的name是以“/”开始的时候Spring会自动对它进行BeanNameUrlHandlerMapping。所以这里相当于是我们把“/userService”映射到了HessianServiceExporter,根据上面的配置我们要请求这个远程服务的时候应该请求“/api/service/userService”。虽然说在Spring的配置文件中我们把bean的名称设为以“/”开始时Spring会自动对它进行一个beanName映射,但有一次不知道是哪里影响了,我这样使用的时候Spring没有对它进行自动映射,所以为了保险起见,这里我们最好自己指定一个BeanNameUrlHandlerMapping,代码如下所示:

Xml代码 
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>  <bean id="userService" class="com.tiantian.hessianserver.service.impl.UserServiceImpl" />  <bean name="/userService"         class="org.springframework.remoting.caucho.HessianServiceExporter">         <property name="service" ref="userService" />         <property name="serviceInterface" value="com.tiantian.hessianserver.service.UserService" />  </bean>  

       注意,因为是根据beanName来进行映射的,所以我们必须要给HessianServiceExporter bean对象指定name属性,而且其对应的name必须以“/”开头,这样我们的客户端才能访问到对应的服务。

1.2.3 客户端整合

对于客户端要使用远程的Hessian服务的,我们需要在Spring的配置文件中定义对应的org.springframework.remoting.caucho.HessianProxyFactoryBean bean对象。     HessianProxyFactoryBean对象需要指定两个属性,一个是serviceInterface属性,表示当前请求的远程服务对应的接口;另一个是serviceUrl属性,表示当前的远程服务对应的服务端请求地址。这里在客户端为了使用hessianServer定义的UserService服务,我们建立一个对应的hessianClient项目,在hessianClient中我们定义一个对应的UserService接口,这个接口的内容跟hessianServer中的UserService接口的内容是一样的。代码如下所示:

Java代码  

package com.tiantian.hessianserver.service;     public interface UserService {            public void addUser();                 public void updateUser();                 public void delUser();                 public String findUser(String username);          } 

       之后我们就在当前Spring的配置文件中定义对应的HessianProxyFactoryBean对象。HessianProxyFactoryBean会根据指定的serviceInterface和serviceUrl属性返回一个serviceInterface对应的代理对象。这里我们的Spring配置文件这样定义:

Xml代码  
<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"         xmlns:mvc="http://www.springframework.org/schema/mvc"         xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-3.0.xsd">            <bean id="userService"                class="org.springframework.remoting.caucho.HessianProxyFactoryBean">                <property name="serviceUrl"                       value="http://localhost:8080/hessianServer/api/service/userService" />                <property name="serviceInterface" value="com.tiantian.hessianserver.service.UserService" />         </bean>  </beans>  

       可以看到我们通过HessianProxyFactoryBean定义了一个UserService对应的远程代理对象,之后我们就可以在我们的程序中把它作为一个普通的bean对象来使用这个UserService的代理对象了。这里我们定义以下测试代码:

Java代码  
package com.tiantian.hessianclient.test;     import org.junit.Test;  import org.junit.runner.RunWith;  import org.springframework.beans.factory.annotation.Autowired;  import org.springframework.test.context.ContextConfiguration;  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  import com.tiantian.hessianserver.service.UserService;     @RunWith(SpringJUnit4ClassRunner.class)  @ContextConfiguration("/applicationContext.xml")  public class HessianTest {            @Autowired         private UserService userService;                 @Test         public void test() {                userService.addUser();                userService.updateUser();                userService.delUser();                String user = userService.findUser("ZhangSan");                System.out.println(user);                System.out.println("---------------------------------finished----------------------------------");         }          }  

     之后我们启动我们的hessianServer,然后执行上面的测试程序,在服务端会输出如下内容:

-------------invoke addUser()---------------
-------------invoke updateUser()---------------
-------------invoke delUser()---------------
-------------invoke findUser---------------

       在客户端会输出如下内容:

    

                              Zhansan
---------------------------------finished----------------------------------

       这说明我们已经成功地调用了远程服务UserService。

注:

    1.Hessian不支持方法的重载,打个比方现在有一AddService,里面有一add(int a, int b)和一add(long a, long b)方法,然后我们把它发布为一个Hessian服务。那么当我们想要远程访问AddService的add方法时Hessian会报错,抛出异常

com.caucho.hessian.io.HessianProtocolException: '?' is an unknown code

因为默认情况下它是不支持方法的重载,这个时候我们可以在客户端使用的时候新增属性overloadEnabled,值为true。如:

Xml代码  
<?xml version="1.0" encoding="UTF-8"?>    <beans xmlns="http://www.springframework.org/schema/beans"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"           xmlns:mvc="http://www.springframework.org/schema/mvc"           xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd">                <bean id="userService"                  class="org.springframework.remoting.caucho.HessianProxyFactoryBean">                  <property name="serviceUrl"                         value="http://localhost:8080/hessianServer/api/service/userService" />                  <property name="serviceInterface" value="com.tiantian.hessianserver.service.UserService" />                <!--新增overloadEnabled属性,并把它的值设置为true,默认是false,则Hessian就能支持方法的重载了。-->                <property name="overloadEnabled" value="true" />         </bean>    </beans>  


原创粉丝点击