欢迎使用CSDN-markdown编辑器
来源:互联网 发布:锤子bigbang软件 编辑:程序博客网 时间:2024/05/23 18:32
一致性hash算法的原理
Java代码 public class ConsistentHash<T> { private final HashFunction hashFunction; private final int numberOfReplicas; private final SortedMap<Integer, T> circle = new TreeMap<Integer, T>(); public ConsistentHash(HashFunction hashFunction, int numberOfReplicas, Collection<T> nodes) { this .hashFunction = hashFunction; this .numberOfReplicas = numberOfReplicas; for (T node : nodes) { add(node); } } public void add(T node) { for (int i = 0; i < numberOfReplicas; i++) { circle .put(hashFunction .hash(node.toString() + i), node); } } public void remove(T node) { for (int i = 0; i < numberOfReplicas; i++) { circle .remove(hashFunction .hash(node.toString() + i)); } } public T get(Object key) { if (circle .isEmpty()) { return null ; } int hash = hashFunction .hash(key); // System.out.println("hash---: " + hash); if (!circle .containsKey(hash)) { SortedMap<Integer, T> tailMap = circle .tailMap(hash); hash = tailMap.isEmpty() ? circle .firstKey() : tailMap.firstKey(); } // System.out.println("hash---: " + hash); return circle .get(hash); } static class HashFunction { int hash(Object key) { //md5加密后,hashcode return Md5Encrypt.md5(key.toString()).hashCode(); } } public static void main(String [] args) { HashSet< String> set = new HashSet< String>(); set.add( "A" ); set.add( "B" ); set.add( "C" ); set.add( "D" ); Map< String, Integer> map = new HashMap< String, Integer>(); ConsistentHash< String> consistentHash = new ConsistentHash<String>( new HashFunction(), 1000, set); int count = 10000; for (int i = 0; i < count; i++) { String key = consistentHash.get(i); if (map.containsKey(key)) { map.put(consistentHash.get(i), map.get(key) + 1); } else { map.put(consistentHash.get(i), 1); } // System.out.println(key); } showServer(map); map.clear(); consistentHash.remove( "A" ); System. out .println("------- remove A" ); for (int i = 0; i < count; i++) { String key = consistentHash.get(i); if (map.containsKey(key)) { map.put(consistentHash.get(i), map.get(key) + 1); } else { map.put(consistentHash.get(i), 1); } // System.out.println(key); } showServer(map); map.clear(); consistentHash.add( "E" ); System. out .println("------- add E" ); for (int i = 0; i < count; i++) { String key = consistentHash.get(i); if (map.containsKey(key)) { map.put(consistentHash.get(i), map.get(key) + 1); } else { map.put(consistentHash.get(i), 1); } // System.out.println(key); } showServer(map); map.clear(); consistentHash.add( "F" ); System. out .println("------- add F服务器 业务量加倍" ); count = count * 2; for (int i = 0; i < count; i++) { String key = consistentHash.get(i); if (map.containsKey(key)) { map.put(consistentHash.get(i), map.get(key) + 1); } else { map.put(consistentHash.get(i), 1); } // System.out.println(key); } showServer(map); } public static void showServer(Map<String , Integer> map) { for (Entry<String, Integer> m : map.entrySet()) { System. out .println("服务器 " + m.getKey() + "----" + m.getValue() + "个" ); } } }
0 0
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 实现一个工具类,可以把HBase任意表的任意多的列导出到任意指定的HDFS中
- Android设计支持库(Android Design Support Library)
- 使用JBoss 5.1.0.GA部署项目可能遇到的问题汇总
- SignalTap Ⅱ使用步骤整理
- CoreLocation框架定位详解
- 欢迎使用CSDN-markdown编辑器
- 动态规划 HDOJ2602-Bone Collector-01背包
- 线程和进程的区别是什么?
- 2015 ACM/ICPC Asia Regional Shenyang Online
- linux 命令 system () 函数详解
- 面试相关之链表
- LeetCode (54) Valid Anagram
- c++primer plus第十三章-抽象基类、纯虚函数及本章总结
- sicily 1021 couples