负载均衡-轮询-两种简易实现

1、描述

下游可用的服务器目前有5个(node),设计一个方法,方法没有任何参数,采用轮询的方式返回其中一个node;

2、使用环形链表

每次取下一个node即可。注意:需要保证线程安全!

// 节点定义
@Data
class Node {

    Node next;

    String name;

    String ip;

}
// 环形状node集合
class LoadBalanceCycle {

    private Node head;

    private Node nextNode;

    /**
     * 添加节点
     */
    public synchronized void addNode(String name, String ip) {
        Node newNode = new Node();
        newNode.name = name;
        newNode.ip = ip;

        if (head == null) {
            head = newNode;
            head.next = head;
        } else {
            Node temp = head;
            while (temp.next != head) {
                temp = temp.next;
            }
            temp.next = newNode;
            newNode.next = head;
        }
    }

    /**
     * 获取下一个节点
     */
    public synchronized Node getNextNode() {
        if (nextNode == null) {
            nextNode = head;
        } else {
            nextNode = nextNode.next;
        }
        return nextNode;
    }

}
// 测试验证
public static void main(String[] args) {

        LoadBalanceCycle loadBalanceCycle = new LoadBalanceCycle();
        for (int i = 0; i < 3; i++) {
            // 初始化三个节点的
            loadBalanceCycle.addNode("node1", "192.168.0.1");
            loadBalanceCycle.addNode("node2", "192.168.0.2");
            loadBalanceCycle.addNode("node3", "192.168.0.3");
        }

        AtomicInteger n1Count = new AtomicInteger();
        AtomicInteger n2Count = new AtomicInteger();
        AtomicInteger n3Count = new AtomicInteger();

        CountDownLatch latch = new CountDownLatch(30);

        // 多线程,返回负载均衡中的节点
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                int j = 10;
                while (j > 0) {
                    try {
                        String name = loadBalanceCycle.getNextNode().getName();
                        System.out.println(Thread.currentThread().getName() + " " + name);
                        if ("node1".equals(name)) {
                            n1Count.incrementAndGet();
                        }
                        if ("node2".equals(name)) {
                            n2Count.incrementAndGet();
                        }
                        if ("node3".equals(name)) {
                            n3Count.incrementAndGet();
                        }
                        j--;
                    } finally {
                        latch.countDown();
                    }
                }

            }).start();

        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("==============================负载均衡调用结果===================================");

        System.out.println("node1 被调用的次数: " + n1Count.get());
        System.out.println("node2 被调用的次数: " + n2Count.get());
        System.out.println("node3 被调用的次数: " + n3Count.get());

    }

}

3、使用AtomicLong

long 类型的最大值?

这个值是 2^63 - 1,即 9223372036854775807,是 long 类型能表示的最大正数值。

是多少亿?

Long.MAX_VALUE 大约是 92233.72 亿。(如果你的请求次数即【负载均衡调用次数】会超过这个值,那么或许下面的例子会存在问题)

@Data
class Node2 {

    String name;

    String ip;

    public Node2(String node1, String s) {
        this.name = node1;
        this.ip = s;
    }
}

/**
 * 基于AtomicLong实现的一个结构
 */
class LoadBalanceByAtomic {

    final List<Node2> nodeList = new ArrayList<>();

    AtomicLong mark = new AtomicLong(0);

    public void add(Node2 node) {
        synchronized (nodeList) {
            nodeList.add(node);
        }
    }

    public Node2 getNext() {
        long andIncrement = mark.getAndIncrement();
        return nodeList.get((int) (andIncrement % nodeList.size()));
    }

}

测试代码

public static void main(String[] args) {

        LoadBalanceByAtomic loadBalanceByAtomic = new LoadBalanceByAtomic();
        for (int i = 0; i < 3; i++) {
            // 初始化三个节点的
            loadBalanceByAtomic.add(new Node2("node1", "192.168.0.1"));
            loadBalanceByAtomic.add(new Node2("node2", "192.168.0.2"));
            loadBalanceByAtomic.add(new Node2("node3", "192.168.0.3"));
        }

        AtomicInteger n1Count = new AtomicInteger();
        AtomicInteger n2Count = new AtomicInteger();
        AtomicInteger n3Count = new AtomicInteger();

        CountDownLatch latch = new CountDownLatch(30);

        // 多线程,返回负载均衡中的节点
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                int j = 10;
                while (j > 0) {
                    try {
                        String name = loadBalanceByAtomic.getNext().getName();
                        System.out.println(Thread.currentThread().getName() + " " + name);
                        if ("node1".equals(name)) {
                            n1Count.incrementAndGet();
                        }
                        if ("node2".equals(name)) {
                            n2Count.incrementAndGet();
                        }
                        if ("node3".equals(name)) {
                            n3Count.incrementAndGet();
                        }
                        j--;
                    } finally {
                        latch.countDown();
                    }
                }

            }).start();

        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("==============================负载均衡调用结果===================================");

        System.out.println("node1 被调用的次数: " + n1Count.get());
        System.out.println("node2 被调用的次数: " + n2Count.get());
        System.out.println("node3 被调用的次数: " + n3Count.get());

    }

结果:

相关推荐

  1. 负载均衡逻辑

    2024-07-13 05:12:02       19 阅读
  2. Nginx的负载均衡(加权

    2024-07-13 05:12:02       22 阅读
  3. 负载均衡加权算法

    2024-07-13 05:12:02       28 阅读
  4. 负载均衡规则算法设计题】

    2024-07-13 05:12:02       32 阅读
  5. Python实现简单负载均衡

    2024-07-13 05:12:02       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 05:12:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 05:12:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 05:12:02       58 阅读
  4. Python语言-面向对象

    2024-07-13 05:12:02       69 阅读

热门阅读

  1. 避免 WebSocket 连接被拒绝

    2024-07-13 05:12:02       25 阅读
  2. 小程序需要做等保测评吗?

    2024-07-13 05:12:02       20 阅读
  3. wireshark与tcpdump使用

    2024-07-13 05:12:02       22 阅读
  4. 韩国裸机云大宽带服务器主要特点和优势

    2024-07-13 05:12:02       26 阅读
  5. 【日常bug记录】el-checkbox 绑定对象数组

    2024-07-13 05:12:02       21 阅读
  6. UniVue@v1.3.0版本发布

    2024-07-13 05:12:02       28 阅读
  7. WXML,WXSS和HTML,CSS的区别

    2024-07-13 05:12:02       24 阅读
  8. ODrive学习笔记一:开发环境搭建

    2024-07-13 05:12:02       21 阅读
  9. Eureka 介绍与使用

    2024-07-13 05:12:02       26 阅读
  10. Perl基础入门指南:从零开始掌握Perl编程

    2024-07-13 05:12:02       30 阅读
  11. 认识string(二)详解

    2024-07-13 05:12:02       35 阅读
  12. Ubuntu下安装配置和调优Docker,支持IPV6

    2024-07-13 05:12:02       26 阅读
  13. Vue的生命周期函数有哪些?详细说明

    2024-07-13 05:12:02       26 阅读
  14. SQL面试题(二)

    2024-07-13 05:12:02       26 阅读