Spring整合Hessian访问远程服务

来源:互联网 发布:g71指令编程简单实例 编辑:程序博客网 时间:2024/05/22 06:07

Spring整合Hessian访问远程服务



1.1     Hessian简介

       Hessian是一个轻量级的Web服务实现工具,它采用的是二进制协议,因此很适合发送二进制数据。它的一个基本原理就是把远程服务对象以二进制的方式进行发送和接收。

1.2     整合

1.2.1 概述

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

1.2.2 服务端整合

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

Java代码 收藏代码
  1. package com.tiantian.hessianserver.service;  
  2.    
  3. public interface UserService {  
  4.    
  5.        public void addUser();  
  6.         
  7.        public void updateUser();  
  8.         
  9.        public void delUser();  
  10.         
  11.        public String findUser(String username);  
  12.         
  13. }  

 

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

Java代码 收藏代码
  1. package com.tiantian.hessianserver.service.impl;  
  2.    
  3. import com.tiantian.hessianserver.service.UserService;  
  4.    
  5. public class UserServiceImpl implements UserService {  
  6.    
  7.        public void addUser() {  
  8.               System.out.println("-------------invoke addUser()---------------");  
  9.        }  
  10.    
  11.        public void updateUser() {  
  12.               System.out.println("-------------invoke updateUser()---------------");  
  13.        }  
  14.    
  15.        public void delUser() {  
  16.               System.out.println("-------------invoke delUser()---------------");  
  17.        }  
  18.         
  19.        public String findUser(String username) {  
  20.               System.out.println("-------------invoke findUser---------------");  
  21.               return "return: " + username;  
  22.        }  
  23.    
  24. }  

 

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

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

Xml代码 收藏代码
  1. <servlet>  
  2.        <servlet-name>hessianServer</servlet-name>  
  3.        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.        <load-on-startup>1</load-on-startup>  
  5. </servlet>  
  6.   
  7. <servlet-mapping>  
  8.        <servlet-name>hessianServer</servlet-name>  
  9.        <url-pattern>/api/service/*</url-pattern>  
  10. </servlet-mapping>  

 

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

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

Xml代码 收藏代码
  1. <bean id="userService" class="com.tiantian.hessianserver.service.impl.UserServiceImpl" />  
  2. <bean name="/userService"  
  3.        class="org.springframework.remoting.caucho.HessianServiceExporter">  
  4.        <property name="service" ref="userService" />  
  5.        <property name="serviceInterface" value="com.tiantian.hessianserver.service.UserService" />  
  6. </bean>  

 

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

Xml代码 收藏代码
  1. <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>  
  2. <bean id="userService" class="com.tiantian.hessianserver.service.impl.UserServiceImpl" />  
  3. <bean name="/userService"  
  4.        class="org.springframework.remoting.caucho.HessianServiceExporter">  
  5.        <property name="service" ref="userService" />  
  6.        <property name="serviceInterface" value="com.tiantian.hessianserver.service.UserService" />  
  7. </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代码 收藏代码
  1. package com.tiantian.hessianserver.service;  
  2.    
  3. public interface UserService {  
  4.    
  5.        public void addUser();  
  6.         
  7.        public void updateUser();  
  8.         
  9.        public void delUser();  
  10.         
  11.        public String findUser(String username);  
  12.         
  13. }  

 

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

Xml代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.        xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.      http://www.springframework.org/schema/context  
  8.      http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  9.    
  10.        <bean id="userService"  
  11.               class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
  12.               <property name="serviceUrl"  
  13.                      value="http://localhost:8080/hessianServer/api/service/userService" />  
  14.               <property name="serviceInterface" value="com.tiantian.hessianserver.service.UserService" />  
  15.        </bean>  
  16. </beans>  

 

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

Java代码 收藏代码
  1. package com.tiantian.hessianclient.test;  
  2.    
  3. import org.junit.Test;  
  4. import org.junit.runner.RunWith;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.test.context.ContextConfiguration;  
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  8. import com.tiantian.hessianserver.service.UserService;  
  9.    
  10. @RunWith(SpringJUnit4ClassRunner.class)  
  11. @ContextConfiguration("/applicationContext.xml")  
  12. public class HessianTest {  
  13.    
  14.        @Autowired  
  15.        private UserService userService;  
  16.         
  17.        @Test  
  18.        public void test() {  
  19.               userService.addUser();  
  20.               userService.updateUser();  
  21.               userService.delUser();  
  22.               String user = userService.findUser("ZhangSan");  
  23.               System.out.println(user);  
  24.               System.out.println("---------------------------------finished----------------------------------");  
  25.        }  
  26.         
  27. }  

 

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



 

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



 

       这说明我们已经成功地调用了远程服务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代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    
  4.        xmlns:mvc="http://www.springframework.org/schema/mvc"    
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans    
  6.      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  7.      http://www.springframework.org/schema/context    
  8.      http://www.springframework.org/schema/context/spring-context-3.0.xsd">    
  9.      
  10.        <bean id="userService"    
  11.               class="org.springframework.remoting.caucho.HessianProxyFactoryBean">    
  12.               <property name="serviceUrl"    
  13.                      value="http://localhost:8080/hessianServer/api/service/userService" />    
  14.               <property name="serviceInterface" value="com.tiantian.hessianserver.service.UserService" />  
  15.               <!--新增overloadEnabled属性,并把它的值设置为true,默认是false,则Hessian就能支持方法的重载了。-->  
  16.               <property name="overloadEnabled" value="true" />  
  17.        </bean>    
  18. </beans>  

 

0 0
原创粉丝点击