一致性hash算法Consistent Hashing

对于原有hash算法hash%n



so...

1.话不多说直接上代码,原理或详解自行百度即可

import cn.pheker.utils.UtilElapsedTime;
import cn.pheker.utils.UtilLogger;
import cn.pheker.utils.UtilMD5; import java.util.*; /**
* <pre>
* author cn.pheker
* date 2018/3/19 10:20
* email 1176479642@qq.com
* desc 一致性哈希算法
*
* </pre>
*/
public class ConsistentHashing { private static final long MIN_NODE_NUMBER = 0;
private static final int MAX_NODE_NUMBER = Integer.MAX_VALUE;
private static int VIRTUAL_NODE_NUMBER; //真实节点
private List<Node> realNodes = new LinkedList<Node>();
//虚拟节点,hash环
private SortedMap<Integer, VNode> circle = new TreeMap<Integer, VNode>(); private ConsistentHashing(int vnNumber) {
VIRTUAL_NODE_NUMBER = vnNumber;
} public static ConsistentHashing build(int vnNumber) {
return new ConsistentHashing(vnNumber);
} public ConsistentHashing addNode(Map<String,String> ipPorts) {
Set<String> ips = ipPorts.keySet();
Iterator<String> ipite = ips.iterator();
while(ipite.hasNext()){
String ip = ipite.next();
addNode(ip, ipPorts.get(ip));
}
return this;
} public ConsistentHashing addNode(String ip, int port) {
addNode(ip, String.valueOf(port));
return this;
} public ConsistentHashing addNode(String ip, String port) {
Node node = new Node(ip, port);
if (!realNodes.contains(node)) {
realNodes.add(node);
UtilLogger.println("[Node]:"+node.toString()+" Hash:"+node.hashFNV1_32());
//生成VIRTUAL_NODE_NUMBER个虚拟节点
for (int i = 0; i < VIRTUAL_NODE_NUMBER; i++) {
VNode vNode = node.createVNode(i);
circle.put(vNode.hashFNV1_32(), vNode);
UtilLogger.println("\t[VNode]:"+vNode.toString()+" Hash:"+vNode.hashFNV1_32());
}
}
return this;
} public void removeNode(String ip,String port) {
Node node = new Node(ip, port);
for(int i = 0;i<VIRTUAL_NODE_NUMBER;i++) {
VNode vNode = node.createVNode(i);
circle.remove(vNode.hashFNV1_32());
}
circle.remove(node.hashFNV1_32());
} public VNode getNode(String ip, int port) {
return getNode(ip, String.valueOf(port));
} public VNode getNode(String ip, String port) {
Node node = new Node(ip, port);
int hash = node.hashFNV1_32();
if(circle.isEmpty()) return null;
SortedMap<Integer, VNode> tailMap = circle.tailMap(hash);
int hashKey;
if (tailMap.isEmpty()) {
hashKey = circle.firstKey();
}else {
hashKey = tailMap.firstKey();
}//顺时针最近节点
VNode vNode = circle.get(hashKey);
UtilLogger.println(String.format("[%s]:%s ==> [%s]:%s",
node.hashFNV1_32(),node,vNode.hashFNV1_32(),vNode));
return vNode;
} /**
* 第个节点都是一个服务器主机
*/
public class Node {
String ip;
String port; public Node(String ip, String port) {
this.ip = ip;
this.port = port;
} public String getIp() {
return ip;
} public void setIp(String ip) {
this.ip = ip;
} public String getPort() {
return port;
} public void setPort(String port) {
this.port = port;
} public VNode createVNode(int vnNumber) {
return new VNode(ip, port, vnNumber);
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node vNode = (Node) o;
return Objects.equals(ip, vNode.ip) &&
Objects.equals(port, vNode.port);
} @Override
public String toString() {
return ip + ":" + port;
} /**
*使用FNV1_32_HASH算法计算服务器的Hash值,这里不能重写hashCode的方法
*/
public int hashFNV1_32(){
String str = UtilMD5.MD5(this.toString());
final int p = 16777619;
int hash = (int)2166136261L;
for (int i = 0; i < str.length(); i++)
hash = (hash ^ str.charAt(i)) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5; // 如果算出来的值为负数则取其绝对值
if (hash < 0)
hash = Math.abs(hash);
return hash;
}
} /**
* 虚拟节点
*/
public class VNode extends Node{
int number; public VNode(String ip, String port,int number) {
super(ip, port);
this.number = number;
} public Node toNode() {
return new Node(ip,port);
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
VNode vNode = (VNode) o;
return number == vNode.number;
} @Override
public int hashCode() {
return Objects.hash(number);
} @Override
public String toString() {
return ip + ":" + port+"#"+number;
}
} /**
* 校验hash分布均匀性
* @return
*/
public float check() {
Set<Integer> ks = circle.keySet();
int size = ks.size();
long sum = 0L;
for (int hash : ks) {
sum += hash;
}
double percent = size == MIN_NODE_NUMBER ? MIN_NODE_NUMBER :
(100*2*sum/size)/MAX_NODE_NUMBER;
return Float.valueOf(String.format("%.2f", percent));
} public static void main(String[] args) {
UtilElapsedTime.test(() -> {
ConsistentHashing ch = ConsistentHashing.build(3);
//添加节点
UtilLogger.println("------------添加节点----------------");
ch.addNode("10.96.74.187", 80);
ch.addNode("127.0.0.1", 8080);
ch.addNode("243.15.155.0", 2150);
ch.addNode("243.15.155.1", 2150); UtilLogger.println("------------是否均匀----------------");
UtilLogger.println(ch.check() + "%"); //获取节点
UtilLogger.println("------------获取节点----------------");
ch.getNode("10.96.74.187", 80);
ch.getNode("123.1.122.253", 44);
ch.getNode("234.67.80.219", 3306);
return "耗时计算完成";
});
} }

