hessian是一個採用二進制格式傳輸的服務框架,相對傳統soap web service,更輕量,更快速。官網地址:http://hessian.caucho.com/php
目前已經支持N多語言,包括:java/c#/flex/php/ruby...java
maven的依賴項以下:web
1 <dependency> 2 <groupId>com.caucho</groupId> 3 <artifactId>hessian</artifactId> 4 <version>4.0.37</version> 5 </dependency>
入門示例:spring
1、服務端開發c#
1.1 先建服務接口api
1 package yjmyzz.cnblogs.com.service; 2 3 public interface HelloService { 4 5 public String helloWorld(String message); 6 }
1.2 提供服務實現spring-mvc
1 package yjmyzz.cnblogs.com.service.impl; 2 3 import yjmyzz.cnblogs.com.service.HelloService; 4 5 public class HelloServiceImpl implements HelloService { 6 7 @Override 8 public String helloWorld(String message) { 9 return "hello," + message; 10 } 11 12 }
1.3 修改web.xmltomcat
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>hessian-showcase</display-name> 7 8 <welcome-file-list> 9 <welcome-file>index.jsp</welcome-file> 10 </welcome-file-list> 11 12 <servlet> 13 <servlet-name>hessian-service</servlet-name> 14 15 <servlet-class> 16 com.caucho.hessian.server.HessianServlet 17 </servlet-class> 18 19 <init-param> 20 <param-name>home-class</param-name> 21 <param-value> 22 <!-- 服務實現類 --> 23 yjmyzz.cnblogs.com.service.impl.HelloServiceImpl 24 </param-value> 25 </init-param> 26 27 <init-param> 28 <param-name>home-api</param-name> 29 <!-- 服務接口 --> 30 <param-value>yjmyzz.cnblogs.com.service.HelloService</param-value> 31 </init-param> 32 33 </servlet> 34 35 <servlet-mapping> 36 <servlet-name>hessian-service</servlet-name> 37 <url-pattern>/hessian</url-pattern> 38 </servlet-mapping> 39 40 </web-app>
部署到tomcat或其它web容器中便可。
1.4 導出服務接口jar包ruby
最終服務是提供給客戶端調用的,客戶端必須知道服務的接口信息(包括接口方法中的傳輸dto定義),因此得將這些java文件導出成jar,提供給調用方。mvc
方法很簡單:eclipse中在接口package(包括dto對應的package)上右擊,選擇Export
再選擇Jar File
2、客戶端調用
一樣先添加maven的hessian依賴項,同時引入上一步導出的服務接口jar包,而後參考下面的示例代碼:
1 import java.net.MalformedURLException; 2 import org.junit.Test; 3 import yjmyzz.cnblogs.com.service.HelloService; 4 import com.caucho.hessian.client.HessianProxyFactory; 5 6 7 public class ServiceTest { 8 @Test 9 public void testService() throws MalformedURLException { 10 11 String url = "http://localhost:8080/hessian-showcase/hessian"; 12 System.out.println(url); 13 14 HessianProxyFactory factory = new HessianProxyFactory(); 15 HelloService helloService = (HelloService) factory.create(HelloService.class, url); 16 System.out.println(helloService.helloWorld("jimmy")); 17 18 } 19 }
3、與Spring的整合
spring-web包裏提供的org.springframework.remoting.caucho.HessianServiceExporter類,能夠將普通方法導出成hessian服務。關鍵是解決org.springframework.web.servlet.DispatcherServlet的url訪問路徑問題,通常狀況下,咱們是這樣配置的
1 <!-- spring mvc --> 2 <servlet> 3 <servlet-name>appServlet</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <init-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:servlet-context.xml</param-value> 8 </init-param> 9 <load-on-startup>1</load-on-startup> 10 <async-supported>true</async-supported> 11 </servlet> 12 13 <servlet-mapping> 14 <servlet-name>appServlet</servlet-name> 15 <url-pattern>/</url-pattern> 16 </servlet-mapping>
這是spring mvc的入口,攔截全部訪問路徑,能夠把這一節再複製一份,追加在後面,只不過url-pattern指定成特定的規則
1 <!-- spring mvc --> 2 <servlet> 3 <servlet-name>appServlet</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <init-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:servlet-context.xml</param-value> 8 </init-param> 9 <load-on-startup>1</load-on-startup> 10 <async-supported>true</async-supported> 11 </servlet> 12 13 <servlet-mapping> 14 <servlet-name>appServlet</servlet-name> 15 <url-pattern>/</url-pattern> 16 </servlet-mapping> 17 18 19 <!-- hessian --> 20 <servlet> 21 <servlet-name>hessianServlet</servlet-name> 22 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 23 <init-param> 24 <param-name>contextConfigLocation</param-name> 25 <param-value>classpath:hessian-context.xml</param-value> 26 </init-param> 27 <load-on-startup>1</load-on-startup> 28 </servlet> 29 30 <servlet-mapping> 31 <servlet-name>hessianServlet</servlet-name> 32 <url-pattern>/hessian/*</url-pattern> 33 </servlet-mapping>
這樣,全部以/hessian/開頭的訪問路徑,約定成hessian服務地址,詳細配置在hessian-context.xml中,內容以下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 8 9 10 <bean id="helloServiceImpl" class="com.cnblogs.yjmyzz.service.hessian.support.HelloServiceImpl" /> 11 12 <!-- 使用HessianServiceExporter 將普通bean導出成Hessian服務 --> 13 <bean name="/service" 14 class="org.springframework.remoting.caucho.HessianServiceExporter"> 15 <property name="service" ref="helloServiceImpl" /> 16 <!-- Hessian服務的接口 --> 17 <property name="serviceInterface" value="com.cnblogs.yjmyzz.service.hessian.HelloService" /> 18 </bean> 19 20 </beans>
這樣,就能直接以http://localhost:8080/spring-mvc4-rest/hessian/service 發佈hessian服務了
再來看看客戶端如何整合,相似的,咱們須要一個配置文件,好比:hessian-client.xml,內容以下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context 7 http://www.springframework.org/schema/context/spring-context.xsd"> 8 9 <bean id="hessianClient" 10 class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> 11 <property name="serviceUrl"> 12 <value>http://localhost:8080/spring-mvc4-rest/hessian/service</value> 13 </property> 14 <property name="serviceInterface"> 15 <value>com.cnblogs.yjmyzz.service.hessian.HelloService</value> 16 </property> 17 </bean> 18 19 </beans>
調用示例:
1 package com.cnblogs.yjmyzz.test; 2 import java.net.MalformedURLException; 3 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import com.cnblogs.yjmyzz.service.hessian.HelloService; 9 10 public class HessianServiceTest { 11 @SuppressWarnings("resource") 12 @Test 13 public void testService() throws MalformedURLException { 14 ApplicationContext context = new ClassPathXmlApplicationContext( 15 "hessian-client.xml"); 16 HelloService hello = (HelloService) context.getBean("hessianClient"); 17 System.out.println(hello.helloWorld("jimmy.yang")); 18 } 19 }