一、简介
Hessian和Burlap是由Caucho Technology提供的基于HTTP协议的轻量级远程服务解决方案。他们都致力于借助尽可能简单那的API和通信协议来简化Web服务。
Hession和Burlap就如同一个事物的两面,但是每一个解决方案都服务于略微不同的目的。Hession就像RMI一样,使用二进制尽心客户端和服务端的交互。但与其他二进制远程调用技术(如,RMI)不同的是,它的二进制消息可以移植到其他开发语言中(如,PHP、Python、C++、C#)。Burlap是一种基于XML的远程调用技术,这使得它可以自然而然的移植到任何能够解析XML的语言上。正因为如此,Burlap比起Hessian的二进制格式而言有更强的可读性。但,与其他基于XML的远程技术(如,SOAP、XML-RPC)不同,Burlap的消息结构尽可能的简单,不需要额外的外部定义语言(如,WSDL、IDL)。
你可能想知道如何在Hession和Burlap之间抉择,很大程度,他们是一样的。唯一的区别在于Hession的消息似乎二进制的,在带宽上更有优势,而Burlap的消息是XML的,有更好的可读性。
由于Hessian和Burlap都是基于HTTP协议的,他们都解决了RMI所头疼的防火墙渗透问题。但是当传递过来的RPC消息中包含序列化对象时,RMI就完胜Hessian和Burlap了。因为Hessian和Burlap都采用私有的序列化机制,如果数据模型非常复杂,那么Hessian和Burlap的序列化模型可能无法胜任。
二、Hession开发步骤
1、编写服务接口
1 package com.cnblogs.javalouvre.service; 2 3 public interface GreetService { 4 5 String sayHello(String name); 6 7 }
2、编写服务实现类,须继承自com.caucho.hessian.server.HessianServlet
1 package com.cnblogs.javalouvre.service; 2 3 import com.caucho.hessian.server.HessianServlet; 4 5 public class GreetServiceImpl extends HessianServlet implements GreetService { 6 7 private static final long serialVersionUID = 1880738686281295739L; 8 9 @Override 10 public String sayHello(String name) { 11 return "Hello " + name; 12 } 13 14 }
3、配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 version="2.5"> 6 7 <servlet> 8 <servlet-name>HessianServlet</servlet-name> 9 <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> 10 <init-param> 11 <param-name>home-class</param-name> 12 <param-value>com.cnblogs.javalouvre.service.GreetServiceImpl</param-value> 13 </init-param> 14 <init-param> 15 <param-name>home-api</param-name> 16 <param-value>com.cnblogs.javalouvre.service.GreetService</param-value> 17 </init-param> 18 <load-on-startup>1</load-on-startup> 19 </servlet> 20 21 <servlet-mapping> 22 <servlet-name>HessianServlet</servlet-name> 23 <url-pattern>/GreetService</url-pattern> 24 </servlet-mapping> 25 26 <welcome-file-list> 27 <welcome-file>index.html</welcome-file> 28 <welcome-file>index.htm</welcome-file> 29 <welcome-file>index.jsp</welcome-file> 30 </welcome-file-list> 31 32 </web-app>
4、测试客户端
1 package com.cnblogs.javalouvre.client; 2 3 import java.net.MalformedURLException; 4 5 import com.caucho.hessian.client.HessianProxyFactory; 6 import com.cnblogs.javalouvre.service.GreetService; 7 8 public class Client { 9 10 public static void main(String[] args) { 11 String url = "http://10.108.1.138:8080/Hessian/GreetService"; 12 13 try { 14 GreetService service = (GreetService) (new HessianProxyFactory()).create(GreetService.class, url); 15 System.out.println(service.sayHello("Jobs")); 16 } catch (MalformedURLException e) { 17 e.printStackTrace(); 18 } 19 } 20 21 }
三、Burlap开发步骤
1、编写服务接口(同Hessian示例的接口)
2、编写服务实现类,须继承自com.caucho.burlap.server.BurlapServlet
1 package com.cnblogs.javalouvre.service; 2 3 import com.caucho.burlap.server.BurlapServlet; 4 5 public class GreetServiceImpl extends BurlapServlet implements GreetService { 6 7 @Override 8 public String sayHello(String name) { 9 return "Hello " + name; 10 } 11 12 }
3、配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 version="2.5"> 6 7 <servlet> 8 <servlet-name>GreetService</servlet-name> 9 <servlet-class>com.cnblogs.javalouvre.service.GreetServiceImpl</servlet-class> 10 <load-on-startup>1</load-on-startup> 11 </servlet> 12 13 <servlet-mapping> 14 <servlet-name>GreetService</servlet-name> 15 <url-pattern>/GreetService</url-pattern> 16 </servlet-mapping> 17 18 <welcome-file-list> 19 <welcome-file>index.html</welcome-file> 20 <welcome-file>index.htm</welcome-file> 21 <welcome-file>index.jsp</welcome-file> 22 </welcome-file-list> 23 24 </web-app>
4、测试客户端
1 package com.cnblogs.javalouvre.client; 2 3 import java.net.MalformedURLException; 4 5 import com.caucho.burlap.client.BurlapProxyFactory; 6 import com.cnblogs.javalouvre.service.GreetService; 7 8 public class Client { 9 10 public static void main(String[] args) { 11 String url = "http://10.108.1.138:8080/Burlap/GreetService"; 12 13 try { 14 GreetService service = (GreetService) (new BurlapProxyFactory()).create(GreetService.class, url); 15 System.out.println(service.sayHello("Jobs")); 16 } catch (MalformedURLException e) { 17 e.printStackTrace(); 18 } 19 } 20 21 }