91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java項目中如何實現負載均衡

發布時間:2020-11-09 17:37:56 來源:億速云 閱讀:284 作者:Leah 欄目:編程語言

這篇文章運用簡單易懂的例子給大家介紹Java項目中如何實現負載均衡,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

輪詢:

package class2.zookeeper.loadbalance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 負載均衡算法,輪詢法
 * @author guoy
 *
 */
public class TestRoundRobin {

  
  static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();

   static{
    serverWeigthMap.put("192.168.1.12", 1);
    serverWeigthMap.put("192.168.1.13", 1);
    serverWeigthMap.put("192.168.1.14", 2);
    serverWeigthMap.put("192.168.1.15", 2);
    serverWeigthMap.put("192.168.1.16", 3);
    serverWeigthMap.put("192.168.1.17", 3);
    serverWeigthMap.put("192.168.1.18", 1);
    serverWeigthMap.put("192.168.1.19", 2);
  }
   Integer pos = 0;
   public String roundRobin()
    {
      //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
      Map<String,Integer> serverMap = new HashMap<String,Integer>();
      serverMap.putAll(serverWeigthMap);
      //獲取ip列表list
      Set<String> keySet = serverMap.keySet();
      ArrayList<String> keyList = new ArrayList<String>();
      keyList.addAll(keySet);
      
      String server = null;
      
      synchronized (pos) {
        if(pos >=keySet.size()){
          pos = 0;
        }
        server = keyList.get(pos);
        pos ++;
      }
      return server;
    }
    
    public static void main(String[] args) {
      TestRoundRobin robin = new TestRoundRobin();
      for (int i = 0; i < 20; i++) {
        String serverIp = robin.roundRobin();
        System.out.println(serverIp);
      }
    }
}

加權輪詢:

package class2.zookeeper.loadbalance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * 加權隨機載均衡算法
 * @author guoy
 *
 */
public class TestWeightRandom {
  
  static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();

   static{
    serverWeigthMap.put("192.168.1.12", 1);
    serverWeigthMap.put("192.168.1.13", 1);
    serverWeigthMap.put("192.168.1.14", 2);
    serverWeigthMap.put("192.168.1.15", 2);
    serverWeigthMap.put("192.168.1.16", 3);
    serverWeigthMap.put("192.168.1.17", 3);
    serverWeigthMap.put("192.168.1.18", 1);
    serverWeigthMap.put("192.168.1.19", 2);
  }

  public static String weightRandom()
  {
    //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
    Map<String,Integer> serverMap = new HashMap<String,Integer>();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set<String> keySet = serverMap.keySet();
    Iterator<String> it = keySet.iterator();

    List<String> serverList = new ArrayList<String>();

    while (it.hasNext()) {
      String server = it.next();
      Integer weight = serverMap.get(server);
      for (int i = 0; i < weight; i++) {
        serverList.add(server);
      }
    }    
    Random random = new Random();
    int randomPos = random.nextInt(serverList.size());
    
    String server = serverList.get(randomPos);
    return server;
  }
  
  public static void main(String[] args) {
    String serverIp = weightRandom();
    System.out.println(serverIp);
  }
}

隨機:

package class2.zookeeper.loadbalance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * 隨機負載均衡算法
 * @author guoy
 *
 */
public class TestRandom {
  
  static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();

   static{
    serverWeigthMap.put("192.168.1.12", 1);
    serverWeigthMap.put("192.168.1.13", 1);
    serverWeigthMap.put("192.168.1.14", 2);
    serverWeigthMap.put("192.168.1.15", 2);
    serverWeigthMap.put("192.168.1.16", 3);
    serverWeigthMap.put("192.168.1.17", 3);
    serverWeigthMap.put("192.168.1.18", 1);
    serverWeigthMap.put("192.168.1.19", 2);
  }

