在(dubbo二)文章中,我们将customer和provider分开部署了,这次主要记录文件在customer和provider之间的传输.
如果文件直接在customer中进行保存,不需要传递到provider,则和dubbo就没有关系了.
如果需要传递,如果是使用dubbo协议的话,是不支持 file,inputStream 这种文件,流的传输的.
dubbo中customer和provider之间的通信协议支持很多种,详见点击打开官方文档
我们这里使用hessian协议进行传输(当然也可以转换成byte[]继续使用dubbo协议)
下面是需要进行的配置
customer
需要引入hessian的包
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency>
provider
pom.xml 需要引入 hessian,jetty的包(Hessian底层采用Http通讯,采用Servlet暴露服务,Dubbo缺省内嵌Jetty作为服务器实现)
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty</artifactId> <version>6.1.26</version> </dependency>
provider.xml 需要配置hessian协议,并给service设置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 监控中心 --> <dubbo:monitor protocol="registry" /> <!-- 暴露服务 --> <dubbo:protocol name="dubbo" port="20886" /> <dubbo:protocol name="hessian" port="20887"/> <dubbo:service interface="com.dingcheng.user.facade.UserFacade" ref="userFacade" /> <!-- 需要指定hessian协议,否则会在上面设置的协议中随机调用,就会一会成功一会失败 --> <dubbo:service protocol="hessian" interface="com.dingcheng.user.facade.UploadFacade" ref="uploadFacade" /></beans>
其中 <dubbo:protocol name="hessian" port="20887"/> 就是默认的 server="jetty", 如果设置server="servlet",则需要web容器支持,我们的provider是以jar方式运行的,所以不适用这种.