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

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());

    }

结果:

相关推荐
空中湖6 小时前
Spring AI RAG 完整实战:从零搭建企业知识库问答系统
java·人工智能·spring
Ai拆代码的曹操6 小时前
K8s Pod Pending 逐层排查:从 FailedScheduling 到 PVC StorageClass 不存在的实战记录
java·linux·kubernetes
什巳6 小时前
JAVA练习306- 翻转二叉树
java·数据结构·算法·leetcode
wangbing11257 小时前
JPA下自定义主键
java·linux·服务器
plainGeekDev8 小时前
NullPointerException → Kotlin 空安全
android·java·kotlin
想做小南娘,发现自己是女生喵9 小时前
第 2 章 顺序表和 vector
java·数据结构·算法
CHANG_THE_WORLD9 小时前
6.多线程的TCP通信
java·网络·tcp/ip
sun032210 小时前
【笔记】Spring MVC 相关介绍
java·spring·mvc
兰令水10 小时前
hot100【acm版】【2026.7.18打卡-java版本】
java·开发语言·算法
CoderYanger11 小时前
A.每日一题:3020. 子集中元素的最大数量
java·程序人生·算法·leetcode·面试·职场和发展·学习方法