Java项目中如何实现负载均衡-成都快上网建站

Java项目中如何实现负载均衡

这篇文章运用简单易懂的例子给大家介绍Java项目中如何实现负载均衡,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

创新互联建站是专业的凭祥网站建设公司,凭祥接单;提供做网站、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行凭祥网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

轮询:

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 serverWeigthMap = new HashMap();

   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 serverMap = new HashMap();
      serverMap.putAll(serverWeigthMap);
      //獲取ip列表list
      Set keySet = serverMap.keySet();
      ArrayList keyList = new ArrayList();
      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 serverWeigthMap = new HashMap();

   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 serverMap = new HashMap();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set keySet = serverMap.keySet();
    Iterator it = keySet.iterator();

    List serverList = new ArrayList();

    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 serverWeigthMap = new HashMap();

   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 serverMap = new HashMap();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set keySet = serverMap.keySet();
    ArrayList keyList = new ArrayList();
    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 serverWeigthMap = new HashMap();

   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 serverMap = new HashMap();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set keySet = serverMap.keySet();
    Iterator it = keySet.iterator();

    List serverList = new ArrayList();

    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 serverWeigthMap = new HashMap();

   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 serverMap = new HashMap();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set keySet = serverMap.keySet();
    ArrayList keyList = new ArrayList();
    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项目中如何实现负载均衡
文章地址:http://kswjz.com/article/jhsjdo.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流