<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 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 閱讀(204) 評論(0)  編輯  收藏 所屬分類: J2EE
    主站蜘蛛池模板: 亚洲国产成人精品女人久久久| 日产亚洲一区二区三区| 亚洲va在线va天堂va手机| 24小时免费看片| 亚洲伦理中文字幕| 国内免费高清在线观看| 亚洲伊人久久大香线蕉AV| 亚欧在线精品免费观看一区 | 精品免费视在线观看| 亚洲老妈激情一区二区三区| 成人妇女免费播放久久久| 亚洲成色在线综合网站| 免费人成在线观看视频高潮| 亚洲AV成人一区二区三区AV| 日韩中文字幕免费视频| 亚洲乱码在线播放| 免费无码成人AV片在线在线播放| 久久久久久久尹人综合网亚洲| 亚洲中文字幕久在线| 夜夜爽免费888视频| 日本特黄特色AAA大片免费| 伊人久久亚洲综合| 亚洲免费在线观看视频| 亚洲成a人无码亚洲成av无码| a级成人毛片免费图片| 亚洲激情视频在线观看| 国产在线观看片a免费观看| 日韩色视频一区二区三区亚洲| 久久爰www免费人成| 亚洲成人免费网址| 免费一级e一片在线播放| 国产拍拍拍无码视频免费| wwwxxx亚洲| 国产亚洲美女精品久久久2020| 亚洲AV无码一区二区三区鸳鸯影院| 最近免费中文字幕大全高清大全1| 亚洲熟妇无码AV在线播放| 免费观看无遮挡www的小视频| 亚洲国产成人片在线观看| 日韩av无码成人无码免费| 一个人免费观看www视频|