日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

1、完全隨機算法

缺點:所有服務器的訪問概率都是相同的。

package com.example.demo.core.random;
 
import JAVA.util.Arrays;
import java.util.List;
import java.util.Random;
 
/**
 * 負載均衡算法
 * 完全隨機算法
 */
public class RandomServer {
 
    public static List<String> list = Arrays.asList("10.180.11.126:8888","10.180.11.128:8888","10.180.11.130:8888");
 
    static Random random = new Random();
 
    public static String getServer() {
        int number = random.nextInt(list.size());
        return list.get(number);
    }
 
    public static void main(String[] args) {
        for(int i = 0; i < 15; i++) {
            System.out.println(getServer());
        }
    }
}

2、加權隨機算法

場景:有的服務器性能高,可以讓隨機到此服務器的可能性增大

缺點:權重低的服務器可能很長一段時間都訪問不到3

package com.example.demo.core.random;
 
import java.util.*;
 
/**
 * 負載均衡算法
 *
 * 如果某一臺服務器性能比較高,設置訪問的權重高一點
 *
 * 加權隨機算法
 */
public class WeightRandomServer {
 
    public static Map<String,Integer> map = new HashMap<>();
 
    static {
        map.put("10.180.11.126:8888",2);
        map.put("10.180.11.128:8888",7);
        map.put("10.180.11.130:8888",1);
    }
 
    static Random random = new Random();
 
    /**
     * 當權重設置過大時,list容易被撐爆
     * @return
     */
    public static String getServer() {
 
        List<String> list = new ArrayList<>();
 
        for(Map.Entry<String,Integer> entry: map.entrySet()) {
 
            //根據權重,決定向list中添加幾次
            for(int i = 0; i < entry.getValue(); i++) {
 
                list.add(entry.getKey());
            }
        }
        //list的大小
        int weight = map.values().stream().mapToInt(p -> p).sum();
 
        int number = random.nextInt(weight);
 
        return list.get(number);
    }
 
 
    /**
     * 優化后
     * @return
     */
    public static String getServer1() {
        //計算總權值
        int weight = map.values().stream().mapToInt(p -> p).sum();
 
        //隨機一個隨機數
        int index = random.nextInt(weight);
 
        //遍歷  服務  map
        for(Map.Entry<String,Integer> entry : map.entrySet()) {
            //如果權重大于  索引
            if(entry.getValue() >= index) {
                // 返回這個服務
                return entry.getKey();
            }
            //否則,索引 = 當前索引 - 當前服務的權重
            index = index - entry.getValue();
        }
        return "";
    }
 
    public static void main(String[] args) {
 
        for(int i = 0; i < 15; i++) {
 
            //System.out.println(getServer());
            System.out.println(getServer1());
        }
    }
}

3、完全輪詢算法

缺點:從頭到尾輪詢一遍,不能根據服務器性能設置權重

package com.example.demo.core.poll;
 
import java.util.Arrays;
import java.util.List;
 
/**
 * 完全輪詢算法
 */
public class PollServer {
    public static List<String> list = Arrays.asList("10.180.11.126:8888","10.180.11.128:8888","10.180.11.130:8888");
 
    static int index;
 
    public static String getServer() {
        if(index == list.size()) {
            index = 0;
        }
        return list.get(index++);
    }
 
    public static void main(String[] args) {
 
        for(int i = 0; i < 15; i++) {
 
            System.out.println(getServer());
        }
    }
}

4、加權輪詢算法

有點:可以根據服務器性能設置訪問權重

缺點:可能某個服務器權重大,長時間執行,遇到耗時大的請求,壓力會很大

package com.example.demo.core.poll;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 加權輪詢算法
 * 實際中可能遇到某個服務器壓力較大,長時間執行。
 */
public class WeightPollServer {
 
    public static Map<String,Integer> map = new HashMap<>();
 
    static {
        map.put("10.180.11.126:8888",2);
        map.put("10.180.11.128:8888",7);
        map.put("10.180.11.130:8888",5);
    }
 
    static int index;
 
