Spring整合Hessian的使用

该文章转赞自  https://www.cnblogs.com/ontheroad_lee/p/3797239.htm

个人感觉写的非常好,刚学习,先记录下来


1.1     Hessian简介

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

1.2     整合

    1.2.1 概述

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

    1.2.2 服务端整合

        基于客户端要调用服务端的远程服务,所以我们先来谈一下服务端的整合。Hessian的远程服务是基于接口的,所以我们要作为远程服务的实现类必须要实现一个接口。

        作为示例,这里我们建立一个叫hessianServer的web项目作为远程服务的服务端,在这个项目中我们建立一个叫做UserService的接口:

  1. package com.lonely.hessian.server;
  2. /**
  3. * 描述: 该类是 作为 hessian服务端的 要提供的接口
  4. *
  5. * @author 80003071
  6. *
  7. */
  8. public interface IUserService {
  9. void addUser();
  10. void updateUser();
  11. void deleteUser();
  12. }

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

  1. package com.lonely.hessian.server.impl;
  2. import com.lonely.hessian.server.IUserService;
  3. /**
  4. * 描述: 作为hessian服务端的 要提供的接口的实现类
  5. *
  6. * @author 80003071
  7. *
  8. */
  9. public class UserService implements IUserService {
  10. @Override
  11. public void addUser() {
  12. System.out.println("-----------调用了实现类的 addUser()方法-------------");
  13. }
  14. @Override
  15. public void updateUser() {
  16. System.out.println("-----------调用了实现类的 updateUser()方法-------------");
  17. }
  18. @Override
  19. public void deleteUser() {
  20. System.out.println("-----------调用了实现类的 deleteUser()方法-------------");
  21. }
  22. }

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

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <servlet>
  7. <servlet-name>hessianServer</servlet-name>
  8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9. <init-param>
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>classpath:application-hessian.xml</param-value>
  12. </init-param>
  13. <load-on-startup>1</load-on-startup>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>hessianServer</servlet-name>
  17. <url-pattern>/hessian/*</url-pattern>
  18. </servlet-mapping>
  19. </web-app>


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

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

  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"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  9. <bean id="userService" class="com.lonely.hessian.server.impl.UserService"></bean>
  10. <bean name="/userService" class="org.springframework.remoting.caucho.HessianServiceExporter">
  11. <property name="service" ref="userService"></property>
  12. <property name="serviceInterface" value="com.lonely.hessian.server.IUserService"></property>
  13. </bean>
  14. </beans>


       注意看我们的HessianServiceExporter对应的bean的name是“/userService”,在SpringMVC的配置文件中,当一个bean的name是以“/”开始的时候Spring会自动对它进行BeanNameUrlHandlerMapping。所以这里相当于是我们把“/userService”映射到了HessianServiceExporter,根据上面的配置我们要请求这个远程服务的时候应该请求“/api/service/userService”。虽然说在Spring的配置文件中我们把bean的名称设为以“/”开始时Spring会自动对它进行一个beanName映射。

       注意,因为是根据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接口的内容是一样的。代码如下所示:

  1. package com.lonely.hessian.client;
  2. public interface IUserService {
  3. void addUser();
  4. void updateUser();
  5. void deleteUser();
  6. }


 

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

  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"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  9. <bean id="userService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
  10. <property name="serviceUrl" value="http://localhost:8080/hessianServer/hessian/userService"></property>
  11. <property name="serviceInterface" value="com.lonely.hessian.client.IUserService"></property>
  12. </bean>
  13. </beans>


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

  1. package com.lonely.hessian.client;
  2. import org.junit.runner.RunWith;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.test.context.ContextConfiguration;
  5. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  6. @RunWith(SpringJUnit4ClassRunner.class)
  7. @ContextConfiguration("classpath:applicationContext.xml")
  8. public class HessianClientTest {
  9. @Autowired
  10. private IUserService userService;
  11. @org.junit.Test
  12. public void Test() {
  13. this.userService.addUser();
  14. }
  15. }


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

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