代码拉取完成,页面将自动刷新
package io.github.consoles.distribution;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.CRC32;
/**
* Created by yiihua-013 on 16/10/22.
* ref http://consoles.github.io/2016/10/18/memcached/
* <p>
* 一致性hash
*/
public class ConsistentHash implements Hash, Distribution {
// private Map<Long, String> nodes = new TreeMap<>();
private Map<Long, String> position = new TreeMap<>();
private static final int MUL = 64; // 每个物理节点有64个虚拟节点
public String lookup(String key) {
long point = this.hash(key); // 算出key的落点
// 找出落点所在的区间
String node = (String) position.values().toArray()[0];
for (Map.Entry<Long, String> entry : position.entrySet()) {
if (point <= entry.getKey()) {
node = entry.getValue();
break;
}
}
return node;
}
public long hash(String str) {
CRC32 crc32 = new CRC32();
crc32.update(str.getBytes());
long ret = crc32.getValue();
return ret > 0 ? ret : -ret;
}
/**
* 加入1个物理节点相当于加入了64个虚拟节点
*
* @param node,例如:192.168.1.1
*/
public void addNode(String node) {
for (int i = 0; i < MUL; i++) {
String vNodeName = "v_node_" + node + "_" + i;
this.position.put(hash(vNodeName), node); // 每64个虚拟节点对应一个物理节点
}
// long pos = this.hash(node); // 根据节点名计算出在圆环上的位置
// this.nodes.put(pos, node); // 13亿=>"192.168.1.1"则表示13亿这个位置上有一个真实节点为192.168.1.1
}
/**
* 删除一个物理节点需要删除64个虚拟节点
*/
public void deleteNode(String node) {
Iterator<Map.Entry<Long, String>> iterator = position.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Long, String> entry = iterator.next();
if (entry.getValue().equals(node)) {
// position.remove(entry.getKey());
iterator.remove();
}
}
// 以下代码将抛出并发修改异常
// for (Map.Entry<Long,String> entry:position.entrySet()){
// if (entry.getValue().equals(node)) {
// position.remove(entry.getKey());
// }
// }
}
// public void printNodes() {
// for (Map.Entry<Long, String> entry : nodes.entrySet()) {
// System.out.println(entry.getKey() + "=>" + entry.getValue());
// }
// }
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。