2.结果

------------添加节点----------------
[Node]:10.96.74.187:80 Hash:1695118842
[VNode]:10.96.74.187:80#0 Hash:1661313686
[VNode]:10.96.74.187:80#1 Hash:1283046442
[VNode]:10.96.74.187:80#2 Hash:564332117
[Node]:127.0.0.1:8080 Hash:678080562
[VNode]:127.0.0.1:8080#0 Hash:1731933288
[VNode]:127.0.0.1:8080#1 Hash:1369405387
[VNode]:127.0.0.1:8080#2 Hash:200594664
[Node]:243.15.155.0:2150 Hash:1175061629
[VNode]:243.15.155.0:2150#0 Hash:134880260
[VNode]:243.15.155.0:2150#1 Hash:1677894747
[VNode]:243.15.155.0:2150#2 Hash:522817245
[Node]:243.15.155.1:2150 Hash:1305999210
[VNode]:243.15.155.1:2150#0 Hash:1193457699
[VNode]:243.15.155.1:2150#1 Hash:279279823
[VNode]:243.15.155.1:2150#2 Hash:2115663065
------------是否均匀----------------
98.0%
------------获取节点----------------
[1695118842]:10.96.74.187:80 ==> [1731933288]:127.0.0.1:8080#0
[601034131]:123.1.122.253:44 ==> [1193457699]:243.15.155.1:2150#0
[508181784]:234.67.80.219:3306 ==> [522817245]:243.15.155.0:2150#2
[23.104187ms] 耗时计算完成 Process finished with exit code 0

3.注意事项

代码中用到了几个工具类UtilMD5,UtilLogger换成自己的即可,UtilElapsedTime用于计算耗时,可以直接去掉。

4.参考链接

对一致性Hash算法,Java代码实现的深入研究

白话解析:一致性哈希算法 consistent hashing

最新文章

  1. Java读写资源文件类Properties
  2. linux TLS 线程本地变量
  3. Java你可能不知道的事(3)HashMap
  4. 【POJ 1523】SPF(割点)
  5. HTML5 video 支持air play
  6. 待实验:Android 增量升级
  7. 关于header(&#39;location:url&#39;)的一些说明,php缓冲区
  8. HTTP头域列表与解释 之 request篇
  9. display:none与visible:hidden区别
  10. 李洪强漫谈iOS开发[C语言-013]-常量
  11. BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛
  12. 初识Treap
  13. Go 语言打包静态文件
  14. 洛谷P3434 [POI2006]KRA-The Disks(线段树)
  15. oracle11实战详解
  16. 装修工人如何在网上&quot;找活&quot;
  17. c/c++ 数组 数组的引用,指针数组的引用
  18. java项目打成war包
  19. redis sentinel哨兵模式集群搭建教程
  20. 小程序API录音 微信录音后 Silk格式转码MP3

热门文章

  1. Codeforces 1209E2. Rotate Columns (hard version)
  2. 使用python 实现 微信好友 个性签名 并 制作 词云图
  3. 从POST与GET、REQUEST响应的php和asp写法对比谈数据过滤
  4. Genymotion模拟器使用camera
  5. Spring Boot自动配置总结
  6. 单点登录之ajax跨域实现
  7. iconv_close - 关闭字符转换描述符
  8. OpenCV笔记(I)
  9. linux如何配置使用sendEmail发送邮件
  10. 主流NoSQL数据库的分析与选择