一致性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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值