Java学习之Hessian通讯基础

1、首先先说Hessian是什么?

hessian是一个轻量级的remoting on http工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简单、快捷。
采用的是二进制RPC协议,由于采用了二进制协议,因此它很适合于发送二进制数据,Hessian主要做面向对象的消息通讯。
Hessian的初衷就是支持动态类型,格式紧凑,跨语言Hessian是使用本身的序列化机制实现的编组和反编组,其支持的数据类型是有限制的,不支持复杂的对象,能够穿透防火墙。
RMI是一组用户开发分布式应用程序的API,使用的是java序列化机制实现调用及返回值的编组与反编组。它使用Java语言接口定义了远程对象,它集合了Java序列化和Java远程方法协议(Java Remote Method Protocol)。他能够被看作是RPC的Java版本,由于传统的RPC并不能很好的应用于分布式对象系统。而Java RMI则支持存储于不一样地址空间的程序级对象之间彼此进行通讯,实现远程对象之间的无缝远程调用。可是它也有缺点,RMI只能经过RMI协议来进行访问,没法经过HTTP协议访问,没法穿透防火墙。
还有一种远程调用方法就是HttpInvoker:它也是将参数和返回值经过Java的序列化机制进行编组和反编组,它具备RMI支持的可序列化对象的优势。试使用Http协议传输二进制流的,同时又具备Hessian、Burlap(传输xml文本)的优势。java

2、写一个简单的hessian通讯

写一个Hessian须要注意的问题:web

一、Java服务器端必须具有如下几点:spring

  • 包含Hessian的jar包
  • 设计一个接口,用来给客户端调用
  • 实现该接口的动能
  • 配置web.xml,配置相应的servlet
  • 对象必须实现Serializable接口
  • 复杂对象可使用Map进行传递

二、客户端必须具有如下几点:api

  • java客户端包含Hessian.jar
  • 具备和服务器端结构同样的接口和实体类,包括命名空间。
  • 利用HessianProxyFactory调用远程接口

3、简单hessian实例:

一、在服务端的接口:服务器

package com.test.hession;

public interface IHello {
    String sayHello();
}

二、在服务端的实现类:app

package com.test.hession.service;

import com.caucho.hessian.server.HessianServlet;
import com.test.hession.IHello;

public class IHelloImpl extends HessianServlet implements IHello {
    public String sayHello() {
        return "Hello,I from HessianService";
    }

}

三、在web.xml中进行配置:分布式

<servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
    <init-param>
      <param-name>home-class</param-name>
      <param-value>com.test.hession.service.impl.IHelloImpl</param-value>
    </init-param>
    <init-param>
      <param-name>home-api</param-name>
      <param-value>com.test.hession.service.IHello</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/Hello</url-pattern>
  </servlet-mapping>

四、在客户端的类:工具

package com.test.hession.client;

import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;
import com.test.hession.IHello;

public class ClientTest {
    public static String url = "http://127.0.0.1:8080/HessianService/Hello";
    public static void main(String[] args){
        HessianProxyFactory factory = new HessianProxyFactory();
        try {
            IHello iHello = (IHello) factory.create(IHello.class, url);
            System.out.println(iHello.sayHello());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

五、先将服务器端的类link到客户端,或者是将服务器端打包放到客户端,并运行ClientTest,结果以下:url

Hello,I from HessianService

4、hessian与spring结合

在实际应用中,咱们不是只是简单地只使用hessian来进行通讯的,若是方法多得话,还不如直接写在客户端来调用,然而当hessian与spring结合后,大大减小了这些操做,将dao层的操做所有放在hessian服务端,将业务逻辑所有放在hessian客户端,这样的话咱们的hessian客户端和服务端彻底分离,所以咱们的业务逻辑和dao层就真正的达到了分离,就能够放在不一样的服务器上,固然hessian通讯的做用不只仅只有这些。
接口和实现和上边的同样,只是在web.xml中配置比较麻烦。spa

一、服务器端

增长remoting-servlet.xml配置文件用来配置bean,并将bean导出为hessian服务:

<?xml version = "1.0" encoding = "UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <!-- 定义普通的bean实例 -->
    <bean id="Hello" class="com.test.hession.service.impl" />
    <!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 -->
    <bean name="/remoting"
        class="org.springframework.remoting.caucho.HessianServiceExporter">
        <!-- 须要导出的目标bean -->
        <property name="service" ref="Hello" />
        <!-- Hessian服务的接口 -->
        <property name="serviceInterface" value="com.kcpt.hessian.service.IHello" />
    </bean>
</beans>

二、web.xml文件的配置

spring的监听器

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <!--添加监听器 -->
    </listener>
    <!-- 指定spring的配置文件在哪里,在这个配置文件中导出了Hessian服务 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/remoting-servlet.xml</param-value>
    </context-param>
    <!-- Hessian经过Servlet提供远程服务,须要将某个匹配的模式映射到hessian服务中,spring的dispatcherServlet能完成此功能,
    DispatcherServlet可将匹配模式的请求转发到Hessian服务,web.xml只是定义了“请求转发器”,该转发器将匹配/remoting/*的请求截获,转发给context的bean处理。
    而HessianServiceExporter提供bean服务。
    -->
    <servlet>
        <servlet-name>remoting</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>remoting</servlet-name>
        <url-pattern>/remoting/*</url-pattern>
    </servlet-mapping>

三、在客户端

<?xml version = "1.0" encoding = "UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    <bean id="myServiceClient"
        class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://127.0.0.1:8080/HessianService/remoting2</value>
        </property>
        <property name="serviceInterface">
            <value>com.test.hession.service.IHello</value>
        </property>
    </bean>
</beans>

四、客户端的程序

package com.test.App;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.hession.service.IHello;


public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Application.xml");  //这里只是你声明的bean的xml文件所在的路径
        IHello b = (IHello) context.getBean("myServiceClient");
        System.out.println(b.sayHello());
    }
}

来获取到IHello这个接口,从而就可以调用这个接口里的方法进行操做

做者:世界百科 连接:http://www.jianshu.com/p/5e8a7148dd68 來源:简书 著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。

相关文章
相关标签/搜索