  public static String random()
  {
    //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
    Map<String,Integer> serverMap = new HashMap<String,Integer>();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set<String> keySet = serverMap.keySet();
    ArrayList<String> keyList = new ArrayList<String>();
    keyList.addAll(keySet);
    
    Random random = new Random();
    int randomPos = random.nextInt(keyList.size());
    
    String server = keyList.get(randomPos);
    return server;
  }
  
  public static void main(String[] args) {
    String serverIp = random();
    System.out.println(serverIp);
  }
}

加權隨機:

package class2.zookeeper.loadbalance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * 加權隨機載均衡算法
 * @author guoy
 *
 */
public class TestWeightRandom {
  
  static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();

   static{
    serverWeigthMap.put("192.168.1.12", 1);
    serverWeigthMap.put("192.168.1.13", 1);
    serverWeigthMap.put("192.168.1.14", 2);
    serverWeigthMap.put("192.168.1.15", 2);
    serverWeigthMap.put("192.168.1.16", 3);
    serverWeigthMap.put("192.168.1.17", 3);
    serverWeigthMap.put("192.168.1.18", 1);
    serverWeigthMap.put("192.168.1.19", 2);
  }

  public static String weightRandom()
  {
    //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
    Map<String,Integer> serverMap = new HashMap<String,Integer>();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set<String> keySet = serverMap.keySet();
    Iterator<String> it = keySet.iterator();

    List<String> serverList = new ArrayList<String>();

    while (it.hasNext()) {
      String server = it.next();
      Integer weight = serverMap.get(server);
      for (int i = 0; i < weight; i++) {
        serverList.add(server);
      }
    }    
    Random random = new Random();
    int randomPos = random.nextInt(serverList.size());
    
    String server = serverList.get(randomPos);
    return server;
  }
  
  public static void main(String[] args) {
    String serverIp = weightRandom();
    System.out.println(serverIp);
  }
}

ip hash:

package class2.zookeeper.loadbalance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 負載均衡 ip_hash算法
 * @author guoy
 *
 */
public class TestIpHash {

  
  static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();

   static{
    serverWeigthMap.put("192.168.1.12", 1);
    serverWeigthMap.put("192.168.1.13", 1);
    serverWeigthMap.put("192.168.1.14", 2);
    serverWeigthMap.put("192.168.1.15", 2);
    serverWeigthMap.put("192.168.1.16", 3);
    serverWeigthMap.put("192.168.1.17", 3);
    serverWeigthMap.put("192.168.1.18", 1);
    serverWeigthMap.put("192.168.1.19", 2);
  }

   /**
   * 獲取請求服務器地址
   * @param remoteIp 負載均衡服務器ip
   * @return
   */
  public static String ipHash(String remoteIp)
  {
    //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
    Map<String,Integer> serverMap = new HashMap<String,Integer>();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set<String> keySet = serverMap.keySet();
    ArrayList<String> keyList = new ArrayList<String>();
    keyList.addAll(keySet);
    
    int hashCode =remoteIp.hashCode();
    int serverListSize = keyList.size();
    int serverPos = hashCode % serverListSize;
    
    return keyList.get(serverPos);
  }
  
  public static void main(String[] args) {
    String serverIp = ipHash("192.168.1.12");
    System.out.println(serverIp);
  }

}

關于Java項目中如何實現負載均衡就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

蓬溪县| 高安市| 锦州市| 巩留县| 红河县| 平乐县| 眉山市| 旬阳县| 磐石市| 昆明市| 宜兴市| 邵阳市| 孟连| 竹溪县| 洛阳市| 武功县| 呼和浩特市| 德化县| 巧家县| 汉沽区| 新民市| 内乡县| 河曲县| 山东省| 彝良县| 始兴县| 邵武市| 温宿县| 崇仁县| 仙桃市| 视频| 衡阳县| 盐城市| 邯郸市| 贺兰县| 共和县| 镶黄旗| 浑源县| 两当县| 福安市| 秀山|