一致性hash算法结构图
分表分库结构图
可进行循环冗余存储,顺时针存储到下一个物理节点(非虚拟节点)
package com.haiziwang.platform.kmcsms.route.algorithm;
import java.util.Collection;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* 一致性Hash算法
* 算法详解:http://blog.csdn.net/sparkliang/article/details/5279393
* 算法实现:https://weblogs.java.net/blog/2007/11/27/consistent-hashing
*
* @param 节点类型,比如ip或host等
* @author xiaoleilu
*/
public class ConsistentHash {
/**
* Hash计算对象,用于自定义hash算法
*/
HashFunc hashFunc;
/**
* 复制的节点个数
*/
private final int numberOfReplicas;
/**
* 一致性Hash环
*/
private final SortedMap circle = new TreeMap();
/**
* 构造,使用Java默认的Hash算法
*
* @param numberOfReplicas 复制的节点个数,增加每个节点的复制节点有利于负载均衡
* @param nodes 节点对象
*/
public ConsistentHash(int numberOfReplicas, Collection nodes) {
this.numberOfReplicas = numberOfReplicas;
this.hashFunc = new HashFunc() {
@Override
public Integer hash(Object key) {
String data = key.toString();
//默认使用FNV1hash算法
final int p = 16777619;
int hash = (int) 2166136261L;
for (int i = 0; i < data.length(); i++)
hash = (hash ^ data.charAt(i)) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return hash;
}
};
//初始化节点
for (T node : nodes) {
add(node);
}
}
/**
* 构造
*
* @param hashFunc hash算法对象
* @param numberOfReplicas 复制的节点个数,增加每个节点的复制节点有利于负载均衡
* @param nodes 节点对象
*/
public ConsistentHash(HashFunc hashFunc, int numberOfReplicas, Collection nodes) {
this.numberOfReplicas = numberOfReplicas;
this.hashFunc = hashFunc;
//初始化节点
for (T node : nodes) {
add(node);
}
}
/**
* 增加节点
* 每增加一个节点,就会在闭环上增加给定复制节点数
* 例如复制节点数是2,则每调用此方法一次,增加两个虚拟节点,这两个节点指向同一Node
* 由于hash算法会调用node的toString方法,故按照toString去重
*
* @param node 节点对象
*/
public void add(T node) {
for (int i = 0; i < numberOfReplicas; i++) {
circle.put(hashFunc.hash(node.toString() + i), node);
}
}
/**
* 移除节点的同时移除相应的虚拟节点
*
* @param node 节点对象
*/
public void remove(T node) {
for (int i = 0; i < numberOfReplicas; i++) {
circle.remove(hashFunc.hash(node.toString() + i));
}
}
/**
* 获得一个最近的顺时针节点
*
* @param key 为给定键取Hash,取得顺时针方向上最近的一个虚拟节点对应的实际节点
* @return 节点对象
*/
public T get(Object key) {
if (circle.isEmpty()) {
return null;
}
int hash = hashFunc.hash(key);
if (!circle.containsKey(hash)) {
SortedMap tailMap = circle.tailMap(hash); //返回此映射的部分视图,其键大于等于 hash
hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();
}
//正好命中
return circle.get(hash);
}
/**
* Hash算法对象,用于自定义hash算法
*
* @author xiaoleilu
*/
public interface HashFunc {
public Integer hash(Object key);
}
}
package com.haiziwang.platform.kmcsms.route.demo;
/**
* Created by wangzhongbao on 2016/12/14.
*/
public class NodeBean {
private String dbIp;
public NodeBean(String dbIp) {
this.dbIp = dbIp;
}
public String getDbIp() {
return dbIp;
}
@Override
public String toString() {
return dbIp;
}
/*@Override
public boolean equals(Object obj) {
return this.toString().equals(obj.toString());
}
@Override
public int hashCode() {
return 1;
}*/
}
package com.haiziwang.platform.kmcsms.route.demo;
/**
* Created by wangzhongbao on 2016/12/14.
*/
public class TelUtil {
private static String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
public static String getTel() {
int index= getNum(0,telFirst.length-1);
String first=telFirst[index];
String second=String.valueOf(getNum(1,10000)+10000).substring(1);
String thrid=String.valueOf(getNum(1,10000)+10000).substring(1);
return first+second+thrid;
}
private static int getNum(int start,int end) {
return (int)(Math.random()*(end-start+1)+start);
}
}
package com.haiziwang.platform.kmcsms.route.demo;
import com.haiziwang.platform.kmcsms.route.algorithm.ConsistentHash;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wangzhongbao on 2016/12/14.
*/
public class Sample {
public static void main(String[] args) {
//每个节点虚拟出来的节点数
final int numberOfReplicas = 10000;
List nodeList = new ArrayList();
nodeList.add(new NodeBean("000"));
nodeList.add(new NodeBean("111"));
nodeList.add(new NodeBean("222"));
nodeList.add(new NodeBean("333"));
nodeList.add(new NodeBean("444"));
nodeList.add(new NodeBean("555"));
nodeList.add(new NodeBean("666"));
nodeList.add(new NodeBean("777"));
nodeList.add(new NodeBean("888"));
nodeList.add(new NodeBean("999"));
ConsistentHash stringConsistentHash = new ConsistentHash(numberOfReplicas,nodeList);
Map countMap = new HashMap(){
@Override
public Integer put(NodeBean key, Integer value) {
Integer oldVal = this.get(key);
if(oldVal==null){
return super.put(key, value);
}else{
return super.put(key,oldVal+1);
}
}
};
long l = System.currentTimeMillis();
for(int i=0;i<1000000;i++){
NodeBean nodeBean = stringConsistentHash.get(TelUtil.getTel());
countMap.put(nodeBean, 1);
}
System.out.println("1000000计算耗时:"+(System.currentTimeMillis() - l)+"毫秒");
for(NodeBean nb:countMap.keySet()){
System.out.println(nb.toString()+" 分配到个数:"+countMap.get(nb));
}
}
}