下面这张图基本可以概括其原理(图片我是复制自其它地方):
自己写了一个简单的实现,帮助理解,代码如下
ConsistentHash代码
/*
* 一个简单的一致性hash算法
*/
public class ConsistentHash {
private HashPair[] consisentArray;
private final int NUMBER_HASH = 50;
// 所有服务器节点、value值映射到hash的范围大小
private final int HASH_SIZE = 1 << 20;
// 将服务器的节点进行映射,使用String类型模拟
private void setCurServer(List<String> nodes) {
int serverNum = nodes.size();
consisentArray = new HashPair[serverNum * NUMBER_HASH];
for (int i = 0; i < serverNum * NUMBER_HASH; i++) {
consisentArray[i] = new HashPair();
}
int curPos = 0;
int nIndex = 0;
for (String node : nodes) {
int hashcode = SimpleHashUtil.hash_1(node, HASH_SIZE);
// 这里使用最简单的方法进行按组设置
for (int i = 0; i < consisentArray.length / nodes.size(); i++) {
consisentArray[curPos].setHash(Math.abs(hashcode));
consisentArray[curPos].setIndex(nIndex);
curPos++;
}
nIndex++;
}
// 重新排序
Arrays.sort(consisentArray);
}
// 获取某一个值的服务器index
public int getServerIndex(String key) {
int hashcode = Math.abs(SimpleHashUtil.hash_1(key, HASH_SIZE));
int nIndex = Arrays.binarySearch(consisentArray, hashcode);
if (nIndex < 0) {
nIndex = Math.abs(nIndex) - 1;
}
if (nIndex >= consisentArray.length) {
nIndex = consisentArray.length - 1;
}
return consisentArray[nIndex].getIndex();
}
}
其中的HashPair代码如下:
public class HashPair implements Comparable<Object> {
private int hash;
private int index;
public HashPair() {
}
public HashPair(int hash) {
this.hash = hash;
}
public int compareTo(Object o) {
int num = 0;
if (o instanceof HashPair) {
num = ((HashPair) o).getHash();
} else if (o instanceof Integer) {
num = ((Integer) o).intValue();
}
if (this.hash < num) {
return -1;
} else if (this.hash > num) {
return 1;
} else {
return 0;
}
}
}
分享到:
相关推荐
ConsistentHash 一致性hash算法的 java 和 C++ 实现
简单模拟实现一致性Hash,透过虚拟节点映射至实际结点,解决一致性Hash的单调性和平衡性问题。
主要介绍了Java语言Consistent Hash算法学习笔记(代码示例),分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
var ConsistentHash = require('consistent-hash') var hr = new ConsistentHash() hr.add('server1') hr.add('server2') var serverToUse = hr.get('resourceName') 安装 npm install consistent-hash 原料药...
#fly-archflylib创立的各种常见的架构技术内容列表cassandra-demo cassandra数据库的入门编程consistent-hash Java implementation of consistent-hashing基于java的一致性hash的实现一致性hash(consistent-hashing)...
一致性哈希 consistent-hash Implementing Consistent Hashing in Kotlin Java Kotlin实现的一致性...val consistentHash = ConsistentHashHelper.create() .withNodes(listOf(a, b)) .build() consistantHash.getNo
[1] 用法import jump "github.com/lithammer/go-jump-consistent-hash"func main () { h := jump . Hash ( 256 , 1024 ) // h = 520} 包括用于将string用作键而不是uint64的辅助函数。 这需要一个哈希器,该Hash()...
#JCH luvit 实现 Consistant 。 从纸上 我们提出了跳转一致哈希,这是一种快速、最小内存、一致的哈希算法,可以用大约 5 行代码表示。 与Karger等人的算法相比,跳转一致hash不需要存储,速度更快,并且在桶之间...
该库是基于出色的实现的Consistent Hash Ring的实现。安装将此添加到应用程序的shard.yml : dependencies : hash_ring : github : TobiasGSmollett/hash_ring用法require " hash_ring "# Register 3 servershash_...
lua 一致性哈希基于 yaoweibin 的一致性哈希分支( )在 lua 中重新实现一致性哈希用法 local chash = require " chash "chash. add_upstream ( " 192.168.0.251 " )chash. add_upstream ( " 192.168.0.252 " )chash...
本文实例讲述了PHP实现的一致性Hash算法。分享给大家供大家参考,具体如下: 一致性哈希算法是分布式系统中常用的算法,为...因此,引入了一致性Hash(Consistent Hashing)分布算法 把数据用hash函数(如md5,sha1
AnchorHash: 是Go的最小内存AnchorHash(一致哈希)实现
在《基于一致性hash算法(consistent hashing)的使用详解》一文中已经介绍了一致性hash的基本原理,本文将会对其具体实现细节进行描述,并用c++语言对一致性hash进行了简单的实现
包 jch 提供了 Go 中 Jump Consistent Hash 一致性哈希算法的实现。 一致性哈希旨在最小化存储桶数量变化时的哈希变化,对于数据分片特别有用。 有关一致性哈希的更多信息,请访问 。 Jump Consistent Hash 由 ...
一致性哈希 该库实现了介绍的。 此库仅用于学习目的。 查看此了解更多信息。
一致的散列这是一致性哈希的简单JavaScript实现。 有关一致性哈希的更多信息,请参见。安装使用以下命令安装依赖项: $ npm install 用法var ConsistentHashing = require ( './consistent_hashing' ) ;var node...
持续的 Node.JS 的一致性哈希模块 安装 npm install consistent ... hash : 'md5' // can use 'md5' or 'murmurhash' } ) ; console . log ( ring . get ( 'some key' ) ) ; // outputs member1 or member2 or membe
ConsistentHash 一致性Hash WordCount Map-Reduce算法例子 Retrive 文件下载 IP 获得IP地址示例 ip QQ纯真数据库示例 HtmlParser 网页内容提取库HtmlParser的源码项目 nekohtml-1.9.7 nekohtml的源码项目 RhinoTest ...
ConsistentHash 一致hash算法 HashAlgorithms hash算法大全 MurmurHash MurMurHash算法,是非加密HASH算法,性能很高,碰撞率低 IPSeeker 封装了腾讯的ip库,提供一些工具,读取QQwry.dat文件,以根据ip获得好友位置 ...
介绍了一致哈希的概念,并给出了实现它的算法。 一致的哈希指定在服务器之间分配数据的方式,使得无需完全重组数据即可添加或删除服务器。 最初提出将其用于Internet上的Web缓存,以解决客户端可能不了解整个缓存...