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

一,Hessian基础概念java

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

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

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

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

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

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

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

二,代码实现spa

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

Creating a Hessian service using Java has four steps:

一、Create an Java interface as the public API
二、Create a client using HessianProxyFactory
三、Create the Service implementation class
四、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(即提供接口类)

相关文章
相关标签/搜索