隨筆 - 100  文章 - 50  trackbacks - 0
          <2017年9月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          我收藏的一些文章!

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          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服務器  業(yè)務量加倍" );  
                      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() + "個" );  
                      }  
                }  
            
          posted on 2017-09-26 00:20 fly 閱讀(205) 評論(0)  編輯  收藏 所屬分類: J2EE
          主站蜘蛛池模板: 龙江县| 乐亭县| 黑水县| 府谷县| 洛南县| 游戏| 鸡西市| 吴江市| 固镇县| 永川市| 东阿县| 六安市| 鸡西市| 阿克苏市| 南投县| 格尔木市| 布尔津县| 余庆县| 铜山县| 咸阳市| 广水市| 湘西| 钟祥市| 合水县| 黎城县| 宜城市| 宜宾县| 湘西| 汾阳市| 海林市| 什邡市| 黄陵县| 南皮县| 宁城县| 蛟河市| 阿巴嘎旗| 崇礼县| 巴林左旗| 赤城县| 景泰县| 盐池县|