hessian是一个采用二进制格式传输的服务框架,相对传统soap web service,更轻量,更快速。官网地址:http://hessian.caucho.com/
目前已经支持N多语言,包括:java/c#/flex/php/ruby...
maven的依赖项如下:
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.37</version>
</dependency>
入门示例:
一、服务端开发
1.1 先建服务接口
package yjmyzz.cnblogs.com.service; public interface HelloService { public String helloWorld(String message);
}
1.2 提供服务实现
package yjmyzz.cnblogs.com.service.impl; import yjmyzz.cnblogs.com.service.HelloService; public class HelloServiceImpl implements HelloService { @Override
public String helloWorld(String message) {
return "hello," + message;
} }
1.3 修改web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>hessian-showcase</display-name> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>hessian-service</servlet-name> <servlet-class>
com.caucho.hessian.server.HessianServlet
</servlet-class> <init-param>
<param-name>home-class</param-name>
<param-value>
<!-- 服务实现类 -->
yjmyzz.cnblogs.com.service.impl.HelloServiceImpl
</param-value>
</init-param> <init-param>
<param-name>home-api</param-name>
<!-- 服务接口 -->
<param-value>yjmyzz.cnblogs.com.service.HelloService</param-value>
</init-param> </servlet> <servlet-mapping>
<servlet-name>hessian-service</servlet-name>
<url-pattern>/hessian</url-pattern>
</servlet-mapping> </web-app>
部署到tomcat或其它web容器中即可。
1.4 导出服务接口jar包
最终服务是提供给客户端调用的,客户端必须知道服务的接口信息(包括接口方法中的传输dto定义),所以得将这些java文件导出成jar,提供给调用方。
方法很简单:eclipse中在接口package(包括dto对应的package)上右击,选择Export
再选择Jar File
二、客户端调用
同样先添加maven的hessian依赖项,同时引入上一步导出的服务接口jar包,然后参考下面的示例代码:
import java.net.MalformedURLException;
import org.junit.Test;
import yjmyzz.cnblogs.com.service.HelloService;
import com.caucho.hessian.client.HessianProxyFactory; public class ServiceTest {
@Test
public void testService() throws MalformedURLException { String url = "http://localhost:8080/hessian-showcase/hessian";
System.out.println(url); HessianProxyFactory factory = new HessianProxyFactory();
HelloService helloService = (HelloService) factory.create(HelloService.class, url);
System.out.println(helloService.helloWorld("jimmy")); }
}
三、与Spring的整合
spring-web包里提供的org.springframework.remoting.caucho.HessianServiceExporter类,可以将普通方法导出成hessian服务。关键是解决org.springframework.web.servlet.DispatcherServlet的url访问路径问题,一般情况下,我们是这样配置的
<!-- spring mvc -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet> <servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这是spring mvc的入口,拦截所有访问路径,可以把这一节再复制一份,追加在后面,只不过url-pattern指定成特定的规则
<!-- spring mvc -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet> <servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- hessian -->
<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-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>hessianServlet</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
这样,所有以/hessian/开头的访问路径,约定成hessian服务地址,详细配置在hessian-context.xml中,内容如下:
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="helloServiceImpl" class="com.cnblogs.yjmyzz.service.hessian.support.HelloServiceImpl" /> <!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 -->
<bean name="/service"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="helloServiceImpl" />
<!-- Hessian服务的接口 -->
<property name="serviceInterface" value="com.cnblogs.yjmyzz.service.hessian.HelloService" />
</bean> </beans>
这样,就能直接以http://localhost:8080/spring-mvc4-rest/hessian/service 发布hessian服务了
再来看看客户端如何整合,类似的,我们需要一个配置文件,比如:hessian-client.xml,内容如下:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="hessianClient"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:8080/spring-mvc4-rest/hessian/service</value>
</property>
<property name="serviceInterface">
<value>com.cnblogs.yjmyzz.service.hessian.HelloService</value>
</property>
</bean> </beans>
调用示例:
package com.cnblogs.yjmyzz.test;
import java.net.MalformedURLException; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cnblogs.yjmyzz.service.hessian.HelloService; public class HessianServiceTest {
@SuppressWarnings("resource")
@Test
public void testService() throws MalformedURLException {
ApplicationContext context = new ClassPathXmlApplicationContext(
"hessian-client.xml");
HelloService hello = (HelloService) context.getBean("hessianClient");
System.out.println(hello.helloWorld("jimmy.yang"));
}
}