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 {@Overridepublic 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 2420x0000: 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..Conten0x0050: 742d 5479 7065 3a20 782d 6170 706c 6963 t-Type:.x-applic0x0060: 6174 696f 6e2f 6865 7373 6961 6e0d 0a41 ation/hessian..A0x0070: 6363 6570 742d 456e 636f 6469 6e67 3a20 ccept-Encoding:.0x0080: 6465 666c 6174 650d 0a55 7365 722d 4167 deflate..User-Ag0x0090: 656e 743a 204a 6176 612f 312e 372e 305f ent:.Java/1.7.0_0x00a0: 3531 0d0a 486f 7374 3a20 6c6f 6361 6c68 51..Host:.localh0x00b0: 6f73 743a 3830 3830 0d0a 4163 6365 7074 ost:8080..Accept0x00c0: 3a20 7465 7874 2f68 746d 6c2c 2069 6d61 :.text/html,.ima0x00d0: 6765 2f67 6966 2c20 696d 6167 652f 6a70 ge/gif,.image/jp0x00e0: 6567 2c20 2a3b 2071 3d2e 322c 202a 2f2a eg,.*;.q=.2,.*/*0x00f0: 3b20 713d 2e32 0d0a 436f 6e6e 6563 7469 ;.q=.2..Connecti0x0100: 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 210x0000: 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..sayHel0x0040: 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 1670x0000: 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.2000x0040: 204f 4b0d 0a53 6572 7665 723a 2041 7061 .OK..Server:.Apa0x0050: 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/hess0x0080: 6961 6e0d 0a54 7261 6e73 6665 722d 456e ian..Transfer-En0x0090: 636f 6469 6e67 3a20 6368 756e 6b65 640d coding:.chunked.0x00a0: 0a44 6174 653a 2054 6875 2c20 3138 2053 .Date:.Thu,.18.S0x00b0: 6570 2032 3031 3420 3039 3a30 373a 3233 ep.2014.09:07:230x00c0: 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 則更合適
轉載于:https://www.cnblogs.com/zemliu/p/3979701.html
總結
以上是生活随笔為你收集整理的Hessian 初探的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux常用命令(第二版) --压缩解
- 下一篇: 关于hbase的read操作的深入研究