該文章轉贊自 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。