因为在项目中需要在dubbo的消费者和生产者之间传递文件,目前使用的是字节数组作为参数的形式,但是看到官网提供的文档说Hessian协议适合传递文件,所以自己做了一个例子,测试后是可以正常运行的。下面是详细代码:(我是通过tomcat发布的服务)

一、1、消费方和服务方都要依赖的API

1 packagecom.isoftstone.iics.email.services.send.common.test;2

3 importjava.io.InputStream;4

5 public interfaceHessianTestService {6

7 publicString sayHello(String message);8

9 publicString upload(String filename, InputStream is);10

11 }

2、下面是需要依赖的jar包

使用dobbo需要的jar和外部API

com.caucho

hessian

4.0.7

使用hessian协议还需要的依赖hessian包

3、因为Hessian协议底层使用的是HTTP,所以需要修改一下web.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2 "http://java.sun.com/dtd/web-app_2_3.dtd">

4

5 hessian

6

7

8

9 org.springframework.web.context.ContextLoaderListener

10

11

12 contextConfigLocation

13 classpath*:spring_echannel_dependence.xml

14

15

16

17 dubbo

18 com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet

19

20

21 dubbo

22 /*

23

24

25

4、下面是一些Spring的配置文件和Spring的生产者配置文件

spring_echannel_dependence.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"

4 xsi:schemaLocation="5 http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd7 http://www.springframework.org/schema/aop8 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd9 http://www.springframework.org/schema/context10 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

11

12

13

14

15

16

17

spring_dubbo_provider.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

5 xsi:schemaLocation="6 http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd8 http://www.springframework.org/schema/aop9 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd10 http://www.springframework.org/schema/context11 http://www.springframework.org/schema/context/spring-context-3.0.xsd12 http://code.alibabatech.com/schema/dubbo13 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

14

15

16

17

18

19

20

hessian协议的端口和服务的端口相同,server使用的是servlet,contextpath使用的项目名称(dubbo文档指出应该是servlet的上下文),如果去掉的话,在测试的时候会找不到远程方法

5、下面是接口的实现类

1 packagecom.yj.fenghao.hessian;2

3 importjava.io.File;4 importjava.io.FileNotFoundException;5 importjava.io.FileOutputStream;6 importjava.io.IOException;7 importjava.io.InputStream;8

9 importorg.apache.commons.io.IOUtils;10 importorg.springframework.stereotype.Component;11

12

13

14

15

16

17

18 importcom.isoftstone.iics.email.services.send.common.test.HessianTestService;19

20 @Component("HessianTest")21 public class HessianRFCServiceImpl implementsHessianTestService{22

23 publicString sayHello(String message) {24 System.out.println("\n message is "+message);25 return "SUCESS";26 }27

28 publicString upload(String filename, InputStream file ) {29 FileOutputStream fos=null;30 try{31 fos=new FileOutputStream(new File("f:/"+filename));32 IOUtils.copy(file, fos);33 } catch(FileNotFoundException e) {34 e.printStackTrace();35 return "Failure";36 } catch(IOException e) {37 e.printStackTrace();38 return "Failure";39 }finally{40 if(fos!=null){41 IOUtils.closeQuietly(fos);42 }43 if(file!=null){44 IOUtils.closeQuietly(file);45 }46 }47 return "SUCESS";48 }49

50 }

6、因为使用的是maven所以在贴一下pom.xml文件

pom.xml

1

2 4.0.0

3 hessian

4 hessian-test

5 0.0.1-SNAPSHOT

6 war

7

8

9 3.0.6.RELEASE

10

11

12

13 org.springframework

14 spring-core

15 ${spring.release}

16

17

18 org.springframework

19 spring-web

20 ${spring.release}

21

22

23 org.springframework

24 spring-beans

25 ${spring.release}

26

27

28 org.springframework

29 spring-context

30 ${spring.release}

31

32

33 org.springframework

34 spring-aop

35 ${spring.release}

36

37

38 org.springframework

39 spring-context-support

40 ${spring.release}

41

42

43 org.springframework

44 spring-aspects

45 ${spring.release}

46

47

48 org.apache.commons

49 commons-io

50 1.3.2

51

52

53 log4j

54 log4j

55 1.2.17

56

57

58 junit

59 junit

60 4.12

61

62

63 com.caucho

64 hessian

65 4.0.7

66

67

68 org.codehaus.castor

69 castor-core

70 1.3.3

71

72

73 org.codehaus.castor

74 castor-xml

75 1.3.3

76

77

78

79

有一些冗余的包

现在启动Tomcat,一个可以使用hessian协议的生产者就注册好了

二 、下面是对服务的测试,我是在同一个工程中写的src/test/java和src/test/resource中写的测试类和添加的配置

spring_dubbo_consumer.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

5 xsi:schemaLocation="6 http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd8 http://www.springframework.org/schema/aop9 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd10 http://www.springframework.org/schema/context11 http://www.springframework.org/schema/context/spring-context-3.0.xsd12 http://code.alibabatech.com/schema/dubbo13 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

14

15

16

17

18

测试类:

1 packagecom.yj.fenghao.hessiantest;2

3 importjava.io.FileInputStream;4 importjava.io.FileNotFoundException;5

6 importorg.junit.Ignore;7 importorg.springframework.context.support.ClassPathXmlApplicationContext;8

9 importcom.isoftstone.iics.email.services.send.common.test.HessianTestService;10

11 public classHessianTest {12

13 ClassPathXmlApplicationContext context;14 publicHessianTest(){15 context=new ClassPathXmlApplicationContext(newString[]{16 "classpath*:spring_dubbo_consumer.xml"

17 });18 }19

20 public static final HessianTest test=newHessianTest();21

22 @Ignore23 @org.junit.Test24 public voidTest(){25 HessianTestService hessian = (HessianTestService)test.context.getBean("hessian");26 String sayHello = hessian.sayHello("ni hao test is pass");27 System.out.println("\nresult is "+sayHello);28 }29

30 @org.junit.Test31 public voidTestIO(){32 HessianTestService hessian = (HessianTestService)test.context.getBean("hessian");33 try{34 String result = hessian.upload("1234.pdf", new FileInputStream("d:/1234.pdf"));35 System.out.println("\nresult is "+result);36 } catch(FileNotFoundException e) {37 e.printStackTrace();38 }39 }40

41 }

结束语:看来做事总要换个思路,以前做这个,刚刚开始总想做成直连的,总是不成功,后来使用zooker注册中心,直接就成功了!

java hessian 协议_dubbo的Hessian协议的使用相关推荐

  1. java dubbo协议_dubbo协议端口

    dubbo框架一共支持9种协议.其中,默认的,也是最常用的,那就是dubbo协议. 既然是协议,那么,就需要有端口. 所以,在使用dubbo协议暴露服务时,需要配置dubbo端口. dubbo协议的默 ...

  2. Java学习---RMI 技术分析[Hessian]

    一.什么是Hessian Hessian 是一个基于 binary-RPC 实现的远程通讯 library.使用二进制传输数据.Hessian通常通过Web应用来提供服务,通过接口暴露.Servlet ...

  3. dubbo provider异步_Dubbo支持什么协议?与SpringCould相比它为什么效率要高一些?

    推荐学习 消息中间件合集:MQ(ActiveMQ/RabbitMQ/RocketMQ)+Kafka+笔记 肝了30天,整出这份[分布式宝典:限流+缓存+通讯],秋招跳槽有望 一箭双雕!Alibaba架 ...

  4. dubbo协议_Dubbo框架支持多少种协议?各有什么特点?文中一一为你揭晓

    概述 Dubbo 允许配置多协议,在不同服务上支持不同协议或者同一服务上同时支持多种协议. 除了dubbo协议外, Dubbo框架还支持另外8种服务暴露协议,如rmi协议. hessian协议. ht ...

  5. Java 面试知识点解析(五)——网络协议篇

    前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 Java 知识点进行复习和学习一番,大 ...

  6. java调用easyxml接口_【技术教程】如何通过Java程序调用RTSP拉流协议视频平台EasyNVR程序接口?...

    原标题:[技术教程]如何通过Java程序调用RTSP拉流协议视频平台EasyNVR程序接口? RTSP协议视频平台EasyNVR经过多年的积累,已经是一套成熟且完善的视频平台了,用户可以通过网页直接访 ...

  7. 对Java的URL类支持的协议进行扩展的方法

    转载自   对Java的URL类支持的协议进行扩展的方法 JAVA默认提供了对file,ftp,gopher,http,https,jar,mailto,netdoc协议的支持.当我们要利用这些协议来 ...

  8. Java基于socket服务实现UDP协议的方法

    转载自 Java基于socket服务实现UDP协议的方法 这篇文章主要介绍了Java基于socket服务实现UDP协议的方法,通过两个简单实例分析了java通过socket实现UDP发送与接收的技巧, ...

  9. dubbo协议_Dubbo协议解析与OPPO自研ESA RPC框架实践

    本文来自OPPO互联网基础技术团队,转载请注名作者.同时欢迎关注我们的公众号:OPPO_tech,与你分享OPPO前沿互联网技术及活动. 1. 背景 Dubbo是一款高性能.轻量级的开源Java RP ...

最新文章

  1. Pyinstall打包多个python脚本,包含DLL文件
  2. 批量put和单条put
  3. 全国计算机等级考试题库二级C操作题100套(第92套)
  4. linux block设备,Linux I/O Block--块设备的表示
  5. Frameworks.Entity.Core 1
  6. 《Linux编程》学习笔记 ·004【文件I/O操作】
  7. 20211027:《Labuladong的算法小抄》学习记录(一)
  8. 创建一个cocos2d-x工程添加一个自定义Scene并显示
  9. 转:: 刺鸟:用python来开发webgame服务端(1)
  10. 苹果手机一直显示搜索服务器,苹果手机safari浏览器搜索页面没有了
  11. 如何将ape无损音转码为wav文件
  12. 两台电脑之间实现串口通信
  13. 高等数学:第三章 微分中值定理与导数的应用(3)泰勒公式
  14. 用matlab实现女声变男声步骤,男变女声、女变男声、如何实现变声效果?
  15. linux下批量转换32bit wav为16bit
  16. mysql基础之多表练习题
  17. gcc for arm 工具链使用(一)
  18. css3斜切加颜色,CSS斜切角
  19. 慕容垂:百万战骨风云里——激荡的鲜卑史略之三(转载)
  20. 午夜00:37分,与蚊子相伴无眠之夜

热门文章

  1. 数字服务器及tms系统,TMS影院管理系统坐阵影院市场
  2. 45度做人,90度做事,180度为人,360度处世
  3. 三国志战略版:Daniel_S7赤壁之战前瞻2_细节爆料
  4. 离子液体[EMIm][PF6],[HMIm][PF6],[C14MIm][PF6]修饰纳米Fe3O4,TiO2和SiO2(离子液体修饰物)
  5. 机械故障诊断02-基于HHT的轴承振动分析
  6. java备忘录源码下载_备忘录/java - WEB源码|JSP源码/Java|源代码 - 源码中国
  7. 初次使用Rubymine
  8. FME对Autodesk AutoCAD Civil 3D的支持
  9. MeeGo 程序开发人员乐园
  10. element-ui 表格第一列随滚动自动吸顶