该文章转赞自 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的接口:
- package com.lonely.hessian.server;
-
- /**
- * 描述: 该类是 作为 hessian服务端的 要提供的接口
- *
- * @author 80003071
- *
- */
- public interface IUserService {
-
- void addUser();
-
- void updateUser();
-
- void deleteUser();
- }
然后建立一个它的实现类UserServiceImpl:
- package com.lonely.hessian.server.impl;
-
- import com.lonely.hessian.server.IUserService;
-
- /**
- * 描述: 作为hessian服务端的 要提供的接口的实现类
- *
- * @author 80003071
- *
- */
- public class UserService implements IUserService {
-
- @Override
- public void addUser() {
- System.out.println("-----------调用了实现类的 addUser()方法-------------");
- }
-
- @Override
- public void updateUser() {
- System.out.println("-----------调用了实现类的 updateUser()方法-------------");
- }
-
- @Override
- public void deleteUser() {
- System.out.println("-----------调用了实现类的 deleteUser()方法-------------");
- }
-
- }
那么接下来我们要做的就是把UserServiceImpl作为一个远程服务进行发布,以致客户端能够进行访问。
首先我们需要在web.xml中配置一个SpringMVC的DispatcherServlet用于接收所有的Web服务请求,这里我们这样配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
-
- <servlet>
- <servlet-name>hessianServer</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:application-hessian.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>hessianServer</servlet-name>
- <url-pattern>/hessian/*</url-pattern>
- </servlet-mapping>
-
-
-
- </web-app>
可以看到我们这个DispatcherServlet会处理url为“/hessian/*”的请求,通配符“*”就对应着我们的处理器映射。
接下来就是在SpringMVC的配置文件中利用Hessian来定义我们的远程服务了,这是通过Spring提供的HessianServiceExporter来实现的。我们需要在SpringMVC的配置文件中定义一个类型为HessianServiceExporter的bean对象。该bean对象需要接收两个属性,一个是service属性,用于关联真正的service对象,另一个是serviceInterface属性,用于指定当前的服务对应的接口。HessianServiceExporter实现了HttpRequestHandler接口,当我们请求某一个远程服务的时候实际上请求的就是其对应的HessianServiceExporter对象,HessianServiceExporter会把请求的服务以二进制的方式返回给客户端。这里我们在SpringMVC的配置文件中这样定义:
- <?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
-
- <bean id="userService" class="com.lonely.hessian.server.impl.UserService"></bean>
-
- <bean name="/userService" class="org.springframework.remoting.caucho.HessianServiceExporter">
- <property name="service" ref="userService"></property>
- <property name="serviceInterface" value="com.lonely.hessian.server.IUserService"></property>
- </bean>
-
-
- </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接口的内容是一样的。代码如下所示:
- package com.lonely.hessian.client;
-
- public interface IUserService {
-
- void addUser();
-
- void updateUser();
-
- void deleteUser();
- }
之后我们就在当前Spring的配置文件中定义对应的HessianProxyFactoryBean对象。HessianProxyFactoryBean会根据指定的serviceInterface和serviceUrl属性返回一个serviceInterface对应的代理对象。这里我们的Spring配置文件这样定义:
- <?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
-
- <bean id="userService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
- <property name="serviceUrl" value="http://localhost:8080/hessianServer/hessian/userService"></property>
- <property name="serviceInterface" value="com.lonely.hessian.client.IUserService"></property>
- </bean>
-
-
- </beans>
可以看到我们通过HessianProxyFactoryBean定义了一个UserService对应的远程代理对象,之后我们就可以在我们的程序中把它作为一个普通的bean对象来使用这个UserService的代理对象了。这里我们定义以下测试代码:
- package com.lonely.hessian.client;
-
- 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;
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("classpath:applicationContext.xml")
- public class HessianClientTest {
-
- @Autowired
- private IUserService userService;
-
- @org.junit.Test
- public void Test() {
- this.userService.addUser();
- }
-
- }
之后我们启动我们的hessianServer,然后执行上面的测试程序,在服务端会输出如下内容:
这说明我们已经成功地调用了远程服务UserService。