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());}
结果: