隨筆 - 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服務器  業務量加倍" );  
                      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 閱讀(208) 評論(0)  編輯  收藏 所屬分類: J2EE
          主站蜘蛛池模板: 洞头县| 铜山县| 会昌县| 离岛区| 南陵县| 永昌县| 五莲县| 伊宁市| 东城区| 随州市| 佛山市| 岑溪市| 保康县| 汝南县| 普安县| 常宁市| 郯城县| 临安市| 嘉善县| 黑山县| 漠河县| 连山| 德令哈市| 洪雅县| 阜阳市| 米脂县| 偃师市| 邢台县| 赤水市| 孟州市| 鲜城| 扎囊县| 南汇区| 小金县| 塔城市| 高淳县| 青阳县| 咸宁市| 亚东县| 巴中市| 贡觉县|