zoukankan      html  css  js  c++  java
  • 一致性hash算法(JAVA)

     本文来自http://www.iteye.com/topic/1132274,版权原作者所有

    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() + "个" ); 
                } 
          } 
     

  • 相关阅读:
    跨域(cross-domain)访问 cookie (读取和设置)
    实用的PHP正则表达式
    Leetcode:find_minimum_in_rotated_sorted_array
    spring Jdbc自己主动获取主键。
    《学习opencv》笔记——矩阵和图像操作——cvSetIdentity,cvSolve,cvSplit,cvSub,cvSubS and cvSubRS
    HTML5 input placeholder 颜色 改动
    Java面试宝典2014版
    Go语言 关于go error处理风格的一些讨论和个人观点(上)
    动静结合学内核:linux idle进程和init进程浅析
    【Bootstrap3.0建站笔记二】button可下拉弹出层
  • 原文地址:https://www.cnblogs.com/weiwelcome0/p/ConsistentHash.html
Copyright © 2011-2022 走看看