一、hessian简介
hessian是RPC框架的一种,即可远程调用方法,采用二进制序列化,使用http协议进行传输。
由于hessian是使用http协议传输,通常hessian服务是采用web项目的方式进行开发的。
二、hessian服务端开发步骤
1、添加maven依赖
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency>
2、创建服务,即是接口和相应的实现类
public interface HelloService { String sayHello(String name); }
public class HelloServiceImpl implements HelloService{ public String sayHello(String name) { return "hello,"+name; } }3、配置web.xml
主要在servlet中配置接口、实现类及访问地址,如:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>helloService</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>home-class</param-name> <param-value>service.impl.HelloServiceImpl</param-value> </init-param> <init-param> <param-name>home-api</param-name> <param-value>service.HelloService</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>helloService</servlet-name> <url-pattern>/helloService</url-pattern> </servlet-mapping> </web-app>这样,地址为http://localhost:8080/helloService的hessian服务便配置完成,启动web项目即可。
三、hessian客户端开发步骤
1、添加maven依赖
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency>2、添加hessian服务接口的jar包(本例就是含有HelloSerivce的包)
3、配置访问地址,并访问服务,如:
public class HessianClient { public static void main(String[] args) throws MalformedURLException { String url = "http://localhost:8080/helloService"; HessianProxyFactory factory = new HessianProxyFactory(); HelloService helloService = (HelloService)factory.create(HelloService.class,url); System.out.println(helloService.sayHello("apple")); } }输出结果:
hello,apple