简介
Hessian是caucho公司开发的一种基于二进制RPC协议(Remote Procedure Call protocol)的轻量级远程调用框架。
在Java中使用Hessian:
服务器端:
- 包含Hessian.jar包
- 设计一个接口,用来给客户端调用
- 实现该接口的功能
- 配置web.xml,配好相应的servlet
- 由于使用二进制RPC协议传输数据,对象必须进行序列化,实现Serializable 接口
- 对于复杂对象可以使用Map的方法传递
客户端:
- 包含Hessian.jar包
- 具有和服务器端结构一样的接口。包括命名空间都最好一样
- 利用HessianProxyFactory调用远程接口
入门例子
到 http://hessian.caucho.com/ 下载hessian.jar包,我这里使用的是hessian-4.0.51.jar。
服务器端
- Eclipse新建一个Web工程,命名为HseeionService。将hessian.jar包放入WEB-INF/lib中,并引入之;
创建pojo类;
12345678910111213141516171819202122232425package app.demo;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = 153519254199840035L;String userName = "soopy";String password = "showme";public User(String user, String pwd){this.userName = user;this.password = pwd;}public String getUserName(){return userName;}public String getPassword(){return password;}}创建接口
1234567package app.demo;public interface BasicAPI {public void setGreeting(String greeting);public String hello();public User getUser();}实现接口
12345678910111213141516171819202122package app.demo;public class BasicService implements BasicAPI {private String _greeting = "Hello, world";@Overridepublic void setGreeting(String greeting) {_greeting = greeting;}@Overridepublic String hello() {return _greeting;}@Overridepublic User getUser() {return new User("prance", "meshow");}}配置web.xml
1234567891011121314151617181920<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"><servlet><servlet-name>hello</servlet-name><servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class><init-param><param-name>service-class</param-name><param-value>app.demo.BasicService</param-value></init-param></servlet><servlet-mapping><servlet-name>hello</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping></web-app>服务器测试test.jsp
123456789101112131415<%@ page import="com.caucho.hessian.client.HessianProxyFactory,app.demo.BasicAPI"%><%@page language="java"%><%HessianProxyFactory factory = new HessianProxyFactory();String url = ("http://" +request.getServerName() + ":" +request.getServerPort() +request.getContextPath() + "/hello");out.println(url);out.println("<br>");BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class,url);out.println("Hello: " + basic.hello());out.println("<br>");out.println("Hello: " + basic.getUser() .getUserName() );out.println("<br>");out.println("Hello: " +basic.getUser().getPassword() );%>将HessianService部署到Tomcat等服务器上,访问 http://localhost:8080/HessianService/test.jsp ,浏览器结果:
1234http://localhost:8080/HessianService/helloHello: Hello, worldHello: pranceHello: meshow
客户端
- 创建一个工程,命名为HessianClient,同样引入hessian.jar包;
- 创建和服务端一样的pojo类;
- 创建和服务端一样的接口;
创建客户端
1234567891011121314151617181920package app.demo;import java.net.MalformedURLException;import com.caucho.hessian.client.HessianProxyFactory;public class BasicClient {public static void main(String[] args) throws MalformedURLException {String url ="http://127.0.0.1:8080/HessianService/hello";HessianProxyFactory factory = new HessianProxyFactory();BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);System.out.println("Hello:" + basic.hello());System.out.println("Hello:" + basic.getUser().getUserName());System.out.println("Hello:" + basic.getUser().getPassword());basic.setGreeting("HelloGreeting");System.out.println("Hello:" + basic.hello());}}运行客户端代码,控制台结果:
1234Hello:Hello, worldHello:pranceHello:meshowHello:HelloGreeting
说明
这里服务端和客户端都编写了同样的接口和基础类,只是为了演示简单例子。在实际使用中,应该是服务端将自己的接口及所需类打成jar包给客户端引入调用。
Spring整合Hessian
服务端项目
新建一个Maven项目,编辑pom.xml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.tenny</groupId><artifactId>SpringHessianService</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>SpringHessianService Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.16.RELEASE</version></dependency><dependency><groupId>com.caucho</groupId><artifactId>hessian</artifactId><version>4.0.7</version></dependency></dependencies><build><finalName>SpringHessianService</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build></project>编辑web.xml
123456789101112131415161718192021222324<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><display-name>Spring Hessian Service</display-name><servlet><servlet-name>HessianServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:hessian-server.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>HessianServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>编辑hessian-server.xml
1234567891011121314151617181920<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.tenny" /><bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter"><property name="service" ref="helloImpl" /><property name="serviceInterface" value="com.tenny.IHello" /></bean></beans>接口代码
1234567891011package com.tenny;/*** @author tenny**/public interface IHello {public String hello(String name);}接口实现代码
12345678910111213141516171819package com.tenny;import org.springframework.stereotype.Service;/*** @author tenny**/@Servicepublic class HelloImpl implements IHello {@Overridepublic String hello(String name) {String result = "hello " + name;System.out.println("service result: " + result);return result;}}
客户端项目
新建Maven工程,编辑pom.xml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.tenny</groupId><artifactId>SpringHessianClient</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>SpringHessianClient Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.16.RELEASE</version></dependency><dependency><groupId>com.caucho</groupId><artifactId>hessian</artifactId><version>4.0.7</version></dependency></dependencies><build><finalName>SpringHessianClient</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build></project>编辑web.xml
123456789101112131415161718192021222324<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><display-name>Spring Hessian Client</display-name><servlet><servlet-name>HessianClient</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:hessian-client.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>HessianClient</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>编辑hessian-client.xml
12345678910111213141516171819202122<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><mvc:annotation-driven /><context:component-scan base-package="com.tenny" /><bean id="hello" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"><property name="serviceUrl" value="http://localhost:8080/SpringHessianService/hello"></property><property name="serviceInterface" value="com.tenny.IHello"></property></bean></beans>接口代码(实际项目中引入服务端的接口jar包)
1234567891011package com.tenny;/*** @author tenny**/public interface IHello {public String hello(String name);}Controller代码
1234567891011121314151617181920212223242526package com.tenny;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;/*** @author tenny**/@RestController@RequestMapping("/hello")public class HelloController {@Autowiredprivate IHello helloHess;@RequestMapping(value = "/sayHello", method=RequestMethod.GET)public String sayHello(){String result = helloHess.hello("tenny");System.out.println("client result: " + result);return result;}}
运行
分别启动服务端(端口为8080)和客户端(端口为8081)项目,浏览器地址访问 http://localhost:8081/SpringHessianClient/hello/sayHello 就能看到结果了。
其中页面显示hello tenny
,服务端控制台打印service result: hello tenny
,客户端控制台打印client result: hello tenny
。
附件
SpringHessianService项目下载:https://github.com/tenny-peng/SpringHessianService
SpringHessianClient项目下载:https://github.com/tenny-peng/SpringHessianClient