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

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

集群容错策略:

failover :失败重连;

failfast:只发起一次请求;

failsafe:失败直接忽略;

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

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

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

负载均衡:

随机按照权重概率选择

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

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

支持多协议:

dubbo、thrift、rmi、jms、redis 等。

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

./*
2. * Copyright 1999-2012 Alibaba Group.
3. *
4. * Licensed under the Apache License, Version 2.0 (the "License");
5. * you may not use this file except in compliance with the License.
6. * You may obtain a copy of the License at
7. *
8. * http://www.apache.org/licenses/LICENSE-2.0
9. *
10. * Unless required by applicable law or agreed to in writing, software
11. * distributed under the License is distributed on an "AS IS" BASIS,
12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13. * See the License for the specific language governing permissions and
14. * limitations under the License.
15. */
.package com.alibaba.dubbo.rpc.cluster.loadbalance;
.
.import java.io.UnsupportedEncodingException;
.import java.security.MessageDigest;
.import java.security.NoSuchAlgorithmException;
.import java.util.List;
.import java.util.SortedMap;
.import java.util.TreeMap;
.import java.util.concurrent.ConcurrentHashMap;
.import java.util.concurrent.ConcurrentMap;
.
.import com.alibaba.dubbo.common.Constants;
.import com.alibaba.dubbo.common.URL;
.import com.alibaba.dubbo.rpc.Invocation;
.import com.alibaba.dubbo.rpc.Invoker;
.
./**
33. * ConsistentHashLoadBalance
34. *
35. * @author william.liangf
36. */
.public class ConsistentHashLoadBalance extends AbstractLoadBalance {
.
. private final ConcurrentMap<String, ConsistentHashSelector<?>> selectors = new ConcurrentHashMap<String, ConsistentHashSelector<?>>();
.
. @SuppressWarnings("unchecked")
. @Override
. protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
. String key = invokers.get().getUrl().getServiceKey() + "." + invocation.getMethodName();
. int identityHashCode = System.identityHashCode(invokers);
. ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) selectors.get(key);
. if (selector == null || selector.getIdentityHashCode() != identityHashCode) {
. selectors.put(key, new ConsistentHashSelector<T>(invokers, invocation.getMethodName(), identityHashCode));
. selector = (ConsistentHashSelector<T>) selectors.get(key);
. }
. return selector.select(invocation);
. }
.
. private static final class ConsistentHashSelector<T> {
.
. private final TreeMap<Long, Invoker<T>> virtualInvokers;
.
. private final int replicaNumber;
.
. private final int identityHashCode;
.
. private final int[] argumentIndex;
.
. public ConsistentHashSelector(List<Invoker<T>> invokers, String methodName, int identityHashCode) {
. this.virtualInvokers = new TreeMap<Long, Invoker<T>>();
. this.identityHashCode = System.identityHashCode(invokers);
. URL url = invokers.get().getUrl();
. this.replicaNumber = url.getMethodParameter(methodName, "hash.nodes", );
. String[] index = Constants.COMMA_SPLIT_PATTERN.split(url.getMethodParameter(methodName, "hash.arguments", ""));
. argumentIndex = new int[index.length];
. for (int i = ; i < index.length; i ++) {
. argumentIndex[i] = Integer.parseInt(index[i]);
. }
. for (Invoker<T> invoker : invokers) {
. for (int i = ; i < replicaNumber / ; i++) {
. byte[] digest = md5(invoker.getUrl().toFullString() + i);
. for (int h = ; h < ; h++) {
. long m = hash(digest, h);
. virtualInvokers.put(m, invoker);
. }
. }
. }
. }
.
. public int getIdentityHashCode() {
. return identityHashCode;
. }
.
. public Invoker<T> select(Invocation invocation) {
. String key = toKey(invocation.getArguments());
. byte[] digest = md5(key);
. Invoker<T> invoker = sekectForKey(hash(digest, ));
. return invoker;
. }
.
. private String toKey(Object[] args) {
. StringBuilder buf = new StringBuilder();
. for (int i : argumentIndex) {
. if (i >= && 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();
. }
. }
. invoker = virtualInvokers.get(key);
. return invoker;
. }
.
. private long hash(byte[] digest, int number) {
. return (((long) (digest[ + number * ] & 0xFF) << )
. | ((long) (digest[ + number * ] & 0xFF) << )
. | ((long) (digest[ + number * ] & 0xFF) << )
. | (digest[ + number * ] & 0xFF))
. & 0xFFFFFFFFL;
. }
.
. private byte[] md5(String value) {
. MessageDigest md5;
. try {
. md5 = MessageDigest.getInstance("MD5");
. } catch (NoSuchAlgorithmException e) {
. throw new IllegalStateException(e.getMessage(), e);
. }
. md5.reset();
. byte[] bytes = null;
. try {
. bytes = value.getBytes("UTF-8");
. } catch (UnsupportedEncodingException e) {
. throw new IllegalStateException(e.getMessage(), e);
. }
. md5.update(bytes);
. return md5.digest();
. }
.
. }
.
.}

