美文网首页
负载均衡算法

负载均衡算法

作者: 金煜博 | 来源:发表于2021-06-22 14:48 被阅读0次

1.什么是负载均衡

对系统中的负载情况进行调整,尽量减小系统中各个节点负载不均衡的情况

2.负载均衡的算法

轮询:按照某种算法进行顺序触发,适合服务器配置相差不大的情况
权重:按照分配的比例大小进行触发,适合服务器配置相差较大的情况
ip取模:按照不同的ip访问指定的服务地址
随机:随机指定访问的服务地址
随机权重:随机权重指定访问地址

3.示例代码

1)创建负载均衡接口

public interface LoadBalancer<T> {
    String getServiceAddres(List<T> addr);
}

2)创建轮询类

通过原子类与集合长度取余来获取list集合中指定的服务地址

public class Polling implements LoadBalancer<String> {
    private AtomicInteger atomicLong = new AtomicInteger(0);

    public String getServiceAddres(List<String> addr) {
        Integer index = atomicLong.incrementAndGet() % addr.size();
        String address = addr.get(index);
        return address;
    }
}

3)创建权重类

通过权重的次数添加服务地址,在使用原子类与集合长度取余获取list集合的服务地址

public class Weight implements LoadBalancer<WeightEntity> {
    private List<String> listAddr = new ArrayList<String>();
    private AtomicInteger atomicLong = new AtomicInteger(0);
    public String getServiceAddres(List<WeightEntity> addr) {
        for (int i = 0; i < addr.size(); i++) {
            WeightEntity wa = addr.get(i);
            for (int j = 0; j < wa.getWeight(); j++) {
                listAddr.add(addr.get(i).getAddress());
            }
        }
        Integer index = atomicLong.incrementAndGet() % listAddr.size();
        String address = listAddr.get(index);
        return address;
    }
}

4)创建ip取模类

通过ip获取hashcode值与集合长度取余

public class IpHash {
    public String getAddress(List<String> addr, String ipAddres) {
        int index = ipAddres.hashCode() % addr.size();
        String value = addr.get(index);
        return value;
    }
}

5)创建客户端类

public class Client {
    public static void main(String[] args) {
        System.out.println("--------------------------轮询算法-------------------------------------");
        List<String> address = new ArrayList<String>();
        address.add("192.168.0.100:8080");
        address.add("192.168.0.100:8081");
        address.add("192.168.0.100:8082");
        Polling polling = new Polling();
        System.out.println(polling.getServiceAddres(address));
        System.out.println(polling.getServiceAddres(address));
        System.out.println(polling.getServiceAddres(address));

        System.out.println("--------------------------权重算法-------------------------------------");
        LoadBalancer loadBalancer = new Weight();
        List<WeightEntity> listWe = new ArrayList<WeightEntity>();
        listWe.add(new WeightEntity("192.168.0.100:8080", 1));
        listWe.add(new WeightEntity("192.168.0.100:8081", 2));
        System.out.println(loadBalancer.getServiceAddres(listWe));
        System.out.println(loadBalancer.getServiceAddres(listWe));
        System.out.println(loadBalancer.getServiceAddres(listWe));


        System.out.println("--------------------------ip取模算法-------------------------------------");
        IpHash ipHash = new IpHash();
        List<String> listStrAddr = new ArrayList<String>();
        listStrAddr.add("192.168.100.1:8081");
        listStrAddr.add("192.168.100.1:8082");
        System.out.println(ipHash.getAddress(listStrAddr, "192.168.1.1"));
        System.out.println(ipHash.getAddress(listStrAddr, "192.168.1.2"));

    }
}

6)运行结果

图片.png

相关文章

网友评论

      本文标题:负载均衡算法

      本文链接:https://www.haomeiwen.com/subject/brapyltx.html