因为在项目中需要在dubbo的消费者和生产者之间传递文件,目前使用的是字节数组作为参数的形式,但是看到官网提供的文档说Hessian协议适合传递文件,所以自己做了一个例子,测试后是可以正常运行的。下面是详细代码:(我是通过tomcat发布的服务)
一、1、消费方和服务方都要依赖的API
1 package com.isoftstone.iics.email.services.send.common.test; 2 3 import java.io.InputStream; 4 5 public interface HessianTestService { 6 7 public String sayHello(String message); 8 9 public String upload(String filename, InputStream is); 10 11 }
2、下面是需要依赖的jar包
使用dobbo需要的jar和外部API
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
使用hessian协议还需要的依赖hessian包
3、因为Hessian协议底层使用的是HTTP,所以需要修改一下web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 <web-app> 5 <display-name>hessian</display-name> 6 7 <!-- 监听spring上下文的加载 --> 8 <listener> 9 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 10 </listener> 11 <context-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath*:spring_echannel_dependence.xml</param-value> 14 </context-param> 15 16 <servlet> 17 <servlet-name>dubbo</servlet-name> 18 <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class> 19 </servlet> 20 <servlet-mapping> 21 <servlet-name>dubbo</servlet-name> 22 <url-pattern>/*</url-pattern> 23 </servlet-mapping> 24 25 </web-app>
4、下面是一些Spring的配置文件和Spring的生产者配置文件
spring_echannel_dependence.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 11 12 <context:component-scan base-package="com.yj.fenghao" annotation-config="true" use-default-filters="true"></context:component-scan> 13 14 <import resource="spring_dubbo_provider.xml"/> 15 16 17 </beans>
spring_dubbo_provider.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/aop 9 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context-3.0.xsd 12 http://code.alibabatech.com/schema/dubbo 13 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 14 15 <dubbo:application name="dubbo-hessian-provider"></dubbo:application> 16 <dubbo:protocol name="dubbo" port="20880" ></dubbo:protocol> 17 <dubbo:registry address="zookeeper://*****:2281" check="false"></dubbo:registry> 18 <dubbo:protocol name="hessian" port="8084" server="servlet" contextpath="hessian-test"></dubbo:protocol> 19 <dubbo:service protocol="hessian" interface="com.isoftstone.iics.email.services.send.common.test.HessianTestService" ref="HessianTest" timeout="100000" ></dubbo:service> 20 </beans>
hessian协议的端口和服务的端口相同,server使用的是servlet,contextpath使用的项目名称(dubbo文档指出应该是servlet的上下文),如果去掉的话,在测试的时候会找不到远程方法
5、下面是接口的实现类
1 package com.yj.fenghao.hessian; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 9 import org.apache.commons.io.IOUtils; 10 import org.springframework.stereotype.Component; 11 12 13 14 15 16 17 18 import com.isoftstone.iics.email.services.send.common.test.HessianTestService; 19 20 @Component("HessianTest") 21 public class HessianRFCServiceImpl implements HessianTestService{ 22 23 public String sayHello(String message) { 24 System.out.println(" message is "+message); 25 return "SUCESS"; 26 } 27 28 public String 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 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>hessian</groupId> 4 <artifactId>hessian-test</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 8 <properties> 9 <spring.release>3.0.6.RELEASE</spring.release> 10 </properties> 11 <dependencies> 12 <dependency> 13 <groupId>org.springframework</groupId> 14 <artifactId>spring-core</artifactId> 15 <version>${spring.release}</version> 16 </dependency> 17 <dependency> 18 <groupId>org.springframework</groupId> 19 <artifactId>spring-web</artifactId> 20 <version>${spring.release}</version> 21 </dependency> 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-beans</artifactId> 25 <version>${spring.release}</version> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>spring-context</artifactId> 30 <version>${spring.release}</version> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-aop</artifactId> 35 <version>${spring.release}</version> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework</groupId> 39 <artifactId>spring-context-support</artifactId> 40 <version>${spring.release}</version> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-aspects</artifactId> 45 <version>${spring.release}</version> 46 </dependency> 47 <dependency> 48 <groupId>org.apache.commons</groupId> 49 <artifactId>commons-io</artifactId> 50 <version>1.3.2</version> 51 </dependency> 52 <dependency> 53 <groupId>log4j</groupId> 54 <artifactId>log4j</artifactId> 55 <version>1.2.17</version> 56 </dependency> 57 <dependency> 58 <groupId>junit</groupId> 59 <artifactId>junit</artifactId> 60 <version>4.12</version> 61 </dependency> 62 <dependency> 63 <groupId>com.caucho</groupId> 64 <artifactId>hessian</artifactId> 65 <version>4.0.7</version> 66 </dependency> 67 <dependency> 68 <groupId>org.codehaus.castor</groupId> 69 <artifactId>castor-core</artifactId> 70 <version>1.3.3</version> 71 </dependency> 72 <dependency> 73 <groupId>org.codehaus.castor</groupId> 74 <artifactId>castor-xml</artifactId> 75 <version>1.3.3</version> 76 </dependency> 77 78 </dependencies> 79 </project>
有一些冗余的包
现在启动Tomcat,一个可以使用hessian协议的生产者就注册好了
二 、下面是对服务的测试,我是在同一个工程中写的src/test/java和src/test/resource中写的测试类和添加的配置
spring_dubbo_consumer.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/aop 9 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context-3.0.xsd 12 http://code.alibabatech.com/schema/dubbo 13 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 14 15 <dubbo:application name="dubbo-hessian-consumer"></dubbo:application> 16 <dubbo:registry address="zookeeper://****:2281" check="false"></dubbo:registry> 17 <dubbo:reference interface="com.isoftstone.iics.email.services.send.common.test.HessianTestService" id="hessian" timeout="100000" check="false" /> 18 </beans>
测试类:
1 package com.yj.fenghao.hessiantest; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 6 import org.junit.Ignore; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 import com.isoftstone.iics.email.services.send.common.test.HessianTestService; 10 11 public class HessianTest { 12 13 ClassPathXmlApplicationContext context; 14 public HessianTest(){ 15 context=new ClassPathXmlApplicationContext(new String[]{ 16 "classpath*:spring_dubbo_consumer.xml" 17 }); 18 } 19 20 public static final HessianTest test=new HessianTest(); 21 22 @Ignore 23 @org.junit.Test 24 public void Test(){ 25 HessianTestService hessian = (HessianTestService)test.context.getBean("hessian"); 26 String sayHello = hessian.sayHello("ni hao test is pass"); 27 System.out.println(" result is "+sayHello); 28 } 29 30 @org.junit.Test 31 public void TestIO(){ 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(" result is "+result); 36 } catch (FileNotFoundException e) { 37 e.printStackTrace(); 38 } 39 } 40 41 }
结束语:看来做事总要换个思路,以前做这个,刚刚开始总想做成直连的,总是不成功,后来使用zooker注册中心,直接就成功了!