Hessian是二进制的web service协议,官方网站提供Java、Flash/Flex、Python、C++、.NET C#等实现。Hessian和Axis、XFire都能实现web service方式的远程方法调用,区别是Hessian是二进制协议,Axis、XFire则是SOAP协议,因此从性能上说Hessian远优于后二者,而且Hessian的JAVA使用方法很是简单。Hessian因为没有WSDL这种服务描述文件去对实现进行规定,彷佛更适合内部分布式系统之间的交互,对外提供服务仍是使用后二者更体面些。hessian采用的是二进制RPC协议,由于采用了二进制协议,因此它很适合于发送二进制数据,Hessian主要做面向对象的消息通讯。Hessian的初衷就是支持动态类型,格式紧凑,跨语言Hessian是使用本身的序列化机制实现的编组和反编组,其支持的数据类型是有限制的,不支持复杂的对象,能够穿透防火墙,在这里不得不说一下RMI:RMI是一组用户开发分布式应用程序的API。他使用的是java序列化机制实现调用及返回值的编组于反编组。它使用Java语言接口定义了远程对象,它集合了Java序列化和Java远程方法协议(Java Remote Method Protocol)。他能够被看作是RPC的Java版本,由于传统的RPC并不能很好的应用于分布式对象系统。而Java RMI则支持存储于不一样地址空间的程序级对象之间彼此进行通讯,实现远程对象之间的无缝远程调用。他也有它的缺点,他只能经过RMI协议来进行访问没法经过HTTP协议访问,没法穿透防火墙。html
JAVA服务端使用步骤:
一、导入Hessian的Jar包
二、设计接口
三、实现接口:必须继承HessianServlet,接口参数对象必须实现序列化
四、配置web.xmljava
<servlet> <!-- 配置 HessianServlet,Servlet的名字随便配置,例如这里配置成ServiceServlet--> <servlet-name>ServiceServlet</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <!-- 配置接口的具体实现类 --> <init-param> <param-name>service-class</param-name> <param-value>com.hessian.service.impl.ServiceImpl</param-value> </init-param> </servlet> <!-- 映射 HessianServlet的访问URL地址--> <servlet-mapping> <servlet-name>ServiceServlet</servlet-name> <url-pattern>/ServiceServlet</url-pattern> </servlet-mapping>
下面来自http://blog.sina.com.cn/s/blog_7f73e06d0100xn9j.htmlweb
在实际应用中,咱们不仅是简单的只使用hessian来进行通讯的,若是方法多得话,还不如直接写在客户端来调用,然而:当hessian与spring结合后,大大减小了这些操做,将dao层的操做所有放在hessian服务端,将业务逻辑所有放在hessian客户端,这样的话咱们的hessian客户端和服务端彻底分离,所以咱们的业务逻辑和dao层就真正的达到了分离,就能够放在不一样的服务器上,固然hessian的通讯的做用不单单只有这些。
接口和实现和上边的同样:只是在web.xml中配置比较麻烦:
例子:
一、服务器端:增长remoting-servlet.xml配置文件:用来配置bean,并将bean导出为hessian服务:spring
<?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.kcpt.hessian.service.IHelloImpl"/> <!-- 使用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>
三、在客户端:
一样要加spring监听器和context-param指定bean的文件
声明bean的xml文件:app
<?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"> //hessian的地址和名称请求转发的名称 <value>http://127.0.0.1:8080/HessianService/remoting</value> </property> <property name="serviceInterface"> //hessian所要调用的接口 <value>com.kcpt.hessian.service.IHello</value> </property> </bean> </beans>
四、客户端的程序中要写:分布式
ApplicationContext context = new ClassPathXmlApplicationContext("com/kcpt/hessian/client/remoting-client.xml")
//这里只是你声明的bean的xml文件所在的路径 IHello b = (IHello) context.getBean("myServiceClient");
来获取到ihello这个接口,从而就可以调用这个接口里的方法进行操做性能