您的位置:首页 > 其它

dubbo负载均衡策略--- 一致哈希算法

2015-12-08 22:18 405 查看



dubbo负载均衡策略一致哈希算法源码

dubbo是阿里巴巴公司开发的一个开源分布式应用框架,基于服务的发布者和订阅者,服务者启动服务向注册中心发布自己的服务;消费者(订阅者)启动服务器向注册中心订阅所需要的服务。注册中心将订阅的服务注册列表返回给订阅者。注册中心会感应服务的提供者的变化,如果服务的提供者发生变化,注册中心会立即通知消费者及时变更服务信息数据;dubbo并且带有审计功能--监控中心,服务的发布者和服务的消费者每分钟间隔都会向监控中心发送自己的统计情况如:调用次数 或者调用时间等(这些数据是保存在内存中以每分钟为单位向监控中心进行发送数据)。监控中心宕机不影响使用,只是减少部分采样的数据,对等的集群任意一台服务宕机,会自动切换到其他对等的机器,注册中心宕机,对服务没有影响,订阅者自己本地会缓存服务提供者的列表信息,由此可见dubbo的健壮性还是可以的。

dubbo有很多非常好的特性:负载均衡、集群容错等


伦理片 http://www.dotdy.com/

集群容错策略:
failover :失败重连;

failfast:只发起一次请求;

failsafe:失败直接忽略;

failback:失败返回定时发送;、

forking:并发执行 只要有一台成功立即返回;

broadcast:调用所有提供者任意一台报错就报错)。

负载均衡:

随机按照权重概率选择

轮循,按公约后的权重设置轮循比率

最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。
一致哈希:相同的请求参数会请求同一个服务提供者。

支持多协议:

dubbo、thrift、rmi、jms、redis 等。

下面我们看下dubbo的一直哈希策略的实现源代码:默认有160个虚拟节点 这样让服务列表更加均匀分布,命中更均匀。

Java代码


/*

* Copyright 1999-2012 Alibaba Group.

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0
*

* Unless required by applicable law or agreed to in writing, software

* See the License for the specific language governing permissions and

}

public Invoker<T> select(Invocation invocation) {

String key = toKey(invocation.getArguments());

byte[] digest = md5(key);

Invoker<T> invoker = sekectForKey(hash(digest, 0));

return invoker;

}

private String toKey(Object[] args) {

StringBuilder buf = new StringBuilder();

for (int i : argumentIndex) {

if (i >= 0 && i < args.length) {

buf.append(args[i]);

}

}

return buf.toString();

}

private Invoker<T> sekectForKey(long hash) {

Invoker<T> invoker;

Long key = hash;

if (!virtualInvokers.containsKey(key)) {

SortedMap<Long, Invoker<T>> tailMap = virtualInvokers.tailMap(key);

if (tailMap.isEmpty()) {

key = virtualInvokers.firstKey();

} else {

key = tailMap.firstKey();

}

影音先锋电影 http://www.iskdy.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: