源码地址:https://gitee.com/xuxinsunqizheng/dubbo.git
在平时dubbo使用中,我们一般是使用的dubbo协议,但是dubbo协议并不是适用于所有场景,比如文件上传或者大字符串的传输,我们查看官网:http://dubbo.apache.org/zh-cn/docs/user/references/protocol/dubbo.html,可以发现dubbo支持多种协议,我们需要根据具体业务挑选所适用的协议,这点我们可以阅读官方文档,查看适用范围进行了解,下面我们进行多协议的配置开发
项目结构如下:
user-api:在实际使用中会打成jar包,定义了项目中使用到的所有接口,这样主要是为了避免重复代码,实体也是可以放在这里面。
user-service:服务提供者,
user-web:服务消费者
一.服务提供者:
服务提供者需要添加jetty以及web启动的相关依赖,不然会报错:
<!--dubbo多协议jettyjar包-->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version> </dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
但是dubbo服务提供者不是web项目,不是以web进行部署,我们需要改一下启动类,让项目不以web项目启动,
package com.xdclass.user.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ImportResource;
@EnableAutoConfiguration
@ImportResource("classpath:provider.xml")
public class ServiceApplication {
public static void main(String[] args)
{
//配置项目不以web项目启动
new SpringApplicationBuilder(ServiceApplication.class).web(WebApplicationType.NONE).run();
// SpringApplication.run(ServiceApplication.class);
}
}
然后模拟一个文件上传接口,在api中定义接口:
在service中写接口实现,打印一下传输的信息:
xml配置文件,在配置文件中定义一个http协议以及暴露的端口,并在需要使用该协议的接口中加上protocol属性。
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:registry address="zookeeper://192.168.17.134:2181"/>
<dubbo:protocol name="dubbo" port="12345"/>
<!--http协议暴露在12346端口-->
<dubbo:protocol name="http" port="12346"/>
<!--xml方式设置超时时间,接口级别以及方法级别,接口中某个方法可能比较耗时,设置超时时间长一点,注解方式暂时不支持方法的超时时间-->
<!--cluster为集群容错模式,如果设置了快速失败,就算消费者设置尝试多次,但也只会调用一次-->
<dubbo:service interface="com.xdclass.user.service.UserService" ref="userService" timeout="3000" cluster="failfast ">
<dubbo:method name="sayHello" timeout="3000"></dubbo:method>
</dubbo:service>
<!--文件上传接口使用http协议,因为dubbo协议对大文件的字符串,以及文件支持不是很好-->
<dubbo:service interface="com.xdclass.user.service.FileService" ref="fileService" timeout="3000" protocol="http"></dubbo:service>
<bean id="userService" class="com.xdclass.user.service.impl.UserServiceImpl"/>
<bean id="fileService" class="com.xdclass.user.service.impl.FileServiceImpl"/>
</beans>
启动该服务:
发现服务启动成功,然后去zookeeper启动zkcli.sh客户端查看,发现在zookeeper上FileService接口已经有了,他的协议是http协议,所以服务提供者配置成功
二.服务消费者:
服务消费者配置比较简单,写一个接口调用刚刚定义的service
配置文件引用如下:
启动,并访问接口:发现控制台已打印,测试成功