您的位置:首页 > 其它

Hessian--轻量级远程调用方案

2017-06-07 00:00 411 查看
摘要: 苦于webservice繁琐的创建,注解,发布流程,使用Hessian作为远程调用替代方案.

一,Hessian基础概念

Hessian 是由 caucho 提供的一个基于 binary-RPC 实现的远程通讯 library.Binary-RPC 是一种和 RMI 类似的远程调用的协议,它和 RMI 的不同之处在于它以标准的二进制格式来定义请求的信息 ( 请求的对象、方法、参数等 ) ,这样的好处就是在跨语言通讯的时候也可以使用。

Binary -RPC 协议的一次远程通信过程:

1 、客户端发起请求,按照 Binary -RPC 协议将请求信息进行填充;

2 、填充完毕后将二进制格式文件转化为流,通过传输协议进行传输;

3 、接收到在接收到流后转换为二进制格式文件,按照 Binary -RPC 协议获取请求的信息并进行处理;

4 、处理完毕后将结果按照 Binary -RPC 协议写入二进制格式文件中并返回

总结:hessian是一个轻量级的remoting onhttp(基于http传输协议)工具,使用简单的方法提供了RMI的功能

二,代码实现

参照官网(http://hessian.caucho.com/#IntroductiontoHessian)上的介绍,一个Hessian的服务创建有以下几个步骤.

Creating a Hessian service using Java has four steps:

1、Create an Java interface as the public API
2、Create a client using HessianProxyFactory
3、Create the Service implementation class
4、Configure the service in your servlet engine.

首先下载hessian-4.0.37.jar,服务端和客户端都要用的。

<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>3.1.5</version>
</dependency>

版本根据需求自行更改

server端:

package com.chuyu.demo;

public interface IBasic {
public  String sayHello();
}

package com.chuyu.demo;

public class BasicImpl  implements  IBasic {
private String helloStr = "Hello, world";
@Override
public String sayHello() {
return helloStr;
}
}

web.xml中添加servlet:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>

<servlet>
<servlet-name>hello-hessian</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>com.chuyu.demo.BasicImpl</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>com.chuyu.demo.IBasic</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hello-hessian</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

在web.xml里,BasicService是通过home-class和home-api两个参数传递给HessianServlet,然后将HessianServlet配置到web.xml的<servlet>里来实现服务配置到容器的。

客户端:

package com.chuyu.demo;

public interface IBasic {
public  String sayHello();
}

调用类:

package com.chuyu.demo;

import com.caucho.hessian.client.HessianProxy;
import com.caucho.hessian.client.HessianProxyFactory;
import java.net.MalformedURLException;

public class HessianClient {
public static void main(String[] args) {
String uslStr="http://localhost:8080/hello";
//         String hessian_url="http://localhost:8080//demo-hessian";
HessianProxyFactory factory=new HessianProxyFactory();
try {
IBasic iBasic =(IBasic)factory.create(IBasic.class,uslStr);
//            IBasic iBasic_hessian =(IBasic)factory.create(IBasic.class,hessian_url);
System.out.println(iBasic.sayHello());
//            System.out.println(iBasic_hessian.sayHello());
} catch (MalformedURLException e) {
e.printStackTrace();
}

}

}

通常情况下,编写客户端程序需要依赖服务器提供的客户端jar(即提供接口类)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hessian