    public static String getServer() {
        int weight = map.values().stream().mapToInt( p -> p).sum();
        int number = (index++) % weight;
        for(Map.Entry<String,Integer> entry : map.entrySet()) {
            if(entry.getValue() >= number) {
                return entry.getKey();
            }
            number = number - entry.getValue();
        }
        return "";
    }
 
    public static void main(String[] args) {
 
        for(int i = 0; i < 15; i++) {
            System.out.println(getServer());
        }
    }
}

5、平滑加權輪詢算法

優點:根據權重分配服務,同時又保證權重低的服務可以被訪問到

缺點:集群環境下,同一個用戶訪問無法分流到固定一臺機器

package com.example.demo.core.smooth;
 
/**
 * 平滑加權
 */
public class SmoothWeight {
 
    private int weight;
 
    private int currentWeight;
 
    private String address;
 
 
    public int getWeight() {
        return weight;
    }
 
    public void setWeight(int weight) {
        this.weight = weight;
    }
 
    public int getCurrentWeight() {
        return currentWeight;
    }
 
    public void setCurrentWeight(int currentWeight) {
        this.currentWeight = currentWeight;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    public SmoothWeight(int weight, int currentWeight, String address) {
        this.weight = weight;
        this.currentWeight = currentWeight;
        this.address = address;
    }
}
package com.example.demo.core.smooth;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 平滑加權輪詢算法
 */
public class SmoothWeightPollServer {
 
 
    public static Map<String,SmoothWeight> map = new HashMap<>();
 
    static {
        map.put("10.180.11.126:8888",new SmoothWeight(5,5,"10.180.11.126:8888"));
        map.put("10.180.11.128:8888",new SmoothWeight(2,2,"10.180.11.128:8888"));
        map.put("10.180.11.130:8888",new SmoothWeight(4,4,"10.180.11.130:8888"));
    }
 
    public static String getServer() {
 
        SmoothWeight maxSmoothWeight = null;
 
        int weight = map.values().stream().mapToInt(SmoothWeight :: getWeight).sum();
 
        for(Map.Entry<String,SmoothWeight> entry : map.entrySet()) {
 
            SmoothWeight currentSmoothWeight = entry.getValue();
 
            if(maxSmoothWeight == null || currentSmoothWeight.getCurrentWeight() > maxSmoothWeight.getCurrentWeight()) {
                maxSmoothWeight = currentSmoothWeight;
            }
        }
        assert maxSmoothWeight != null;
        maxSmoothWeight.setCurrentWeight(maxSmoothWeight.getCurrentWeight() - weight);
        for(Map.Entry<String,SmoothWeight> entry : map.entrySet()) {
 
            SmoothWeight currentSmoothWeight = entry.getValue();
 
            currentSmoothWeight.setCurrentWeight(currentSmoothWeight.getCurrentWeight() + currentSmoothWeight.getWeight());
        }
 
        return maxSmoothWeight.getAddress();
    }
 
 
    public static void main(String[] args) {
 
        for(int i = 0; i < 15; i++) {
            System.out.println(getServer());
        }
    }
}

6、哈希負載算法

package com.example.demo.core.hash;
 
import java.util.Arrays;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
 
/**
 * hash負載算法
 * 在一個集群環境下,讓同一個用戶的訪問,分流到固定的一臺機器上
 */
public class HashServer {
 
    public static List<String> list = Arrays.asList("10.180.11.126:8888","10.180.11.128:8888","10.180.11.130:8888");
 
    public static String getServer(String client){
        int nodeCount = 40;
 
        TreeMap<Integer,String> treeMap = new TreeMap<>();
 
        for(String s : list) {
            for(int i = 0; i < nodeCount; i++) {
                treeMap.put((s + "address:" + i).hashCode(), s);
            }
        }
 
        SortedMap<Integer,String> sortedMap = treeMap.tailMap(client.hashCode());
 
        Integer firstHash = (sortedMap.size() > 0) ? sortedMap.firstKey() : treeMap.firstKey();
 
        return treeMap.get(firstHash);
    }
 
    public static void main(String[] args) {
 
        for(int i = 0; i < 100; i++) {
            System.out.println(getServer("用戶:" + i + "訪問"));
        }
    }
 
}

分享到:
標簽:負載均衡
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定