<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
    主站蜘蛛池模板: 中文字幕亚洲不卡在线亚瑟| 在线观看人成视频免费| 亚洲人成中文字幕在线观看| 亚洲AV成人无码网天堂| 午夜一区二区免费视频| 亚洲色偷偷色噜噜狠狠99| 成年女人男人免费视频播放| 亚洲国产91在线| 在线看片无码永久免费视频| 国产精品亚洲自在线播放页码| 99热这里只有精品6免费| 亚洲视频一区二区在线观看| 亚洲视频在线观看免费| 亚洲视频免费观看| 在线观看AV片永久免费| 亚洲精品无码成人| 免费二级毛片免费完整视频| free哆拍拍免费永久视频| 中文字幕无码精品亚洲资源网| 中文精品人人永久免费 | 一区二区三区免费在线观看| 亚洲精品无码av天堂| 久久精品无码免费不卡| 亚洲电影一区二区三区| 0588影视手机免费看片| 亚洲日韩av无码中文| 国产一级做a爱免费视频| a在线观看免费视频| 亚洲欧洲日产国码www| 国产免费啪嗒啪嗒视频看看| 中文字幕永久免费| 内射干少妇亚洲69XXX| 毛片免费在线视频| 九九免费精品视频在这里| 亚洲AV人无码激艳猛片| 18禁超污无遮挡无码免费网站国产| 深夜A级毛片视频免费| 亚洲国产成人私人影院| 日本免费一区尤物| 久久国产精品萌白酱免费| 亚洲欧洲国产综合AV无码久久|