Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
discovery
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈文顺
discovery
Commits
b9da7639
Commit
b9da7639
authored
May 30, 2019
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化和修复随机权重的算法
parent
f2c6e70a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
64 deletions
+20
-64
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/decorator/PredicateBasedRuleDecorator.java
+4
-3
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/decorator/ZoneAvoidanceRuleDecorator.java
+4
-3
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/loadbalance/weight/AbstractWeightRandomLoadBalance.java
+12
-58
No files found.
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/decorator/PredicateBasedRuleDecorator.java
View file @
b9da7639
...
@@ -17,7 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -17,7 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import
com.nepxion.discovery.common.entity.WeightFilterEntity
;
import
com.nepxion.discovery.common.entity.WeightFilterEntity
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.loadbalance.WeightRandomLoadBalance
;
import
com.nepxion.discovery.plugin.framework.loadbalance.IWeightRandomLoadBalance
;
import
com.nepxion.discovery.plugin.framework.loadbalance.weight.ArrayWeightRandomLoadBalance
;
import
com.netflix.loadbalancer.PredicateBasedRule
;
import
com.netflix.loadbalancer.PredicateBasedRule
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.Server
;
...
@@ -25,11 +26,11 @@ public abstract class PredicateBasedRuleDecorator extends PredicateBasedRule {
...
@@ -25,11 +26,11 @@ public abstract class PredicateBasedRuleDecorator extends PredicateBasedRule {
@Autowired
@Autowired
private
PluginAdapter
pluginAdapter
;
private
PluginAdapter
pluginAdapter
;
private
WeightRandomLoadBalance
weightRandomLoadBalance
;
private
I
WeightRandomLoadBalance
weightRandomLoadBalance
;
@PostConstruct
@PostConstruct
private
void
initialize
()
{
private
void
initialize
()
{
weightRandomLoadBalance
=
new
WeightRandomLoadBalance
();
weightRandomLoadBalance
=
new
Array
WeightRandomLoadBalance
();
weightRandomLoadBalance
.
setPluginAdapter
(
pluginAdapter
);
weightRandomLoadBalance
.
setPluginAdapter
(
pluginAdapter
);
}
}
...
...
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/decorator/ZoneAvoidanceRuleDecorator.java
View file @
b9da7639
...
@@ -17,7 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -17,7 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import
com.nepxion.discovery.common.entity.WeightFilterEntity
;
import
com.nepxion.discovery.common.entity.WeightFilterEntity
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.loadbalance.WeightRandomLoadBalance
;
import
com.nepxion.discovery.plugin.framework.loadbalance.weight.AbstractWeightRandomLoadBalance
;
import
com.nepxion.discovery.plugin.framework.loadbalance.weight.ArrayWeightRandomLoadBalance
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ZoneAvoidanceRule
;
import
com.netflix.loadbalancer.ZoneAvoidanceRule
;
...
@@ -25,11 +26,11 @@ public class ZoneAvoidanceRuleDecorator extends ZoneAvoidanceRule {
...
@@ -25,11 +26,11 @@ public class ZoneAvoidanceRuleDecorator extends ZoneAvoidanceRule {
@Autowired
@Autowired
private
PluginAdapter
pluginAdapter
;
private
PluginAdapter
pluginAdapter
;
private
WeightRandomLoadBalance
weightRandomLoadBalance
;
private
Abstract
WeightRandomLoadBalance
weightRandomLoadBalance
;
@PostConstruct
@PostConstruct
private
void
initialize
()
{
private
void
initialize
()
{
weightRandomLoadBalance
=
new
WeightRandomLoadBalance
();
weightRandomLoadBalance
=
new
Array
WeightRandomLoadBalance
();
weightRandomLoadBalance
.
setPluginAdapter
(
pluginAdapter
);
weightRandomLoadBalance
.
setPluginAdapter
(
pluginAdapter
);
}
}
...
...
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/loadbalance/WeightRandomLoadBalance.java
→
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/loadbalance/
weight/Abstract
WeightRandomLoadBalance.java
View file @
b9da7639
package
com
.
nepxion
.
discovery
.
plugin
.
framework
.
loadbalance
;
package
com
.
nepxion
.
discovery
.
plugin
.
framework
.
loadbalance
.
weight
;
/**
/**
* <p>Title: Nepxion Discovery</p>
* <p>Title: Nepxion Discovery</p>
...
@@ -11,7 +11,6 @@ package com.nepxion.discovery.plugin.framework.loadbalance;
...
@@ -11,7 +11,6 @@ package com.nepxion.discovery.plugin.framework.loadbalance;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.concurrent.ThreadLocalRandom
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.MapUtils
;
import
org.apache.commons.collections4.MapUtils
;
...
@@ -22,13 +21,19 @@ import com.nepxion.discovery.common.entity.RegionWeightEntity;
...
@@ -22,13 +21,19 @@ import com.nepxion.discovery.common.entity.RegionWeightEntity;
import
com.nepxion.discovery.common.entity.RuleEntity
;
import
com.nepxion.discovery.common.entity.RuleEntity
;
import
com.nepxion.discovery.common.entity.WeightEntity
;
import
com.nepxion.discovery.common.entity.WeightEntity
;
import
com.nepxion.discovery.common.entity.WeightFilterEntity
;
import
com.nepxion.discovery.common.entity.WeightFilterEntity
;
import
com.nepxion.discovery.common.exception.DiscoveryException
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.loadbalance.IWeightRandomLoadBalance
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.Server
;
public
class
WeightRandomLoadBalance
{
public
abstract
class
AbstractWeightRandomLoadBalance
implements
I
WeightRandomLoadBalance
{
private
PluginAdapter
pluginAdapter
;
private
PluginAdapter
pluginAdapter
;
@Override
public
void
setPluginAdapter
(
PluginAdapter
pluginAdapter
)
{
this
.
pluginAdapter
=
pluginAdapter
;
}
@Override
public
WeightFilterEntity
getWeightFilterEntity
()
{
public
WeightFilterEntity
getWeightFilterEntity
()
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
if
(
ruleEntity
==
null
)
{
if
(
ruleEntity
==
null
)
{
...
@@ -45,54 +50,7 @@ public class WeightRandomLoadBalance {
...
@@ -45,54 +50,7 @@ public class WeightRandomLoadBalance {
return
weightFilterEntity
;
return
weightFilterEntity
;
}
}
public
Server
choose
(
List
<
Server
>
serverList
,
WeightFilterEntity
weightFilterEntity
)
{
protected
int
getWeight
(
Server
server
,
WeightFilterEntity
weightFilterEntity
)
{
if
(
CollectionUtils
.
isEmpty
(
serverList
))
{
return
null
;
}
int
[]
weights
=
new
int
[
serverList
.
size
()];
for
(
int
i
=
0
;
i
<
serverList
.
size
();
i
++)
{
Server
server
=
serverList
.
get
(
i
);
int
weight
=
getWeight
(
server
,
weightFilterEntity
);
if
(
weight
>
0
)
{
weights
[
i
]
=
weight
;
}
}
int
index
=
getIndex
(
weights
);
return
serverList
.
get
(
index
);
}
private
int
getIndex
(
int
[]
weights
)
{
// 次序号/权重区间值
int
[][]
weightHolder
=
new
int
[
weights
.
length
][
2
];
// 总权重
int
totalWeight
=
0
;
// 赋值次序号和区间值累加的数组值,从小到大排列
// 例如,对于权重分别为20,40, 60的三个服务,将形成[0, 20),[20, 60),[60, 120]三个区间
for
(
int
i
=
0
;
i
<
weights
.
length
;
i
++)
{
if
(
weights
[
i
]
<=
0
)
{
continue
;
}
totalWeight
+=
weights
[
i
];
weightHolder
[
i
][
0
]
=
i
;
weightHolder
[
i
][
1
]
=
totalWeight
;
}
// 获取介于0(含)和n(不含)伪随机,均匀分布的int值
int
hitWeight
=
ThreadLocalRandom
.
current
().
nextInt
(
totalWeight
)
+
1
;
// [1, totalWeight)
for
(
int
i
=
0
;
i
<
weightHolder
.
length
;
i
++)
{
if
(
hitWeight
<=
weightHolder
[
i
][
1
])
{
return
weightHolder
[
i
][
0
];
}
}
return
weightHolder
[
0
][
0
];
}
private
int
getWeight
(
Server
server
,
WeightFilterEntity
weightFilterEntity
)
{
Map
<
String
,
List
<
WeightEntity
>>
weightEntityMap
=
weightFilterEntity
.
getWeightEntityMap
();
Map
<
String
,
List
<
WeightEntity
>>
weightEntityMap
=
weightFilterEntity
.
getWeightEntityMap
();
RegionWeightEntity
regionWeightEntity
=
weightFilterEntity
.
getRegionWeightEntity
();
RegionWeightEntity
regionWeightEntity
=
weightFilterEntity
.
getRegionWeightEntity
();
...
@@ -114,8 +72,9 @@ public class WeightRandomLoadBalance {
...
@@ -114,8 +72,9 @@ public class WeightRandomLoadBalance {
weight
=
getWeight
(
providerRegion
,
regionWeightEntity
);
weight
=
getWeight
(
providerRegion
,
regionWeightEntity
);
}
}
// 所有的权重配置都没找到,则按权重值为0来处理
if
(
weight
<
0
)
{
if
(
weight
<
0
)
{
throw
new
DiscoveryException
(
"Weight isn't configed for serviceId="
+
providerServiceId
)
;
weight
=
0
;
}
}
return
weight
;
return
weight
;
...
@@ -168,8 +127,4 @@ public class WeightRandomLoadBalance {
...
@@ -168,8 +127,4 @@ public class WeightRandomLoadBalance {
return
-
1
;
return
-
1
;
}
}
}
}
public
void
setPluginAdapter
(
PluginAdapter
pluginAdapter
)
{
this
.
pluginAdapter
=
pluginAdapter
;
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment