hessian学习

简介: hessian是一个采用二进制格式传输的服务框架,相对传统soap web service,更轻量,更快速。官网地址:http://hessian.caucho.com/ 目前已经支持N多语言,包括:java/c#/flex/php/ruby.

hessian是一个采用二进制格式传输的服务框架,相对传统soap web service,更轻量,更快速。官网地址:http://hessian.caucho.com/

目前已经支持N多语言,包括:java/c#/flex/php/ruby...

maven的依赖项如下:

1 <dependency>
2     <groupId>com.caucho</groupId>
3     <artifactId>hessian</artifactId>
4     <version>4.0.37</version>
5 </dependency>

入门示例:

一、服务端开发

1.1 先建服务接口

1 package yjmyzz.cnblogs.com.service;
2 
3 public interface HelloService {
4     
5     public String helloWorld(String message);
6 }

1.2 提供服务实现

 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.xml

 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包

最终服务是提供给客户端调用的,客户端必须知道服务的接口信息(包括接口方法中的传输dto定义),所以得将这些java文件导出成jar,提供给调用方。

方法很简单:eclipse中在接口package(包括dto对应的package)上右击,选择Export

再选择Jar File

 

二、客户端调用

同样先添加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 }

 

三、与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 }

示例源码地址:http://code.taobao.org/p/spring-mvc4-rest/src/

 

目录
相关文章
|
JSON 网络协议 开发工具
对已有的docker容器添加新的目录映射, 端口映射,环境变量,dns等
docker容器已经建立并运行, 需要在已有容器上添加新的目录映射,端口映射,环境变量等
3550 0
Vxworks 6.6系列下载地址
Vxworks 6.6系列下载地址: <br> ---------------------------------- <br><a target="_blank" href="ftp://ftp.windriver.speedera.net/ftp.windriver/internal/eval/vxworks-6.6/CDR-R133467.1-1-00.zip">ftp://ftp.
7397 0
|
Web App开发 数据可视化
如何轮播 DataV 大屏
如何轮播 DataV 大屏 当你使用 DataV 制作了足够多的大屏时,一定会冒出一个需求:轮流播放大屏页面,不要怕,一分钟就可以搞定 安装 Chrome 插件 TabCarousel 首先安装神器插件 TabCarousel 使用 安装完成之后,地址栏右侧会出现这么个小图标 。
19628 154
如何轮播 DataV 大屏
|
Web App开发 移动开发 定位技术
15款得心应手的思维导图软件
15款得心应手的思维导图软件
872 6
|
数据可视化 Docker Python
|
缓存 算法 NoSQL
京东物流面试真题(附参考答案)
京东物流面试真题(附参考答案)
985 1
|
弹性计算 固态存储 大数据
2024年阿里云服务器租用费用_阿里云云服务器ECS价格表(2核2G - 8核64G)
阿里云服务器分为云服务器ECS和轻量应用服务器,云服务器s6公网带宽可选1M到5M,系统盘40G起可选高效云盘、SSD云盘或ESSD云盘,阿里云服务器租用费用,轻量应用服务器和云服务器ECS优惠价格表,云服务器ECS经济型e实例2核2G、3M固定带宽99元一年、ECS u1实例2核4G、5M固定带宽、80G ESSD Entry盘优惠价格199元一年,轻量应用服务器2核2G3M带宽轻量服务器一年61元、2核4G4M带宽轻量服务器一年165元12个月、2核4G服务器30元3个月,幻兽帕鲁4核16G和8核32G服务器配置,云服务器ECS可以选择经济型e实例、通用算力u1实例、ECS计算型c7。
403 4
|
消息中间件 关系型数据库 MySQL
实时计算 Flink版产品使用合集之CDCPipelineConnectors支持哪些数据库的采集
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStreamAPI、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
搜索推荐
Stable Diffusion 人物发型提示词大全,中英文列表!
使用发型提示词能更精确描述所需图像的发型特征,如卷发、短发、颜色和风格。结合正负提示词,确保生成图片符合预期。尝试使用工具如[PromptChoose](https://promptchoose.com/)来创建个性化图像描述,包含多种发型选项,如刘海、马尾、波浪发型等,以增强图像细节和个性化。负面提示词防止不合适内容。利用提示词工具可提高生成图片的准确性和满足度。
|
机器学习/深度学习 人工智能 分布式计算
阿里云PAI:一站式AI研发平台,引领深度学习潮流
阿里云PAI:一站式AI研发平台,引领深度学习潮流 随着人工智能的飞速发展,深度学习框架已经成为AI研发的核心工具。然而,选择合适的深度学习框架并不容易,需要考虑的因素包括计算性能、易用性、支持的算法组件等多种因素。今天,我们就来介绍一款一站式AI研发平台——阿里云PAI,看看它如何解决这些痛点。
770 1

热门文章

最新文章