-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from lunasaw/dev_1.0.3
Dev 1.0.3 实现多种负载均衡算法,增加注入配置
- Loading branch information
Showing
16 changed files
with
414 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/io/github/lunasaw/zlm/enums/LoadBalancerEnums.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.github.lunasaw.zlm.enums; | ||
|
||
import lombok.Getter; | ||
|
||
/** | ||
* @author weidian | ||
*/ | ||
|
||
@Getter | ||
public enum LoadBalancerEnums { | ||
RANDOM(1, "Random"), | ||
|
||
ROUND_ROBIN(2, "RoundRobin"), | ||
|
||
CONSISTENT_HASHING(3, "ConsistentHashing"), | ||
|
||
WEIGHT_ROUND_ROBIN(4, "WeightRoundRobin"), | ||
|
||
WEIGHT_RANDOM(5, "WeightRandom"); | ||
|
||
private int code; | ||
private String type; | ||
|
||
private LoadBalancerEnums(int code, String type) { | ||
this.code = code; | ||
this.type = type; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/io/github/lunasaw/zlm/node/LoadBalancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.github.lunasaw.zlm.node; | ||
|
||
import io.github.lunasaw.zlm.config.ZlmNode; | ||
|
||
/** | ||
* @author luna | ||
* @date 2024/1/5 | ||
*/ | ||
public interface LoadBalancer { | ||
|
||
ZlmNode selectNode(String key); | ||
|
||
String getType(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
package io.github.lunasaw.zlm.node; | ||
|
||
import io.github.lunasaw.zlm.api.ZlmRestService; | ||
import io.github.lunasaw.zlm.config.ZlmNodeConfig; | ||
import io.github.lunasaw.zlm.config.ZlmProperties; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author luna | ||
* @date 2023/12/4 | ||
*/ | ||
@Service | ||
@ConditionalOnBean(ZlmProperties.class) | ||
public class NodeService { | ||
|
||
|
||
public void method() { | ||
|
||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/io/github/lunasaw/zlm/node/impl/ConsistentHashingLoadBalander.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.github.lunasaw.zlm.node.impl; | ||
|
||
import io.github.lunasaw.zlm.config.ZlmNode; | ||
import io.github.lunasaw.zlm.config.ZlmProperties; | ||
import io.github.lunasaw.zlm.enums.LoadBalancerEnums; | ||
import io.github.lunasaw.zlm.node.LoadBalancer; | ||
|
||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
/** | ||
* @author luna | ||
* @date 2024/1/5 | ||
*/ | ||
public class ConsistentHashingLoadBalander implements LoadBalancer { | ||
|
||
static final TreeMap<Integer, String> VIRTUAL_NODE_MAP = new TreeMap<>(); | ||
|
||
public ConsistentHashingLoadBalander() { | ||
init(); | ||
} | ||
|
||
public static void init() { | ||
for (String nodeName : ZlmProperties.nodeMap.keySet()) { | ||
ZlmNode nodeConfig = ZlmProperties.nodeMap.get(nodeName); | ||
int weight = nodeConfig.getWeight(); | ||
for (int i = 0; i < weight * 10; i++) { | ||
String virtualNodeName = nodeName + "#" + i; | ||
int hash = getHash(virtualNodeName); | ||
VIRTUAL_NODE_MAP.put(hash, virtualNodeName); | ||
} | ||
} | ||
} | ||
|
||
private static int getHash(String str) { | ||
final int p = 16777619; | ||
int hash = (int) 2166136261L; | ||
for (int i = 0; i < str.length(); i++) { | ||
hash = (hash ^ str.charAt(i)) * p; | ||
} | ||
hash += hash << 13; | ||
hash ^= hash >> 7; | ||
hash += hash << 3; | ||
hash ^= hash >> 17; | ||
hash += hash << 5; | ||
return hash; | ||
} | ||
|
||
@Override | ||
public ZlmNode selectNode(String key) { | ||
int hash = getHash(key); | ||
Map.Entry<Integer, String> entry = VIRTUAL_NODE_MAP.ceilingEntry(hash); | ||
if (entry == null) { | ||
entry = VIRTUAL_NODE_MAP.firstEntry(); | ||
} | ||
String virtualNodeName = entry.getValue(); | ||
String nodeName = virtualNodeName.split("#")[0]; | ||
return ZlmProperties.nodeMap.get(nodeName); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return LoadBalancerEnums.CONSISTENT_HASHING.getType(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/io/github/lunasaw/zlm/node/impl/RandomLoadBalancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.lunasaw.zlm.node.impl; | ||
|
||
import io.github.lunasaw.zlm.config.ZlmNode; | ||
import io.github.lunasaw.zlm.config.ZlmProperties; | ||
import io.github.lunasaw.zlm.enums.LoadBalancerEnums; | ||
import io.github.lunasaw.zlm.node.LoadBalancer; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* @author luna | ||
* @date 2024/1/5 | ||
*/ | ||
public class RandomLoadBalancer implements LoadBalancer { | ||
static final Random RANDOM = new Random(); | ||
|
||
@Override | ||
public ZlmNode selectNode(String key) { | ||
int index = RANDOM.nextInt(ZlmProperties.nodes.size()); | ||
return ZlmProperties.nodes.get(index); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return LoadBalancerEnums.RANDOM.getType(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/io/github/lunasaw/zlm/node/impl/RoundRobinLoadBalancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.lunasaw.zlm.node.impl; | ||
|
||
import io.github.lunasaw.zlm.config.ZlmNode; | ||
import io.github.lunasaw.zlm.config.ZlmProperties; | ||
import io.github.lunasaw.zlm.enums.LoadBalancerEnums; | ||
import io.github.lunasaw.zlm.node.LoadBalancer; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* @author luna | ||
* @date 2024/1/5 | ||
*/ | ||
public class RoundRobinLoadBalancer implements LoadBalancer { | ||
|
||
static final AtomicInteger SEQUENCE = new AtomicInteger(0); | ||
|
||
@Override | ||
public ZlmNode selectNode(String key) { | ||
int index = SEQUENCE.getAndIncrement() % ZlmProperties.nodes.size(); | ||
return ZlmProperties.nodes.get(index); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return LoadBalancerEnums.RANDOM.getType(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/io/github/lunasaw/zlm/node/impl/WeightRandomLoadBalancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.github.lunasaw.zlm.node.impl; | ||
|
||
import io.github.lunasaw.zlm.config.ZlmNode; | ||
import io.github.lunasaw.zlm.config.ZlmProperties; | ||
import io.github.lunasaw.zlm.enums.LoadBalancerEnums; | ||
import io.github.lunasaw.zlm.node.LoadBalancer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* @author luna | ||
* @date 2024/1/5 | ||
*/ | ||
public class WeightRandomLoadBalancer implements LoadBalancer { | ||
|
||
static final AtomicInteger TOTAL_WEIGHT = new AtomicInteger(0); | ||
static final Map<String, Integer> WEIGHT_MAP = new ConcurrentHashMap<>(); | ||
static final Random RANDOM = new Random(); | ||
|
||
|
||
public WeightRandomLoadBalancer() { | ||
init(); | ||
} | ||
|
||
public static void init() { | ||
List<String> nodeList = new ArrayList<>(ZlmProperties.nodeMap.keySet()); | ||
int size = nodeList.size(); | ||
if (size > 0) { | ||
for (String nodeName : nodeList) { | ||
ZlmNode nodeConfig = ZlmProperties.nodeMap.get(nodeName); | ||
int weight = nodeConfig.getWeight(); | ||
WEIGHT_MAP.put(nodeName, weight); | ||
TOTAL_WEIGHT.addAndGet(weight); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public ZlmNode selectNode(String key) { | ||
int randomNum = RANDOM.nextInt(TOTAL_WEIGHT.get()); | ||
int currentWeight = 0; | ||
for (String nodeName : ZlmProperties.nodeMap.keySet()) { | ||
currentWeight += WEIGHT_MAP.get(nodeName); | ||
if (currentWeight > randomNum) { | ||
return ZlmProperties.nodeMap.get(nodeName); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return LoadBalancerEnums.WEIGHT_RANDOM.getType(); | ||
} | ||
} |
Oops, something went wrong.