hessian原理解析一(客户端分析)
hessian 是一款开源的二进制远程通讯协议,使用简单方法提供了RMI功能,主要用于面向对象的消息通信。 优点:跨平台、多语言支持、使用简单 缺点:传递复杂对象性能会下降,不适合安全性高的应用
一 、hessian demo 示例:
1、新建一个maven项目,包含3个模块 API 模块(远程接口,供客户端和服务端使用),客户端模块和服务端模块
2、添加hessian依赖包
在pom.xml 中添加hessian依赖
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
3、远程接口定义
在hessian-api 模块定义接口
public interface HelloHessian {
String sayHello(User user);
}
4、服务端接口实现
在hessian-server 模块实现API 接口
public class HelloHessianImpl implements HelloHessian {
public String sayHello(User user) {
String userName = user.getName();
System.out.println("hello:"+userName);
return userName;
}
}
5、服务端配置远程服务地址
hessian 是依赖web 容器 需在web.xml 中配置服务请求地址
<servlet>
<servlet-name>HelloHessian</servlet-name>
<!-- RPC HessianServlet处理类 -->
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<!-- 远程服务实现类 -->
<param-value>com.hessian.server.HelloHessianImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloHessian</servlet-name>
<url-pattern>/HelloHessian</url-pattern>
</servlet-mapping>
6、客户端远程接口调用
// 远程服务地址
String url = "http://localhost:8080/HelloHessian";
//通过hessian代理工程创建接口代理
HessianProxyFactory fatory = new HessianProxyFactory();
HelloHessian helloHessian = (HelloHessian)fatory .create(HelloHessian.class,url);
// 执行方法调用
helloHessian.sayHello(new User(10, "tiger"));
7、运行demo
7.1、启动服务器
7.2、运行客户端程序
控制台打印 hello:tiger
二、hessian 工作流程
三、客户端源码分析
1、客户端请求时序图
2、客户端创建接口的代理类分析:
HessianProxyFactory factory = new HessianProxyFactory();
HelloHessian helloHessian = (HelloHessian)factory.create(HelloHessian.class,url)
helloHessian.sayHello(new User(10, "tiger"))
2.1 通过HessianProxyFactory hessian代理工程类 创建代理类HessianProxy 具体代码如下:
public Object create(Class<?> api, URL url, ClassLoader loader) {
......
InvocationHandler handler = new HessianProxy(url, this, api);
return Proxy.newProxyInstance(loader, new Class[] { api, HessianRemoteObject.class }, handler);
}
2.2 HessianProxy 源码分析:
代理类HessianProxy 实现了InvocationHandler 接口 invoke(Object proxy, Method method, Object[] args)
2.2.1 获取调用的方法名以及方法参数类型
String methodName = method.getName();
2.2.2 equals、hashCode、getHessianType、getHessianURL、toString 本地调用
if(is1.equals("hashCode") && conn.length == 0) {
return new Integer(this._url.hashCode());
}
if(is1.equals("getHessianType")) {
return proxy.getClass().getInterfaces()[0].getName();
}
2.2.3 重载支持 重载处理 mangleName = sayHello_User // 方法名_参数1_参数2_XX
2.2.4 执行远程调用(建立http链接,设置http header 信息, 序列化方法和参数,执行http请求)
//建立http链接
URLConnection conn = this._factory.openConnection(this._url);
//设置http header 信息
this.addRequestHeaders(conn);
//序列化并输出流
AbstractHessianOutput out = this._factory.getHessianOutput(conn.getOutputStream());
out .call(mangleName , args) // out .call("sayHello",new Object[]{user})
out .flush();
call 方法封装hessian 版本信息、调用的方法名称、参数信息,格式如下
'c' 一个调用方法编码的开始标识符
1和0 Hessian的版本号,各占1 byte
length 方法名称的长度,占2 byte
method_name 方法名称
[params] 参数序列化内容,此参数是可选的
'z' 一个调用方法编码的结束标识符
eg:
User user = new User(10,"tiger");
FileOutputStream fos = new FileOutputStream("out.txt");
HessianOutput out = new HessianOutput(fos);
out.call("sayHello",new Object[]{user});
用BinaryViewer 工具打开 out.txt ,查看内容:此处为16进制数表示
63 01 00 6D 00 08 73 61 79 48 65 6C 6C 6F 4D 74
00 07 76 6F 2E 55 73 65 72 53 00 03 61 67 65 49
00 00 00 0A 53 00 04 6E 61 6D 65 53 00 05 74 69
67 65 72 7A 7A
63为'c'字符的编码
01 00为Hessian协议版本
6D为'm'字符编码
00 08 为函数名称的长度,因为'sayHello’函数的长度为8
73 61 79 48 65 6C 6C 6F 即为'sayHello'函数名称的字符编码;
7A为'z'的字符编码
2.2.5 获取输入流并反序列
is = conn.getInputStream();
AbstractHessianInput in = _factory.getHessianInput(is);
Object value = in.readObject(method.getReturnType())
三、服务端源码分析
我们在回头看看 web.xml 中 servlet 配置
<servlet>
<servlet-name>HelloHessian</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<param-value>com.hessian.server.HelloHessianImpl</param-value>
</init-param>
</servlet>
服务端HessianServlet 处理hessian 请求 ,HessianServlet 继承HttpServlet, 每个servlet有两个重要的方法 init 和 service
1、void init(ServletConfig config) 初始化远程服务实现类、远程服务接口类以及HessianSkeleton
a、初始化远程服务实现类, home-class 、service-class
if (getInitParameter("home-class") != null) {
String className = getInitParameter("home-class");
Class<?> homeClass = loadClass(className);
_homeImpl = homeClass.newInstance();
init(_homeImpl);
} else if (getInitParameter("service-class") != null) {
String className = getInitParameter("service-class");
Class<?> homeClass = loadClass(className);
_homeImpl = homeClass.newInstance(); init(_homeImpl);
}
b 、初始化远程服务接口
if (getInitParameter("home-api") != null) {
String className = getInitParameter("home-api");
_homeAPI = loadClass(className);
} else if (getInitParameter("api-class") != null) {
String className = getInitParameter("api-class");
_homeAPI = loadClass(className);
} else if (_homeImpl != null) {
_homeAPI = _homeImpl.getClass();
}
c 、初始化 HessianSkeleton
_homeSkeleton = new HessianSkeleton(_homeImpl, _homeAPI);
// 取服务类的方法
Method []methodList = apiClass.getMethods()
// 遍历 服务类的方法,放入_methodMap
for (int i = 0; i < methodList.length; i++) {
Method method = methodList[i];
Class []param = method.getParameterTypes();
String mangledName = method.getName() + "__" + param.length;
_methodMap.put(method.getName(), methodList[i]);
_methodMap.put(mangledName, methodList[i]);
_methodMap.put(mangleName(method, false), methodList[i]);
}
hessian 支持重载处理,关键点: 比如对于一个方法 public void sayHello(User user), 那么在_methodMap中会存放三个key sayHello sayHello_1, sayHello_User
2、service(ServletRequest request, ServletResponse response) 重写父类的service 方法
2.1、 检查请求方式是否为POST
if (! req.getMethod().equals("POST")) {
res.setStatus(500); // "Hessian Requires POST"
return;
}
2.2、获取输入输出流、对象的序列化工厂 执行目标目标服务方法
ServletInputStream e = request.getInputStream();
ServletOutputStream os = response.getOutputStream();
SerializerFactory serializerFactory = this.getSerializerFactory();
hessianSkeleton.invoke(is, os, serializerFactory)
hessianServlet 处理请求时序图
HessianSkeleton invoke 方法解析:
public void invoke(Object service,AbstractHessianInput in,AbstractHessianOutput out){
//读取方法调用名
String methodName = in.readMethod();
int argLength = in.readMethodArgLength();
Method method = this.getMethod(methodName + "_" + argLength);
//读取调用方法参数
Class[] args = method.getParameterTypes();
Object[] values = new Object[args.length];
for(int i= 0; i < args.length; ++i) {
values[i] = in.readObject(args[i]);
}
//执行目标方法调用
result = method.invoke(service, values)
//结果输出
out.writeReply(result);
out.close();
输出格式:
此处为16进制数表示
72 01 00 53 00 05 74 69 67 65 72 7A
r s t i g e r z
hessian原理解析一(客户端分析)的更多相关文章
- Hessian 原理分析
Hessian 原理分析 一.远程通讯协议的基本原理 网络通信需要做的就是将流从一台计算机传输到另外一台计算机,基于传输协议和网络 IO 来实现,其中传输协议比较出名的有 http . tcp . u ...
- Request 接收参数乱码原理解析三:实例分析
通过前面两篇<Request 接收参数乱码原理解析一:服务器端解码原理>和<Request 接收参数乱码原理解析二:浏览器端编码原理>,了解了服务器和浏览器编码解码的原理,接下 ...
- (转)Apache和Nginx运行原理解析
Apache和Nginx运行原理解析 原文:https://www.server110.com/nginx/201402/6543.html Web服务器 Web服务器也称为WWW(WORLD WID ...
- android黑科技系列——应用市场省流量更新(增量升级)原理解析
一.前言 最近在看热修复相关的框架,之前我们已经看过了阿里的Dexposed和AndFix这两个框架了,不了解的同学可以点击这里进行查看:Dexposed框架原理解析 和 AndFix热修复框架原理解 ...
- gpfdist原理解析
gpfdist原理解析 前言:gpfdist作为批量向postgresql写入数据的工具,了解其内部原理有助于正确使用以及提供更合适的数据同步方案.文章先简要介绍gpfdist的整体流程,然后针对重要 ...
- Request 接收参数乱码原理解析二:浏览器端编码原理
上一篇<Request 接收参数乱码原理解析一:服务器端解码原理>,分析了服务器端解码的过程,那么浏览器是根据什么编码的呢? 1. 浏览器解码 浏览器根据服务器页面响应Header中的“C ...
- Request 接收参数乱码原理解析一:服务器端解码原理
“Server.UrlDecode(Server.UrlEncode("北京")) == “北京””,先用UrlEncode编码然后用UrlDecode解码,这条语句永远为true ...
- 开源磁力搜索爬虫dhtspider原理解析
开源地址:https://github.com/callmelanmao/dhtspider. 开源的dht爬虫已经有很多了,有php版本的,python版本的和nodejs版本.经过一些测试,发现还 ...
- JSONP跨域的原理解析
JavaScript是一种在Web开发中经常使用的前端动态脚本技术.在JavaScript中,有一个很重要的安全性限制,被称为“Same- Origin Policy”(同源策略).这一策略对于Jav ...
随机推荐
- CodeForces 616C The Labyrinth
先预处理出所有连通块,对于每一个*,看他四周的连通块即可 #include<cstdio> #include<cstring> #include<queue> #i ...
- js删除最后一个字符串方法
JS 删除字符串最后一个字符的几种方法 2010-12-02 08:18:35| 分类: 编程 |举报 |字号 订阅 字符串:string s = "1,2,3,4,5," ...
- 毕业论文评审意见、导师意见范文、模板_Smile~风_百度空间
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- (简单) POJ 3159 Candies,Dijkstra+差分约束。
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
- C++异常(exception)第三篇-------如何释放资源(没有finally语句)
问题: C++中没有finally,那么应该在哪里关闭资源? C++的try{}catch(){},为什么没有finally{}块来释放资源? 如类中的一个方法,局部变量 File * file=NU ...
- 51nod1119(除法取模)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 题意:中文题诶- 思路:这题数据比较大直接暴力肯定是不 ...
- Victor 串口 VCL 控件 - 简单实用, 功能强大的 C++ Builder 串口控件!
源:Victor 串口 VCL 控件 - 简单实用, 功能强大的 C++ Builder 串口控件! 2014年02月06日发布控件的重要更新版本: Victor 串口控件 1.5.0.2 版本 (包 ...
- javascript 闭包理解
摘自:http://www.cnblogs.com/jkswjw/p/3180384.html javascript 闭包基础分享 闭包向来给包括JavaScript程序员在内的程序员以神秘,高深的感 ...
- linux pci 协议一
当当热卖商品推荐 先打下广告,上面是一本好书了O(∩_∩)O~ 前言 因为遇到一个pci总线的问题,所以去学习了解linux 的pci驱动,中间总结了一些pci总线原理和linux驱动的知识,在此总结 ...
- matlab find函数
find函数用来获取向量或矩阵中非0元素的索引 假设X是行向量,则find(X)返回的是一个行向量:X'为列向量,find(X')返回的是一个列向量. 看一个例子:A是一个行向量,B是一个列向量,将B ...