java hessian 协议_dubbo的Hessian协议的使用

因为在项目中需要在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包

66433e88435633adee7fb5f35c172c65.png

使用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注册中心,直接就成功了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值