隨筆 - 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服務(wù)器  業(yè)務(wù)量加倍" );  
                      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("服務(wù)器 " + m.getKey() + "----" + m.getValue() + "個" );  
                      }  
                }  
            
          posted on 2017-09-26 00:20 fly 閱讀(205) 評論(0)  編輯  收藏 所屬分類: J2EE
          主站蜘蛛池模板: 榆社县| 辽阳县| 宿迁市| 伊川县| 黔西县| 滦南县| 林西县| 法库县| 孝义市| 秦安县| 平谷区| 和田县| 喜德县| 白城市| 卢龙县| 凤冈县| 泰州市| 北京市| 兴山县| 准格尔旗| 苍溪县| 铜山县| 曲松县| 抚州市| 宣恩县| 墨竹工卡县| 社旗县| 河池市| 揭西县| 鄂温| 乳源| 仪征市| 黑河市| 临湘市| 巢湖市| 建平县| 巴彦淖尔市| 安泽县| 三原县| 瑞昌市| 庆阳市|