首页 > 代码库 > Hessian 初探

Hessian 初探

Hessian 是一个序列化协议, 他的优点在于比 Java 原生的对象序列化/反序列化速度更快, 序列化出来以后的数据更小.

序列化协议跟应用层协议无关, 可以将 Hessian 序列化以后的数据放在 HTTP Body 里, 也可以放在 DUBBO 里, 或者直接用 Socket 传输

下面是一个使用 Jetty 跟 Hessian 实现的 Hessian web service 的例子

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>hessian-test</groupId>    <artifactId>hessian-test</artifactId>    <packaging>war</packaging>    <version>1.0-SNAPSHOT</version>    <name>hessian-test Maven Webapp</name>    <url>http://maven.apache.org</url>    <dependencies>        <dependency>            <groupId>com.caucho</groupId>            <artifactId>hessian</artifactId>            <version>4.0.38</version>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>servlet-api</artifactId>        </dependency>    </dependencies>    <build>        <finalName>hessian-test</finalName>    </build></project>

提供服务的接口 HelloService

public interface HelloService {    public String sayHello(String name);}

服务端的实现

import com.caucho.hessian.server.HessianServlet;public class HelloServlet extends HessianServlet implements HelloService {    @Override    public String sayHello(String name) {        return "Hello " + name;    }}

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4"         xmlns="http://java.sun.com/xml/ns/j2ee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <display-name>Archetype Created Web Application</display-name>    <servlet>        <servlet-name>HelloServlet</servlet-name>        <servlet-class>com.qunar.test.HelloServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>HelloServlet</servlet-name>        <url-pattern>/hello</url-pattern>    </servlet-mapping></web-app>

 

客户端的调用

import com.caucho.hessian.client.HessianProxyFactory;public class Client {    public static void main(String[] args) throws Exception {        String url = "http://localhost:8080/hello";        HessianProxyFactory factory = new HessianProxyFactory();        HelloService service = (HelloService) factory.create(HelloService.class, url);        System.out.println(service.sayHello("Vee"));    }}

tomcat部署后启动, 调用后输出

Hello Vee

用 tcpdump 查看传输的数据

请求

17:07:23.287059 IP localhost.59914 > localhost.http-alt: Flags [P.], seq 1:243, ack 1, win 9186, options [nop,nop,TS val 503550177 ecr 503550095], length 242    0x0000:  4500 0126 5eb1 4000 4006 0000 7f00 0001  E..&^.@.@.......    0x0010:  7f00 0001 ea0a 1f90 b925 a236 308a bf24  .........%.60..$    0x0020:  8018 23e2 ff1a 0000 0101 080a 1e03 90e1  ..#.............    0x0030:  1e03 908f 504f 5354 202f 6865 6c6c 6f20  ....POST./hello.    0x0040:  4854 5450 2f31 2e31 0d0a 436f 6e74 656e  HTTP/1.1..Conten    0x0050:  742d 5479 7065 3a20 782d 6170 706c 6963  t-Type:.x-applic    0x0060:  6174 696f 6e2f 6865 7373 6961 6e0d 0a41  ation/hessian..A    0x0070:  6363 6570 742d 456e 636f 6469 6e67 3a20  ccept-Encoding:.    0x0080:  6465 666c 6174 650d 0a55 7365 722d 4167  deflate..User-Ag    0x0090:  656e 743a 204a 6176 612f 312e 372e 305f  ent:.Java/1.7.0_    0x00a0:  3531 0d0a 486f 7374 3a20 6c6f 6361 6c68  51..Host:.localh    0x00b0:  6f73 743a 3830 3830 0d0a 4163 6365 7074  ost:8080..Accept    0x00c0:  3a20 7465 7874 2f68 746d 6c2c 2069 6d61  :.text/html,.ima    0x00d0:  6765 2f67 6966 2c20 696d 6167 652f 6a70  ge/gif,.image/jp    0x00e0:  6567 2c20 2a3b 2071 3d2e 322c 202a 2f2a  eg,.*;.q=.2,.*/*    0x00f0:  3b20 713d 2e32 0d0a 436f 6e6e 6563 7469  ;.q=.2..Connecti    0x0100:  6f6e 3a20 6b65 6570 2d61 6c69 7665 0d0a  on:.keep-alive..    0x0110:  436f 6e74 656e 742d 4c65 6e67 7468 3a20  Content-Length:.    0x0120:  3231 0d0a 0d0a                           21....17:07:23.287089 IP localhost.59914 > localhost.http-alt: Flags [P.], seq 243:264, ack 1, win 9186, options [nop,nop,TS val 503550177 ecr 503550095], length 21    0x0000:  4500 0049 8a49 4000 4006 0000 7f00 0001  E..I.I@.@.......    0x0010:  7f00 0001 ea0a 1f90 b925 a328 308a bf24  .........%.(0..$    0x0020:  8018 23e2 fe3d 0000 0101 080a 1e03 90e1  ..#..=..........    0x0030:  1e03 908f 6302 006d 0008 7361 7948 656c  ....c..m..sayHel    0x0040:  6c6f 5300 0356 6565 7a                   loS..Veez

响应

17:07:23.288131 IP localhost.http-alt > localhost.59914: Flags [P.], seq 1:168, ack 264, win 9170, options [nop,nop,TS val 503550178 ecr 503550177], length 167    0x0000:  4500 00db ab9f 4000 4006 0000 7f00 0001  E.....@.@.......    0x0010:  7f00 0001 1f90 ea0a 308a bf24 b925 a33d  ........0..$.%.=    0x0020:  8018 23d2 fecf 0000 0101 080a 1e03 90e2  ..#.............    0x0030:  1e03 90e1 4854 5450 2f31 2e31 2032 3030  ....HTTP/1.1.200    0x0040:  204f 4b0d 0a53 6572 7665 723a 2041 7061  .OK..Server:.Apa    0x0050:  6368 652d 436f 796f 7465 2f31 2e31 0d0a  che-Coyote/1.1..    0x0060:  436f 6e74 656e 742d 5479 7065 3a20 782d  Content-Type:.x-    0x0070:  6170 706c 6963 6174 696f 6e2f 6865 7373  application/hess    0x0080:  6961 6e0d 0a54 7261 6e73 6665 722d 456e  ian..Transfer-En    0x0090:  636f 6469 6e67 3a20 6368 756e 6b65 640d  coding:.chunked.    0x00a0:  0a44 6174 653a 2054 6875 2c20 3138 2053  .Date:.Thu,.18.S    0x00b0:  6570 2032 3031 3420 3039 3a30 373a 3233  ep.2014.09:07:23    0x00c0:  2047 4d54 0d0a 0d0a 650d 0a48 0200 5209  .GMT....e..H..R.    0x00d0:  4865 6c6c 6f20 5665 650d 0a              Hello.Vee..

可以看到相对于直接传输字符串数据, hessian 序列化以后多了一些控制字符.

//

如果仅仅是传输字符串, 实际上没有必要使用 hessian, 因为这样反而多了序列化反序列的过程, 数据也多了控制字符, 变得更大

而对于复杂的 java 对象, 用 hessian 则更合适

Hessian 初探