阿里巴巴的分布式应用框架-dubbo负载均衡策略--- 一致哈希算法的更多相关文章

  1. dubbo负载均衡策略和集群容错策略都有哪些

    dubbo负载均衡策略 random loadbalance 默认情况下,dubbo是random load balance随机调用实现负载均衡,可以对provider不同实例设置不同的权重,会按照权 ...

  2. dubbo负载均衡策略和集群容错策略

    dubbo负载均衡策略 random loadbalance 默认情况下,dubbo是random load balance随机调用实现负载均衡,可以对provider不同实例设置不同的权重,会按照权 ...

  3. 3.dubbo 负载均衡策略和集群容错策略都有哪些?动态代理策略呢?

    作者:中华石杉 面试题 dubbo 负载均衡策略和集群容错策略都有哪些?动态代理策略呢? 面试官心理分析 继续深问吧,这些都是用 dubbo 必须知道的一些东西,你得知道基本原理,知道序列化是什么协议 ...

  4. 分布式的几件小事(四)dubbo负载均衡策略和集群容错策略

    1.dubbo负载均衡策略 ①random loadbalance 策略 默认情况下,dubbo是random loadbalance 随机调用实现负载均衡,可以对provider不同实例设置不同的权 ...

  5. Dubbo入门到精通学习笔记(十一):Dubbo服务启动依赖检查、Dubbo负载均衡策略、Dubbo线程模型(结合Linux线程数限制配置的实战分享)

    文章目录 Dubbo服务启动依赖检查 Dubbo负载均衡策略 Dubbo线程模型(结合Linux线程数限制配置的实战分享) 实战经验分享( ** 属用性能调优**): Dubbo服务启动依赖检查 Du ...

  6. 面试系列24 dubbo负载均衡策略和集群容错策略

    (1)dubbo负载均衡策略 1)random loadbalance 默认情况下,dubbo是random load balance随机调用实现负载均衡,可以对provider不同实例设置不同的权重 ...

  7. 面试系列16 dubbo负载均衡策略和集群容错策略都有哪些?动态代理策略呢

    (1)dubbo负载均衡策略 1)random loadbalance 默认情况下,dubbo是random load balance随机调用实现负载均衡,可以对provider不同实例设置不同的权重 ...

  8. dubbo负载均衡策略和集群容错策略都有哪些?动态代理策略呢?

    (1)dubbo负载均衡策略 1)random loadbalance 默认情况下,dubbo是random load balance随机调用实现负载均衡,可以对provider不同实例设置不同的权重 ...

  9. 一文讲透Dubbo负载均衡之最小活跃数算法

    本文是对于Dubbo负载均衡策略之一的最小活跃数算法的详细分析.文中所示源码,没有特别标注的地方均为2.6.0版本. 为什么没有用截止目前的最新的版本号2.7.4.1呢?因为2.6.0这个版本里面有两 ...

随机推荐

  1. 稀疏自动编码之反向传播算法(BP)

    假设给定m个训练样本的训练集,用梯度下降法训练一个神经网络,对于单个训练样本(x,y),定义该样本的损失函数: 那么整个训练集的损失函数定义如下: 第一项是所有样本的方差的均值.第二项是一个归一化项( ...

  2. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  3. HDU 4941 Magical Forest 【离散化】【map】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 题目大意:给你10^5个点.每一个点有一个数值.点的xy坐标是0~10^9.点存在于矩阵中.然后 ...

  4. 【原创】AltiumDesigner 6 的自定义菜单

    AltiumDesigner 6 的自定义菜单,数据保存在DXP.RCS文件中通过测试,添加一个自定义菜单名为HHH,然后用Editplus在C:\Users\pcittsy\AppData\Roam ...

  5. TP复习15

    ## ThinkPHP 3.1.2 URL#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:一.URL规则 1.默认是区分大小写的 2.如果我们不想区分大小 ...

  6. [Ramda] Filter, Reject and Partition

    We'll learn how to get a subset of an array by specifying items to include with filter, or items to ...

  7. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  8. 对javascript this的理解

    对于this的理解,大部分时间都比较模糊,最近几天做了一些研究,记录一下 首先应该明白,this是执行上下文的一个属性,它的值取决于执行上下文,执行上下文和函数调用方式相关,定义一个function的 ...

  9. 基于jQuery的图片相册滑出放大插件

    今天给大家带来一款基于jQuery的图片相册滑出放大插件.点击相册图片,展示该图片.该插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗..效 ...

  10. MySQL ALTER语法的运用方法 && 操作索引和字段

    语法:alter_specification: ADD [COLUMN] create_definition [FIRST | AFTER column_name ] or ADD INDEX [in ...