Skip to content

Commit ed468a1

Browse files
committed
update 更新笔记代码
1 parent b900f54 commit ed468a1

File tree

7 files changed

+70
-61
lines changed

7 files changed

+70
-61
lines changed

code/loadbalance-java/src/com/caojx/learn/ConsistentHashing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class ConsistentHashing {
1010

1111
public static void main(String[] args) {
12-
String[] requestIps = {"192.168.0.15", "192.168.0.30", "192.168.0.45"};
12+
String[] requestIps = {"192.168.0.8", "192.168.0.18", "192.168.0.28"};
1313
for(String requestIp: requestIps){
1414
String address = getServer(requestIp);
1515
System.out.println("请求 " + requestIp + " 被分配给服务 " + address);

code/loadbalance-java/src/com/caojx/learn/ServerIps.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
public class ServerIps {
1414

1515
/**
16-
* A、B、C 假设是三台服务器
16+
* 三台服务器的ip地址
1717
*/
1818
public static final List<String> LIST = Arrays.asList("A", "B", "C");
1919

2020
/**
2121
* 三台服务器的ip地址
2222
*/
23-
public static final List<String> LIST_IP = Arrays.asList("192.168.0.10", "192.168.0.20", "192.168.0.40", "192.168.0.50");
23+
public static final List<String> LIST_IP = Arrays.asList("192.168.0.10", "192.168.0.20", "192.168.0.30");
2424

2525
/**
2626
* A、B、C 服务器,带权重
2727
* 即假如10次请求,A要请求2次,B要请求3次,C要请求5次
2828
*/
29-
public static final Map<String, Integer> WEIGHT_LIST = new LinkedHashMap<>();
29+
public static final Map<String, Integer> WEIGHT_MAP = new LinkedHashMap<>();
3030
static {
31-
WEIGHT_LIST.put("A", 2);
32-
WEIGHT_LIST.put("B", 3);
33-
WEIGHT_LIST.put("C", 5);
31+
WEIGHT_MAP.put("A", 2);
32+
WEIGHT_MAP.put("B", 3);
33+
WEIGHT_MAP.put("C", 5);
3434
}
3535
}

code/loadbalance-java/src/com/caojx/learn/WeightRandom.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public static String getServer() {
2626

2727
List<String> ips = new ArrayList<>();
2828

29-
for (String ip : ServerIps.WEIGHT_LIST.keySet()) {
30-
Integer weight = ServerIps.WEIGHT_LIST.get(ip);
29+
for (String ip : ServerIps.WEIGHT_MAP.keySet()) {
30+
Integer weight = ServerIps.WEIGHT_MAP.get(ip);
3131

3232
// weight多少在ips里面存多少 例A权重为2 在ips里面存两个
3333
for (int i = 0; i < weight; i++) {

code/loadbalance-java/src/com/caojx/learn/WeightRoundRobin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public static String getServer() {
2828

2929
List<String> ips = new ArrayList<>();
3030

31-
for (String ip : ServerIps.WEIGHT_LIST.keySet()) {
32-
Integer weight = ServerIps.WEIGHT_LIST.get(ip);
31+
for (String ip : ServerIps.WEIGHT_MAP.keySet()) {
32+
Integer weight = ServerIps.WEIGHT_MAP.get(ip);
3333

3434
// weight多少在ips里面存多少 例A权重为2 在ips里面存两个
3535
for (int i = 0; i < weight; i++) {

code/loadbalance-java/src/com/caojx/learn/WeightRoundRobinV2.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public static void main(String[] args) {
1414
}
1515

1616
public static String getServer(Integer num) {
17-
int totalWeights = ServerIps.WEIGHT_LIST.values().stream().mapToInt(w -> w).sum();
17+
int totalWeights = ServerIps.WEIGHT_MAP.values().stream().mapToInt(w -> w).sum();
1818

1919
Integer pos = num % totalWeights;
2020

21-
for (String ip : ServerIps.WEIGHT_LIST.keySet()) {
22-
Integer weight = ServerIps.WEIGHT_LIST.get(ip);
21+
for (String ip : ServerIps.WEIGHT_MAP.keySet()) {
22+
Integer weight = ServerIps.WEIGHT_MAP.get(ip);
2323

2424
// 坐标小于权重,说明可以由该服务器处理
2525
if (pos < weight) {

code/loadbalance-java/src/com/caojx/learn/WeightRoundRobinV3.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.caojx.learn;
22

3+
import java.util.Comparator;
34
import java.util.HashMap;
45
import java.util.Map;
56

@@ -22,32 +23,29 @@ public static void main(String[] args) {
2223
public static Map<String, Weight> currWeights = new HashMap<>();
2324

2425
public static String getServer() {
25-
int totalWeights = ServerIps.WEIGHT_LIST.values().stream().mapToInt(w -> w).sum();
26+
int totalWeights = ServerIps.WEIGHT_MAP.values().stream().mapToInt(w -> w).sum();
2627

27-
// 初始化一个 动态权重currWeights 默认值 0
28+
// 初始化服务器的固定权重、动态权重 currWeights 默认值 0
2829
if (currWeights.isEmpty()) {
29-
ServerIps.WEIGHT_LIST.forEach((ip, weight) -> {
30-
currWeights.put(ip, new Weight(ip, weight, 0));
30+
ServerIps.WEIGHT_MAP.forEach((ip, weightNum) -> {
31+
Weight weight = new Weight(ip, weightNum, 0);
32+
currWeights.put(ip, weight);
3133
});
3234
}
3335

34-
// currWeight +weight 动态权重+固定权重
36+
// 计算动态权重
37+
// 动态权重 = 固定权重 + 动态权重
38+
// currWeight = currWeight +weight
3539
for (Weight weight : currWeights.values()) {
3640
weight.setCurrentWeight(weight.getCurrentWeight() + weight.getWeight());
3741
}
3842

39-
// 找最大值 max(currWeight)
40-
Weight maxCurrentWeight = null;
41-
for (Weight weight : currWeights.values()) {
42-
if (maxCurrentWeight == null || weight.getCurrentWeight() > maxCurrentWeight.getCurrentWeight()) {
43-
maxCurrentWeight = weight;
44-
}
45-
}
43+
// 获取权重最大的动态权重
44+
Weight maxCurrentWeight = currWeights.values().stream().max(Comparator.comparing(Weight::getCurrentWeight)).get();
4645

4746
// 动态变化的权重:max(currWeight)-=sum(weight)
4847
maxCurrentWeight.setCurrentWeight(maxCurrentWeight.getCurrentWeight() - totalWeights);
4948

50-
5149
return maxCurrentWeight.getIp();
5250
}
5351

code/loadbalance-java/负载均衡算法学习/负载均衡算法学习.md

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,13 @@ C
420420

421421
**步骤:**
422422

423-
1. currWeight+weight :固定权重+动态变化的权重,按照位置相加
423+
1. currWeight+weight :动态变化的权重+固定权重,按照位置相加
424424

425425
<img src="img/image-20220620220445384.png" alt="image-20220620220445384" style="zoom:50%;" align=left />
426426

427427
2+0=2,、3+0=3、5+0=5
428428

429-
2. max(currWeight) 计算最大权重 = 5
430-
431-
3. 结果是C服务器
429+
2. max(currWeight) 计算最大权重 = 5()结果是C服务器
432430

433431
4. 计算动态变化的权重:max(currWeight)-=sum(weight)
434432

@@ -442,27 +440,23 @@ C
442440

443441

444442

445-
| currWeight +weight | max(currWeight) | result | 动态变化的权重:max(currWeight)-=sum(weight) |
446-
| ------------------ | --------------- | ------------------------ | -------------------------------------------- |
447-
| 2、3、5 | 5 | 假设权重5,由服务器C处理 | 2、3、-5=(最大的权重5-10=-5) |
448-
| 4、6、0 | 6 | B | 4、-4、0 |
449-
| 6、-1、5 | 6 | B | -4、-1、5 |
450-
| -2、2、10 | 10 | C | -2、2、0 |
451-
| 0、5、5、 | 5 | B | 0、-5、5 |
452-
| 2、-2、10 | 10 | C | 2、-2、0 |
453-
| 4、1、5 | 5 | C | 4、1、-5 |
454-
| 6、4、0 | 6 | A | -4、4、0 |
455-
| -2、7、5 | 7 | B | -2、-3、5 |
456-
| 0、0、10 | 10 | C | 0、0、0 最后动态权重,又变回了初始值 |
457-
458-
443+
| 轮次 | **weight** | **currWeight** | **currWeight=currWeight +weight** | **max(currWeight)** | 坐标位置 | **result** | **动态变化的权重:max(currWeight)-=sum(weight)** |
444+
| ---- | ---------- | -------------- | --------------------------------- | ------------------- | -------- | --------------------- | ------------------------------------------------ |
445+
| 1 | 2、3、5 | 0、0、0 | 2、3、5 | 5 | 2 | C(2号坐标是C服务器) | 2、3、-5=(最大的权重5-固定权重和10=-5) |
446+
| 2 | 2、3、5 | 2、3、-5 | 4、6、0 | 6 | 1 | B | 4、-4、0 |
447+
| 3 | 2、3、5 | 4、-4、0 | 6、-1、5 | 6 | 0 | A | -4、-1、5 |
448+
| 4 | 2、3、5 | -4、-1、5 | -2、2、10 | 10 | 2 | C | -2、2、0 |
449+
| 5 | 2、3、5 | -2、2、0 | 0、5、5、 | 5 | 1 | B | 0、-5、5 |
450+
| 6 | 2、3、5 | 0、-5、5 | 2、-2、10 | 10 | 2 | C | 2、-2、0 |
451+
| 7 | 2、3、5 | 2、-2、0 | 4、1、5 | 5 | 2 | C | 4、1、-5 |
452+
| 8 | 2、3、5 | 4、1、-5 | 6、4、0 | 6 | 0 | A | -4、4、0 |
453+
| 9 | 2、3、5 | -4、4、0 | -2、7、5 | 7 | 1 | B | -2、-3、5 |
454+
| 10 | 2、3、5 | -2、-3、5 | 0、0、10 | 10 | 2 | C | 0、0、0 最后动态权重,又变回了初始值 |
459455

460456
这些也不用太纠结,记住就行了
461457

462458

463459

464-
465-
466460
```java
467461
/**
468462
* 平滑加权轮询
@@ -483,32 +477,29 @@ public class WeightRoundRobinV3 {
483477
public static Map<String, Weight> currWeights = new HashMap<>();
484478

485479
public static String getServer() {
486-
int totalWeights = ServerIps.WEIGHT_LIST.values().stream().mapToInt(w -> w).sum();
480+
int totalWeights = ServerIps.WEIGHT_MAP.values().stream().mapToInt(w -> w).sum();
487481

488-
// 初始化一个 动态权重currWeights 默认值 0
482+
// 初始化服务器的固定权重、动态权重 currWeights 默认值 0
489483
if (currWeights.isEmpty()) {
490-
ServerIps.WEIGHT_LIST.forEach((ip, weight) -> {
491-
currWeights.put(ip, new Weight(ip, weight, 0));
484+
ServerIps.WEIGHT_MAP.forEach((ip, weightNum) -> {
485+
Weight weight = new Weight(ip, weightNum, 0);
486+
currWeights.put(ip, weight);
492487
});
493488
}
494489

495-
// currWeight +weight 动态权重+固定权重
490+
// 计算动态权重
491+
// 动态权重 = 固定权重 + 动态权重
492+
// currWeight = currWeight +weight
496493
for (Weight weight : currWeights.values()) {
497494
weight.setCurrentWeight(weight.getCurrentWeight() + weight.getWeight());
498495
}
499496

500-
// 找最大值 max(currWeight)
501-
Weight maxCurrentWeight = null;
502-
for (Weight weight : currWeights.values()) {
503-
if (maxCurrentWeight == null || weight.getCurrentWeight() > maxCurrentWeight.getCurrentWeight()) {
504-
maxCurrentWeight = weight;
505-
}
506-
}
497+
// 获取权重最大的动态权重
498+
Weight maxCurrentWeight = currWeights.values().stream().max(Comparator.comparing(Weight::getCurrentWeight)).get();
507499

508500
// 动态变化的权重:max(currWeight)-=sum(weight)
509501
maxCurrentWeight.setCurrentWeight(maxCurrentWeight.getCurrentWeight() - totalWeights);
510-
511-
502+
512503
return maxCurrentWeight.getIp();
513504
}
514505

@@ -563,6 +554,26 @@ public class WeightRoundRobinV3 {
563554
}
564555
}
565556

557+
558+
```
559+
560+
561+
562+
结果:
563+
564+
可以看到 A 出现了2次,B出现了3次,C出现了5次
565+
566+
```结果
567+
C
568+
B
569+
A
570+
C
571+
B
572+
C
573+
C
574+
A
575+
B
576+
C
566577
```
567578

568579

0 commit comments

Comments
 (0)