欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

详解Java实现负载均衡的几种算法代码

时间:2020-11-20 17:24:33|栏目:JAVA代码|点击:

本篇文章主要介绍Java实现负载均衡的几种算法,具体如下:

轮询:

package class2.zookeeper.loadbalance;

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

/**
 * ??d均衡算法,??法
 * @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,避免出?F由於服?掌魃暇?和下??е碌?K?l???}
      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;

/**
 * 加?嚯S?C载均衡算法
 * @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,避免出?F由於服?掌魃暇?和下??е碌?K?l???}
    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;

/**
 * ?S?C负载均衡算法
 * @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,避免出?F由於服?掌魃暇?和下??е碌?K?l???}
    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;

/**
 * 加?嚯S?C载均衡算法
 * @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,避免出?F由於服?掌魃暇?和下??е碌?K?l???}
    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,避免出?F由於服?掌魃暇?和下??е碌?K?l???}
    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对象类型的判断详解

栏    目:JAVA代码

下一篇:解析Java图形化编程中的文本框和文本区

本文标题:详解Java实现负载均衡的几种算法代码

本文地址:http://www.codeinn.net/misctech/24337.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有