Hessian是二進制的web service協議,官方網站提供java、Flash/Flex、Python、C++、.NET C#等實現。Hessian和Axis、XFire都能實現web service方式的遠程方法調用,區別是Hessian是二進制協議,Axis、XFire則是SOAP協議,所以從性能上說Hessian遠優于后兩者,并且Hessian的JAVA使用方法非常簡單。Hessian由于沒有WSDL這種服務描述文件去對實現進行規定,似乎更適合內部分布式系統之間的交互,對外提供服務還是使用后兩者更體面些。hessian采用的是二進制RPC協議,因為采用了二進制協議,所以它很適合于發送二進制數據,Hessian主要作面向對象的消息通信。Hessian的初衷就是支持動態類型,格式緊湊,跨語言Hessian是使用自己的序列化機制實現的編組和反編組,其支持的數據類型是有限制的,不支持復雜的對象,可以穿透防火墻,在這里不得不說一下RMI:RMI是一組用戶開發分布式應用程序的API。他使用的是java序列化機制實現調用及返回值的編組于反編組。它使用Java語言接口定義了遠程對象,它集合了Java序列化和Java遠程方法協議(Java Remote Method PRotocol)。他可以被看做是RPC的Java版本,因為傳統的RPC并不能很好的應用于分布式對象系統。而Java RMI則支持存儲于不同地址空間的程序級對象之間彼此進行通信,實現遠程對象之間的無縫遠程調用。他也有它的缺點,他只能通過RMI協議來進行訪問無法通過HTTP協議訪問,無法穿透防火墻。
JAVA服務端使用步驟:1、導入Hessian的Jar包2、設計接口3、實現接口:必須繼承HessianServlet,接口參數對象必須實現序列化4、配置web.xml
<servlet> <!-- 配置 HessianServlet,Servlet的名字隨便配置,例如這里配置成ServiceServlet--> <servlet-name>ServiceServlet</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <!-- 配置接口的具體實現類 --> <init-param> <param-name>service-class</param-name> <param-value>com.hessian.service.impl.ServiceImpl</param-value> </init-param> </servlet> <!-- 映射 HessianServlet的訪問URL地址--> <servlet-mapping> <servlet-name>ServiceServlet</servlet-name> <url-pattern>/ServiceServlet</url-pattern> </servlet-mapping>hessian與spring結合.
下面來自http://blog.sina.com.cn/s/blog_7f73e06d0100xn9j.html
在實際應用中,我們不只是簡單的只使用hessian來進行通信的,如果方法多得話,還不如直接寫在客戶端來調用,然而:當hessian與spring結合后,大大減少了這些操作,將dao層的操作全部放在hessian服務端,將業務邏輯全部放在hessian客戶端,這樣的話我們的hessian客戶端和服務端完全分離,因此我們的業務邏輯和dao層就真正的達到了分離,就可以放在不同的服務器上,當然hessian的通信的作用不僅僅只有這些。接口和實現和上邊的一樣:只是在web.xml中配置比較麻煩:例子:1、服務器端:增加remoting-servlet.xml配置文件:用來配置bean,并將bean導出為hessian服務:
<?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:aop = "http://www.springframework.org/schema/aop" xmlns:tx = "http://www.springframework.org/schema/tx" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" > <!-- 定義普通的bean實例 --> <bean id="Hello" class="com.kcpt.hessian.service.IHelloImpl"/> <!-- 使用HessianServiceExporter 將普通bean導出成Hessian服務--> <bean name="/remoting" class="org.springframework.remoting.caucho.HessianServiceExporter"> <!-- 需要導出的目標bean--> <property name="service" ref="Hello"/> <!-- Hessian服務的接口--> <property name="serviceInterface" value="com.kcpt.hessian.service.IHello"/> </bean> </beans>
2、web.xml文件的配置:首先是監聽器:spring的監聽器
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <!--添加監聽器 --></listener><!-- 指定spring的配置文件在哪里,在這個配置文件中導出了Hessian服務 --><context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/remoting-servlet.xml</param-value> </context-param><!-- Hessian通過Servlet提供遠程服務,需要將某個匹配的模式映射到hessian服務中,spring的dispatcherServlet能完成此功能,DispatcherServlet可將匹配模式的請求轉發到Hessian服務,web.xml只是定義了“請求轉發器”,該轉發器將匹配/remoting/*的請求截獲,轉發給context的bean處理。而HessianServiceExporter提供bean服務。 --><servlet> <servlet-name>remoting</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet><servlet-mapping><servlet-name>remoting</servlet-name><url-pattern>/remoting/*</url-pattern></servlet-mapping>
3、在客戶端:同樣要加spring監聽器和context-param指定bean的文件聲明bean的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:aop = "http://www.springframework.org/schema/aop" xmlns:tx = "http://www.springframework.org/schema/tx" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" > <bean id="myServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"><property name="serviceUrl"> //hessian的地址和名稱請求轉發的名稱<value>http://127.0.0.1:8080/HessianService/remoting</value></property> <property name="serviceInterface"> //hessian所要調用的接口<value>com.kcpt.hessian.service.IHello</value></property></bean></beans>
4、客戶端的程序中要寫:
applicationContext context = new ClassPathXmlApplicationContext("com/kcpt/hessian/client/remoting-client.xml") //這里只是你聲明的bean的xml文件所在的路徑IHello b = (IHello) context.getBean("myServiceClient");
來獲取到ihello這個接口,從而就能夠調用這個接口里的方法進行操作
新聞熱點
疑難解答