本篇内容主要讲解“sofa-registry是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“sofa-registry是什么”吧!
SOFARegistry 是蚂蚁金服开源的一个生产级、高时效、高可用的服务注册中心
功能特性
* 支持服务发布与服务订阅
* 支持服务变更时的主动推送
* 丰富的 REST 接口
* 采用分层架构及数据分片,支持海量连接及海量数据
* 支持多副本备份,保证数据高可用
* 基于 SOFABolt 通信框架,服务上下线秒级通知
* AP 架构,保证网络分区下的可用性
其支持服务发布与服务订阅
功能,依赖一致性hash算法, 其简介:参见:https://www.jianshu.com/p/e968c081f563
在解决分布式系统中负载均衡的问题时候可以使用Hash算法让固定的一部分请求落到同一台服务器上,这样每台服务器固定处理一部分请求(并维护这些请求的信息),起到负载均衡的作用。 但是普通的余数hash(hash(比如用户id)%服务器机器数)算法伸缩性很差,当新增或者下线服务器机器时候,用户id与服务器的映射关系会大量失效。一致性hash则利用hash环对其进行了改进。
核心代码参见:代码地址:[ConsistentHash.java](https://github.com/sofastack/sofa- registry/blob/master/server/consistency/src/main/java/com/alipay/sofa/registry/consistency/hash/ConsistentHash.java "ConsistentHash.java")
private final SortedMap<Integer, T> circle = new TreeMap<>();
/**
* This returns the closest node for the object. If the object is the node it
* should be an exact hit, but if it is a value traverse to find closest
* subsequent node.
* @param key the key
* @return node for
*/
public T getNodeFor(Object key) {
if (circle.isEmpty()) {
return null;
}
int hash = hashFunction.hash(key);
T node = circle.get(hash);
if (node == null) {
// inexact match -- find the next value in the circle
SortedMap<Integer, T> tailMap = circle.tailMap(hash);
hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();
node = circle.get(hash);
}
return node;
}
获取大于该node节点对应hash值的的hash环(tailMap方法)信息,即tailMap
若tailMap不为空,则获取最近的一个node节点(firstKey() 方法)
若tailMap为空,则获取hash环的第一个node节点(firstKey() 方法)
tailMap(K fromKey) 方法用于返回此映射,其键大于或等于fromKey的部分视图。
返回的映射受此映射支持,因此改变返回映射反映在此映射中,反之亦然。
新的节点尝试注册进来,会调用addNode(T node)
方法,同时会有虚拟节点存在
/**
* Add a new node to the consistent hash
*
* This is not thread safe.
* @param node the node
*/
private void addNode(T node) {
realNodes.add(node);
for (int i = 0; i < numberOfReplicas; i++) {
// The string addition forces each replica to have different hash
circle.put(hashFunction.hash(node.getNodeName() + SIGN + i), node);
}
}
到此,相信大家对“sofa-registry是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3136014/blog/3083372