dubbo调用模块核心功能是发起一个远程方法的调用并顺利拿到返回结果,其体系组成如下:
- 透明代理:通过动态代理技术,屏蔽远程调用细节以提高编程友好性
- 负载均衡:当有多个提供者是,如何选择哪个进行调用的负载算法
- 容错机制:当服务调用失败时采取的策略
- 调用方式:支持同步调用、异步调用
- 结果获取:指同步等待结果返回,还是异步通过回调通知获取结果
1.负载均衡
Dubbo 目前官方支持以下负载均衡策略:
- 随机(random):按权重设置随机概率。此为默认算法
- 轮循(roundrobin):按公约后的权重设置轮循比率
- 最少活跃调用数(leastactive):相同活跃数的随机,活跃数指调用前后计数差
- 一致性hash(consistenthash):相同的参数总是发到同一台机器
设置方式支持如下四种方式设置,优先级由低至高
<!-- 服务端级别-->
<dubbo:service interface="..." loadbalance="roundrobin" />
<!-- 客户端级别-->
<dubbo:reference interface="..." loadbalance="roundrobin" />
<!-- 服务端方法级别-->
<dubbo:service interface="...">
<dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:service>
<!-- 客户端方法级别-->
<dubbo:reference interface="...">
<dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:reference>
使用一致性hash时,默认虚拟出160个(可配置)节点(以达到分散的效果,节点挂掉不会影响),均匀分配给后端提供者,对默认第一个参数(可配置)进行很复杂的hash,固定指向一个虚拟节点,这个虚拟节点再找到真正的服务提供者。
2.容错
Dubbo 官方目前支持以下容错策略:
- 失败自动切换:调用失败后基于retries=“2” 属性重试其它服务器
- 快速失败:快速失败,只发起一次调用,失败立即报错
- 忽略失败:失败后勿略,不抛出异常给客户端
- 失败重试:失败自动恢复,后台记录失败请求,定时重发。通常用于消息通知操作
- 并行调用:只要一个成功即返回,并行调用指定数量机器,可通过 forks=“2” 来设置最大并行数
- 广播调用:广播调用所有提供者,逐个调用,任意一台报错则报错
设置方式支持如下两种方式设置,优先级由低至高
<!--
Failover 失败自动切换 retries="1" 切换次数
Failfast 快速失败
Failsafe 勿略失败
Failback 失败重试,5秒后仅重试一次
Forking 并行调用 forks="2" 最大并行数
Broadcast 广播调用
-->
<dubbo:service interface="..." cluster="broadcast" />
<dubbo:reference interface="..." cluster="broadcast"/ >
3.异步调用
异步调用是指发起远程调用之后获取结果的方式。
- 同步等待结果返回
- 异步等待结果返回
- 不需要返回结果
Dubbo 中关于异步等待结果返回的实现流程如下图:
异步调用配置:
<dubbo:reference id="asyncDemoService" interface="...">
<!-- 异步调async:true 异步调用 false 同步调用-->
<dubbo:method name="...1" async="true"/>
<dubbo:method name="...2" async="false"/>
<!-- return="false" 不需要返回结果,直接返回-->
<dubbo:method name="notReturn" return="false"/>
</dubbo:reference>
4.Dubbo 其它使用场景
泛化提供
是指不通过接口的方式直接将服务暴露出去。通常用于Mock框架或服务降级框架实现。
public static void doExportGenericService() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("demo-provider");
// 注册中心
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("192.168.xxx.xxx:2181");
ProtocolConfig protocol=new ProtocolConfig();
protocol.setPort(-1);
protocol.setName("dubbo");
GenericService demoService = new MyGenericService();
ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
// 弱类型接口名
service.setInterface("xxx");
// 指向一个通用服务实现
service.setRef(demoService);
service.setApplication(applicationConfig);
service.setRegistry(registryConfig);
service.setProtocol(protocol);
// 暴露及注册服务
service.export();
}
类似于写一个静态服务作为备用,当真正的服务挂掉时调用这个备用服务,内容时固定的
泛化引用
是指不通过常规接口的方式去引用服务,通常用于测试框架。
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("demo-provider");
// 注册中心
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("192.168.xxx.xxx:2181");
// 引用远程服务
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
// 弱类型接口名
reference.setInterface("xxx");
// 声明为泛化接口
reference.setGeneric(true);
reference.setApplication(applicationConfig);
reference.setRegistry(registryConfig);
// 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用
GenericService genericService = reference.get();
Object result = genericService.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"world"});
隐示传参
是指通过非常方法参数传递参数,类似于http 调用当中添加cookie值。通常用于分布式追踪框架的实现。使用方式如下 :
//客户端隐示设置值
RpcContext.getContext().setAttachment("index", "1"); // 隐式传参,后面的远程调用都会隐
//服务端隐示获取值
String index = RpcContext.getContext().getAttachment("index");
令牌验证
通过令牌验证在注册中心控制权限,以决定要不要下发令牌给消费者,可以防止消费者绕过注册中心访问提供者,另外通过注册中心可灵活改变授权方式,而不需修改或升级提供者
<!--随机token令牌,使用UUID生成--><dubbo:provider interface="com.foo.BarService" token="true" />