欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-07 21:15:27
本篇文章主要介绍java实现负载均衡的几种算法,具体如下: 轮询: package class2.zookeeper.loadbalance; imp...

本篇文章主要介绍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);
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。