一致性hash算法的java实现
程序员文章站
2024-03-19 22:31:58
...
一致性hash算法的java实现
分布式场景中对数据的均匀分布可以通过一致性hash环来解决,对与一致性hash环的具体介绍网上有很多博客写得很详细,我这里主要讲代码实现,将节点数据抽象为Node类,将一致性hash环抽象为HashCircle类,具体代码如下:
Node.java
import java.util.ArrayList;
import java.util.List;
public class Node {
private List<Integer> partition;
private String name;
Node(String name)
{
setPartition(new ArrayList<Integer>());
this.setName(name);
}
public void addPartition(int p)
{
partition.add(p);
}
public void showNode()
{
System.out.println("name: "+name);
System.out.println("partition: "+partition.toString());
}
public List<Integer> getPartition() {
return partition;
}
public void setPartition(List<Integer> partition) {
this.partition = partition;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HashCircle.java
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
public class HashCircle {
public static void main(String[] args) throws NoSuchAlgorithmException {
List<Node> nodes = new ArrayList<Node>();
TreeMap<Long, Node> dht = new TreeMap<Long, Node>();
int virsulNode = 150;
//初始化10个Node节点
for (int i=0; i<10; i++)
{
nodes.add(new Node("node"+i));
}
//将Node节点和虚拟节点放入hash环
for (int i=0; i<nodes.size(); i++)
{
Node node = nodes.get(i);
for (int j=0; j<virsulNode; j++)
{
dht.put(hash(computeMd5("node"+i+j), 2), node);
}
}
//将200个分区存入hash环
for (int i=0; i<200; i++)
{
Integer partition = i;
Node node;
SortedMap<Long, Node> map = dht.tailMap(hash(computeMd5("partition"+i), 2));
if (map.isEmpty())
node = nodes.get(0);
else
node = dht.get(map.firstKey());
node.addPartition(partition);
}
for (int i=0; i<nodes.size(); i++)
{
nodes.get(i).showNode();
}
}
/**
* 计算MD5值
*/
public static byte[] computeMd5(String k) {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 not supported", e);
}
md5.reset();
byte[] keyBytes = null;
try {
keyBytes = k.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unknown string :" + k, e);
}
md5.update(keyBytes);
return md5.digest();
}
/**
* 根据2^32把节点分布到环上面
*
* @param digest
* @param nTime
* @return
*/
public static long hash(byte[] digest, int nTime) {
long rv = ((long) (digest[3 + nTime * 4] & 0xFF) << 24)
| ((long) (digest[2 + nTime * 4] & 0xFF) << 16)
| ((long) (digest[1 + nTime * 4] & 0xFF) << 8)
| (digest[0 + nTime * 4] & 0xFF);
return rv & 0xffffffffL; /* Truncate to 32-bits */
}
}
上一篇: 一致性hash环算法java实现
下一篇: django之session的使用