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
ded68199
Commit
ded68199
authored
Aug 26, 2018
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加局部和全局权重配置功能
parent
81263056
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
59 additions
and
30 deletions
+59
-30
discovery-plugin-admin-center/src/main/java/com/nepxion/discovery/plugin/admincenter/endpoint/RouterEndpoint.java
+13
-1
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/parser/xml/XmlConfigParser.java
+4
-4
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/decorator/PredicateBasedRuleDecorator.java
+5
-4
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/decorator/ZoneAvoidanceRuleDecorator.java
+5
-4
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/loadbalance/WeightRandomLoadBalance.java
+27
-16
discovery-springcloud-example-service/src/main/resources/rule.xml
+5
-1
No files found.
discovery-plugin-admin-center/src/main/java/com/nepxion/discovery/plugin/admincenter/endpoint/RouterEndpoint.java
View file @
ded68199
...
@@ -292,8 +292,18 @@ public class RouterEndpoint {
...
@@ -292,8 +292,18 @@ public class RouterEndpoint {
}
}
String
serviceId
=
pluginAdapter
.
getServiceId
();
String
serviceId
=
pluginAdapter
.
getServiceId
();
// 取局部的权重配置
int
weight
=
getWeight
(
serviceId
,
providerServiceId
,
providerVersion
,
weightEntityMap
);
// 局部权重配置没找到,取全局的权重配置
if
(
weight
<
0
)
{
weight
=
getWeight
(
null
,
providerServiceId
,
providerVersion
,
weightEntityMap
);
}
return
weight
;
}
List
<
WeightEntity
>
weightEntityList
=
weightEntityMap
.
get
(
serviceId
);
private
int
getWeight
(
String
consumerServiceId
,
String
providerServiceId
,
String
providerVersion
,
Map
<
String
,
List
<
WeightEntity
>>
weightEntityMap
)
{
List
<
WeightEntity
>
weightEntityList
=
weightEntityMap
.
get
(
consumerServiceId
);
if
(
CollectionUtils
.
isEmpty
(
weightEntityList
))
{
if
(
CollectionUtils
.
isEmpty
(
weightEntityList
))
{
return
-
1
;
return
-
1
;
}
}
...
@@ -305,6 +315,8 @@ public class RouterEndpoint {
...
@@ -305,6 +315,8 @@ public class RouterEndpoint {
Integer
weight
=
weightMap
.
get
(
providerVersion
);
Integer
weight
=
weightMap
.
get
(
providerVersion
);
if
(
weight
!=
null
)
{
if
(
weight
!=
null
)
{
return
weight
;
return
weight
;
}
else
{
return
-
1
;
}
}
}
}
}
}
...
...
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/parser/xml/XmlConfigParser.java
View file @
ded68199
...
@@ -368,11 +368,11 @@ public class XmlConfigParser implements PluginConfigParser {
...
@@ -368,11 +368,11 @@ public class XmlConfigParser implements PluginConfigParser {
WeightEntity
weightEntity
=
new
WeightEntity
();
WeightEntity
weightEntity
=
new
WeightEntity
();
Attribute
consumerServiceNameAttribute
=
childElement
.
attribute
(
ConfigConstant
.
CONSUMER_SERVICE_NAME_ATTRIBUTE_NAME
);
Attribute
consumerServiceNameAttribute
=
childElement
.
attribute
(
ConfigConstant
.
CONSUMER_SERVICE_NAME_ATTRIBUTE_NAME
);
if
(
consumerServiceNameAttribute
==
null
)
{
String
consumerServiceName
=
null
;
throw
new
DiscoveryException
(
"Attribute["
+
ConfigConstant
.
CONSUMER_SERVICE_NAME_ATTRIBUTE_NAME
+
"] in element["
+
childElement
.
getName
()
+
"] is missing"
);
if
(
consumerServiceNameAttribute
!=
null
)
{
consumerServiceName
=
consumerServiceNameAttribute
.
getData
().
toString
().
trim
();
weightEntity
.
setConsumerServiceName
(
consumerServiceName
);
}
}
String
consumerServiceName
=
consumerServiceNameAttribute
.
getData
().
toString
().
trim
();
weightEntity
.
setConsumerServiceName
(
consumerServiceName
);
Attribute
providerServiceNameAttribute
=
childElement
.
attribute
(
ConfigConstant
.
PROVIDER_SERVICE_NAME_ATTRIBUTE_NAME
);
Attribute
providerServiceNameAttribute
=
childElement
.
attribute
(
ConfigConstant
.
PROVIDER_SERVICE_NAME_ATTRIBUTE_NAME
);
if
(
providerServiceNameAttribute
==
null
)
{
if
(
providerServiceNameAttribute
==
null
)
{
...
...
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/decorator/PredicateBasedRuleDecorator.java
View file @
ded68199
...
@@ -10,10 +10,11 @@ package com.nepxion.discovery.plugin.framework.decorator;
...
@@ -10,10 +10,11 @@ package com.nepxion.discovery.plugin.framework.decorator;
*/
*/
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
javax.annotation.PostConstruct
;
import
javax.annotation.PostConstruct
;
import
org.apache.commons.collections4.
Collection
Utils
;
import
org.apache.commons.collections4.
Map
Utils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.nepxion.discovery.common.entity.WeightEntity
;
import
com.nepxion.discovery.common.entity.WeightEntity
;
...
@@ -36,15 +37,15 @@ public abstract class PredicateBasedRuleDecorator extends PredicateBasedRule {
...
@@ -36,15 +37,15 @@ public abstract class PredicateBasedRuleDecorator extends PredicateBasedRule {
@Override
@Override
public
Server
choose
(
Object
key
)
{
public
Server
choose
(
Object
key
)
{
List
<
WeightEntity
>
weightEntityList
=
weightRandomLoadBalance
.
getWeightEntityList
();
Map
<
String
,
List
<
WeightEntity
>>
weightEntityMap
=
weightRandomLoadBalance
.
getWeightEntityMap
();
if
(
CollectionUtils
.
isEmpty
(
weightEntityList
))
{
if
(
MapUtils
.
isEmpty
(
weightEntityMap
))
{
return
super
.
choose
(
key
);
return
super
.
choose
(
key
);
}
}
List
<
Server
>
eligibleServers
=
getPredicate
().
getEligibleServers
(
getLoadBalancer
().
getAllServers
(),
key
);
List
<
Server
>
eligibleServers
=
getPredicate
().
getEligibleServers
(
getLoadBalancer
().
getAllServers
(),
key
);
try
{
try
{
return
weightRandomLoadBalance
.
choose
(
eligibleServers
,
weightEntity
List
);
return
weightRandomLoadBalance
.
choose
(
eligibleServers
,
weightEntity
Map
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
return
super
.
choose
(
key
);
return
super
.
choose
(
key
);
}
}
...
...
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/decorator/ZoneAvoidanceRuleDecorator.java
View file @
ded68199
...
@@ -10,10 +10,11 @@ package com.nepxion.discovery.plugin.framework.decorator;
...
@@ -10,10 +10,11 @@ package com.nepxion.discovery.plugin.framework.decorator;
*/
*/
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
javax.annotation.PostConstruct
;
import
javax.annotation.PostConstruct
;
import
org.apache.commons.collections4.
Collection
Utils
;
import
org.apache.commons.collections4.
Map
Utils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.nepxion.discovery.common.entity.WeightEntity
;
import
com.nepxion.discovery.common.entity.WeightEntity
;
...
@@ -36,15 +37,15 @@ public class ZoneAvoidanceRuleDecorator extends ZoneAvoidanceRule {
...
@@ -36,15 +37,15 @@ public class ZoneAvoidanceRuleDecorator extends ZoneAvoidanceRule {
@Override
@Override
public
Server
choose
(
Object
key
)
{
public
Server
choose
(
Object
key
)
{
List
<
WeightEntity
>
weightEntityList
=
weightRandomLoadBalance
.
getWeightEntityList
();
Map
<
String
,
List
<
WeightEntity
>>
weightEntityMap
=
weightRandomLoadBalance
.
getWeightEntityMap
();
if
(
CollectionUtils
.
isEmpty
(
weightEntityList
))
{
if
(
MapUtils
.
isEmpty
(
weightEntityMap
))
{
return
super
.
choose
(
key
);
return
super
.
choose
(
key
);
}
}
List
<
Server
>
eligibleServers
=
getPredicate
().
getEligibleServers
(
getLoadBalancer
().
getAllServers
(),
key
);
List
<
Server
>
eligibleServers
=
getPredicate
().
getEligibleServers
(
getLoadBalancer
().
getAllServers
(),
key
);
try
{
try
{
return
weightRandomLoadBalance
.
choose
(
eligibleServers
,
weightEntity
List
);
return
weightRandomLoadBalance
.
choose
(
eligibleServers
,
weightEntity
Map
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
return
super
.
choose
(
key
);
return
super
.
choose
(
key
);
}
}
...
...
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/loadbalance/WeightRandomLoadBalance.java
View file @
ded68199
...
@@ -16,8 +16,6 @@ import java.util.concurrent.ThreadLocalRandom;
...
@@ -16,8 +16,6 @@ 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
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.nepxion.discovery.common.entity.DiscoveryEntity
;
import
com.nepxion.discovery.common.entity.DiscoveryEntity
;
import
com.nepxion.discovery.common.entity.RuleEntity
;
import
com.nepxion.discovery.common.entity.RuleEntity
;
...
@@ -28,11 +26,9 @@ import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
...
@@ -28,11 +26,9 @@ import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.Server
;
public
class
WeightRandomLoadBalance
{
public
class
WeightRandomLoadBalance
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
WeightRandomLoadBalance
.
class
);
private
PluginAdapter
pluginAdapter
;
private
PluginAdapter
pluginAdapter
;
public
List
<
WeightEntity
>
getWeightEntityList
()
{
public
Map
<
String
,
List
<
WeightEntity
>>
getWeightEntityMap
()
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
if
(
ruleEntity
==
null
)
{
if
(
ruleEntity
==
null
)
{
return
null
;
return
null
;
...
@@ -53,14 +49,10 @@ public class WeightRandomLoadBalance {
...
@@ -53,14 +49,10 @@ public class WeightRandomLoadBalance {
return
null
;
return
null
;
}
}
String
serviceId
=
pluginAdapter
.
getServiceId
();
return
weightEntityMap
;
List
<
WeightEntity
>
weightEntityList
=
weightEntityMap
.
get
(
serviceId
);
return
weightEntityList
;
}
}
public
Server
choose
(
List
<
Server
>
serverList
,
List
<
WeightEntity
>
weightEntityList
)
{
public
Server
choose
(
List
<
Server
>
serverList
,
Map
<
String
,
List
<
WeightEntity
>>
weightEntityMap
)
{
if
(
CollectionUtils
.
isEmpty
(
serverList
))
{
if
(
CollectionUtils
.
isEmpty
(
serverList
))
{
return
null
;
return
null
;
}
}
...
@@ -68,7 +60,7 @@ public class WeightRandomLoadBalance {
...
@@ -68,7 +60,7 @@ public class WeightRandomLoadBalance {
int
[]
weights
=
new
int
[
serverList
.
size
()];
int
[]
weights
=
new
int
[
serverList
.
size
()];
for
(
int
i
=
0
;
i
<
serverList
.
size
();
i
++)
{
for
(
int
i
=
0
;
i
<
serverList
.
size
();
i
++)
{
Server
server
=
serverList
.
get
(
i
);
Server
server
=
serverList
.
get
(
i
);
int
weight
=
getWeight
(
server
,
weightEntity
List
);
int
weight
=
getWeight
(
server
,
weightEntity
Map
);
if
(
weight
>
0
)
{
if
(
weight
>
0
)
{
weights
[
i
]
=
weight
;
weights
[
i
]
=
weight
;
}
}
...
@@ -107,10 +99,31 @@ public class WeightRandomLoadBalance {
...
@@ -107,10 +99,31 @@ public class WeightRandomLoadBalance {
return
weightHolder
[
0
][
0
];
return
weightHolder
[
0
][
0
];
}
}
private
int
getWeight
(
Server
server
,
List
<
WeightEntity
>
weightEntityList
)
{
private
int
getWeight
(
Server
server
,
Map
<
String
,
List
<
WeightEntity
>>
weightEntityMap
)
{
String
providerServiceId
=
server
.
getMetaInfo
().
getAppName
();
String
providerServiceId
=
server
.
getMetaInfo
().
getAppName
();
String
providerVersion
=
pluginAdapter
.
getServerVersion
(
server
);
String
providerVersion
=
pluginAdapter
.
getServerVersion
(
server
);
String
serviceId
=
pluginAdapter
.
getServiceId
();
// 取局部的权重配置
int
weight
=
getWeight
(
serviceId
,
providerServiceId
,
providerVersion
,
weightEntityMap
);
// 局部权重配置没找到,取全局的权重配置
if
(
weight
<
0
)
{
weight
=
getWeight
(
null
,
providerServiceId
,
providerVersion
,
weightEntityMap
);
}
if
(
weight
<
0
)
{
throw
new
DiscoveryException
(
"Weight isn't configed for serviceId="
+
providerServiceId
+
", version="
+
providerVersion
);
}
return
weight
;
}
private
int
getWeight
(
String
consumerServiceId
,
String
providerServiceId
,
String
providerVersion
,
Map
<
String
,
List
<
WeightEntity
>>
weightEntityMap
)
{
List
<
WeightEntity
>
weightEntityList
=
weightEntityMap
.
get
(
consumerServiceId
);
if
(
CollectionUtils
.
isEmpty
(
weightEntityList
))
{
return
-
1
;
}
for
(
WeightEntity
weightEntity
:
weightEntityList
)
{
for
(
WeightEntity
weightEntity
:
weightEntityList
)
{
String
providerServiceName
=
weightEntity
.
getProviderServiceName
();
String
providerServiceName
=
weightEntity
.
getProviderServiceName
();
if
(
StringUtils
.
equalsIgnoreCase
(
providerServiceName
,
providerServiceId
))
{
if
(
StringUtils
.
equalsIgnoreCase
(
providerServiceName
,
providerServiceId
))
{
...
@@ -119,9 +132,7 @@ public class WeightRandomLoadBalance {
...
@@ -119,9 +132,7 @@ public class WeightRandomLoadBalance {
if
(
weight
!=
null
)
{
if
(
weight
!=
null
)
{
return
weight
;
return
weight
;
}
else
{
}
else
{
LOG
.
error
(
"Weight isn't configed for serviceId={}, version={}"
,
providerServiceId
,
providerVersion
);
return
-
1
;
throw
new
DiscoveryException
(
"Weight isn't configed for serviceId="
+
providerServiceId
+
", version="
+
providerVersion
);
}
}
}
}
}
}
...
...
discovery-springcloud-example-service/src/main/resources/rule.xml
View file @
ded68199
...
@@ -69,10 +69,14 @@
...
@@ -69,10 +69,14 @@
<!-- 权重策略介绍 -->
<!-- 权重策略介绍 -->
<!-- 1. 标准配置,举例如下 -->
<!-- 1. 标准配置,举例如下 -->
<!-- <service consumer-service-name="a" provider-service-name="b" provider-weight-value="1.0=90;1.1=10"/> 表示消费端访问提供端的时候,提供端的1.0版本提供90%的权重流量,1.1版本提供10%的权重流量 -->
<!-- <service consumer-service-name="a" provider-service-name="b" provider-weight-value="1.0=90;1.1=10"/> 表示消费端访问提供端的时候,提供端的1.0版本提供90%的权重流量,1.1版本提供10%的权重流量 -->
<!-- 2. 尽量为线上所有版本都赋予权重值 -->
<!-- <service provider-service-name="b" provider-weight-value="1.0=90;1.1=10"/> 表示所有消费端访问提供端的时候,提供端的1.0版本提供90%的权重流量,1.1版本提供10%的权重流量 -->
<!-- 2. 局部配置,指当consumer-service-name不为空的时候,专门为该消费端配置权重。全局配置,可以为所有消费端配置权重。当局部配置和全局配置同时存在的时候,以局部配置优先 -->
<!-- 3. 尽量为线上所有版本都赋予权重值 -->
<weight>
<weight>
<!-- 表示消费端服务b访问提供端服务c的时候,提供端服务c的1.0版本提供90%的权重流量,1.1版本提供10%的权重流量 -->
<!-- 表示消费端服务b访问提供端服务c的时候,提供端服务c的1.0版本提供90%的权重流量,1.1版本提供10%的权重流量 -->
<service
consumer-service-name=
"discovery-springcloud-example-b"
provider-service-name=
"discovery-springcloud-example-c"
provider-weight-value=
"1.0=90;1.1=10"
/>
<service
consumer-service-name=
"discovery-springcloud-example-b"
provider-service-name=
"discovery-springcloud-example-c"
provider-weight-value=
"1.0=90;1.1=10"
/>
<!-- 表示所有消费端服务访问提供端服务c的时候,提供端服务c的1.0版本提供80%的权重流量,1.1版本提供20%的权重流量 -->
<service
provider-service-name=
"discovery-springcloud-example-c"
provider-weight-value=
"1.0=80;1.1=20"
/>
</weight>
</weight>
</discovery>
</discovery>
...
...
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