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

1、描述

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

2、使用环形链表

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

java 复制代码
// 节点定义
@Data
class Node {

    Node next;

    String name;

    String ip;

}
java 复制代码
// 环形状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;
    }

}
java 复制代码
// 测试验证
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 亿。(如果你的请求次数即【负载均衡调用次数】会超过这个值,那么或许下面的例子会存在问题)

java 复制代码
@Data
class Node2 {

    String name;

    String ip;

    public Node2(String node1, String s) {
        this.name = node1;
        this.ip = s;
    }
}
java 复制代码
/**
 * 基于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()));
    }

}

测试代码

java 复制代码
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());

    }

结果:

相关推荐
聚美智数2 小时前
智能结构化识别-图片结构解析-图像结构化解析-照片结构化解析
android·java·数据库
十年Java程序媛2 小时前
Java枚举Enum完整教程|替代静态常量,开发与面试要点
java·开发语言·spring boot·后端·面试
我是人✓2 小时前
SpringBoot静态资源映射揭秘
java·spring boot·后端
土豆.exe3 小时前
nf_tables 的 verdict 被读了两次:一个十年未闭合的位域错配
java·数据库·网络安全·缓存
小坏讲微服务4 小时前
刚发布一个月的JDK 27,为什么我建议你立刻升级?
java·开发语言·spring boot·spring·架构·求职招聘·springboot4
kunicoding4 小时前
多线程编程核心概念详解:从线程基础到并发安全
java·jvm·安全
snow@li4 小时前
SpringBoot:核心注解全景梳理 / @xxx 叫“注解”(Annotation)
java·spring
devilnumber4 小时前
避坑_IDEA 提取方法(Extract Method)工具
java·ide·intellij-idea
鬼手点金5 小时前
Agent 的学习机制:后训练、上下文学习与外部化学习
java·人工智能·git·学习·自动化·nanogpt
小陈不好吃5 小时前
equals 与 hashCode 的契约关系:从 HashMap 到 Lombok 注解
java·hashmap·lombok