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
d4b03539
Commit
d4b03539
authored
Aug 15, 2018
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持策略的规则可配置化
parent
0d384df0
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
258 additions
and
48 deletions
+258
-48
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/decorator/PredicateBasedRuleDecorator.java
+53
-0
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/configuration/StrategyAutoConfiguration.java
+2
-27
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/configuration/StrategyLoadBalanceConfiguration.java
+75
-0
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/constant/StrategyConstant.java
+2
-0
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/discovery/DiscoveryEnabledBasePredicate.java
+1
-1
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/discovery/DiscoveryEnabledBaseRule.java
+15
-20
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/discovery/DiscoveryEnabledZoneAvoidancePredicate.java
+58
-0
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/discovery/DiscoveryEnabledZoneAvoidanceRule.java
+52
-0
No files found.
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/decorator/PredicateBasedRuleDecorator.java
0 → 100644
View file @
d4b03539
package
com
.
nepxion
.
discovery
.
plugin
.
framework
.
decorator
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
java.util.List
;
import
javax.annotation.PostConstruct
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.nepxion.discovery.common.entity.WeightEntity
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.loadbalance.WeightRandomLoadBalance
;
import
com.netflix.loadbalancer.PredicateBasedRule
;
import
com.netflix.loadbalancer.Server
;
public
abstract
class
PredicateBasedRuleDecorator
extends
PredicateBasedRule
{
@Autowired
private
PluginAdapter
pluginAdapter
;
private
WeightRandomLoadBalance
weightRandomLoadBalance
;
@PostConstruct
private
void
initialize
()
{
weightRandomLoadBalance
=
new
WeightRandomLoadBalance
();
weightRandomLoadBalance
.
setPluginAdapter
(
pluginAdapter
);
}
@Override
public
Server
choose
(
Object
key
)
{
List
<
WeightEntity
>
weightEntityList
=
weightRandomLoadBalance
.
getWeightEntityList
();
if
(
CollectionUtils
.
isEmpty
(
weightEntityList
))
{
return
super
.
choose
(
key
);
}
List
<
Server
>
eligibleServers
=
getPredicate
().
getEligibleServers
(
getLoadBalancer
().
getAllServers
(),
key
);
try
{
return
weightRandomLoadBalance
.
choose
(
eligibleServers
,
weightEntityList
);
}
catch
(
Exception
e
)
{
return
super
.
choose
(
key
);
}
}
}
\ No newline at end of file
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/configuration/StrategyAutoConfiguration.java
View file @
d4b03539
...
@@ -9,39 +9,15 @@ package com.nepxion.discovery.plugin.strategy.configuration;
...
@@ -9,39 +9,15 @@ package com.nepxion.discovery.plugin.strategy.configuration;
* @version 1.0
* @version 1.0
*/
*/
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.AutoConfigureBefore
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration
;
import
org.springframework.cloud.netflix.ribbon.RibbonClients
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.strategy.constant.StrategyConstant
;
import
com.nepxion.discovery.plugin.strategy.constant.StrategyConstant
;
import
com.nepxion.discovery.plugin.strategy.discovery.DiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.discovery.DiscoveryEnabledPredicate
;
import
com.nepxion.discovery.plugin.strategy.discovery.DiscoveryEnabledRule
;
@Configuration
@Configuration
@
AutoConfigureBefore
(
RibbonClientConfiguration
.
class
)
@
RibbonClients
(
defaultConfiguration
=
{
StrategyLoadBalanceConfiguration
.
class
}
)
@ConditionalOnProperty
(
value
=
StrategyConstant
.
SPRING_APPLICATION_STRATEGY_CONTROL_ENABLED
,
matchIfMissing
=
true
)
@ConditionalOnProperty
(
value
=
StrategyConstant
.
SPRING_APPLICATION_STRATEGY_CONTROL_ENABLED
,
matchIfMissing
=
true
)
public
class
StrategyAutoConfiguration
{
public
class
StrategyAutoConfiguration
{
@Autowired
protected
PluginAdapter
pluginAdapter
;
@Autowired
private
DiscoveryEnabledAdapter
discoveryEnabledAdapter
;
@Bean
@ConditionalOnMissingBean
// @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public
DiscoveryEnabledRule
discoveryEnabledRule
()
{
DiscoveryEnabledRule
discoveryEnabledRule
=
new
DiscoveryEnabledRule
();
DiscoveryEnabledPredicate
discoveryEnabledPredicate
=
discoveryEnabledRule
.
getDiscoveryEnabledPredicate
();
discoveryEnabledPredicate
.
setPluginAdapter
(
pluginAdapter
);
discoveryEnabledPredicate
.
setDiscoveryEnabledAdapter
(
discoveryEnabledAdapter
);
return
discoveryEnabledRule
;
}
}
}
\ No newline at end of file
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/configuration/StrategyLoadBalanceConfiguration.java
0 → 100644
View file @
d4b03539
package
com
.
nepxion
.
discovery
.
plugin
.
strategy
.
configuration
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.AutoConfigureBefore
;
import
org.springframework.cloud.netflix.ribbon.PropertiesFactory
;
import
org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration
;
import
org.springframework.cloud.netflix.ribbon.RibbonClientName
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.strategy.constant.StrategyConstant
;
import
com.nepxion.discovery.plugin.strategy.discovery.DiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.discovery.DiscoveryEnabledBasePredicate
;
import
com.nepxion.discovery.plugin.strategy.discovery.DiscoveryEnabledBaseRule
;
import
com.nepxion.discovery.plugin.strategy.discovery.DiscoveryEnabledZoneAvoidancePredicate
;
import
com.nepxion.discovery.plugin.strategy.discovery.DiscoveryEnabledZoneAvoidanceRule
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.loadbalancer.IRule
;
@Configuration
@AutoConfigureBefore
(
RibbonClientConfiguration
.
class
)
public
class
StrategyLoadBalanceConfiguration
{
@Value
(
"${"
+
StrategyConstant
.
SPRING_APPLICATION_STRATEGY_ZONE_AVOIDANCE_RULE_ENABLED
+
":true}"
)
private
boolean
zoneAvoidanceRuleEnabled
;
@RibbonClientName
private
String
serviceId
=
"client"
;
@Autowired
private
PropertiesFactory
propertiesFactory
;
@Autowired
private
PluginAdapter
pluginAdapter
;
@Autowired
private
DiscoveryEnabledAdapter
discoveryEnabledAdapter
;
@Bean
public
IRule
ribbonRule
(
IClientConfig
config
)
{
if
(
this
.
propertiesFactory
.
isSet
(
IRule
.
class
,
serviceId
))
{
return
this
.
propertiesFactory
.
get
(
IRule
.
class
,
config
,
serviceId
);
}
if
(
zoneAvoidanceRuleEnabled
)
{
DiscoveryEnabledZoneAvoidanceRule
discoveryEnabledRule
=
new
DiscoveryEnabledZoneAvoidanceRule
();
discoveryEnabledRule
.
initWithNiwsConfig
(
config
);
DiscoveryEnabledZoneAvoidancePredicate
discoveryEnabledPredicate
=
discoveryEnabledRule
.
getDiscoveryEnabledPredicate
();
discoveryEnabledPredicate
.
setPluginAdapter
(
pluginAdapter
);
discoveryEnabledPredicate
.
setDiscoveryEnabledAdapter
(
discoveryEnabledAdapter
);
return
discoveryEnabledRule
;
}
else
{
DiscoveryEnabledBaseRule
discoveryEnabledRule
=
new
DiscoveryEnabledBaseRule
();
DiscoveryEnabledBasePredicate
discoveryEnabledPredicate
=
discoveryEnabledRule
.
getDiscoveryEnabledPredicate
();
discoveryEnabledPredicate
.
setPluginAdapter
(
pluginAdapter
);
discoveryEnabledPredicate
.
setDiscoveryEnabledAdapter
(
discoveryEnabledAdapter
);
return
discoveryEnabledRule
;
}
}
}
\ No newline at end of file
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/constant/StrategyConstant.java
View file @
d4b03539
...
@@ -11,4 +11,5 @@ package com.nepxion.discovery.plugin.strategy.constant;
...
@@ -11,4 +11,5 @@ package com.nepxion.discovery.plugin.strategy.constant;
public
class
StrategyConstant
{
public
class
StrategyConstant
{
public
static
final
String
SPRING_APPLICATION_STRATEGY_CONTROL_ENABLED
=
"spring.application.strategy.control.enabled"
;
public
static
final
String
SPRING_APPLICATION_STRATEGY_CONTROL_ENABLED
=
"spring.application.strategy.control.enabled"
;
public
static
final
String
SPRING_APPLICATION_STRATEGY_ZONE_AVOIDANCE_RULE_ENABLED
=
"spring.application.strategy.zone.avoidance.rule.enabled"
;
}
}
\ No newline at end of file
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/discovery/DiscoveryEnabledPredicate.java
→
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/discovery/DiscoveryEnabled
Base
Predicate.java
View file @
d4b03539
...
@@ -16,7 +16,7 @@ import com.netflix.loadbalancer.AbstractServerPredicate;
...
@@ -16,7 +16,7 @@ import com.netflix.loadbalancer.AbstractServerPredicate;
import
com.netflix.loadbalancer.PredicateKey
;
import
com.netflix.loadbalancer.PredicateKey
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.Server
;
public
class
DiscoveryEnabledPredicate
extends
AbstractServerPredicate
{
public
class
DiscoveryEnabled
Base
Predicate
extends
AbstractServerPredicate
{
protected
PluginAdapter
pluginAdapter
;
protected
PluginAdapter
pluginAdapter
;
protected
DiscoveryEnabledAdapter
discoveryEnabledAdapter
;
protected
DiscoveryEnabledAdapter
discoveryEnabledAdapter
;
...
...
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/discovery/DiscoveryEnabledRule.java
→
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/discovery/DiscoveryEnabled
Base
Rule.java
View file @
d4b03539
...
@@ -9,38 +9,34 @@ package com.nepxion.discovery.plugin.strategy.discovery;
...
@@ -9,38 +9,34 @@ package com.nepxion.discovery.plugin.strategy.discovery;
* @version 1.0
* @version 1.0
*/
*/
import
org.springframework.util.Assert
;
import
com.nepxion.discovery.plugin.framework.decorator.PredicateBasedRuleDecorator
;
import
com.nepxion.discovery.plugin.framework.decorator.ZoneAvoidanceRuleDecorator
;
import
com.netflix.loadbalancer.AbstractServerPredicate
;
import
com.netflix.loadbalancer.AbstractServerPredicate
;
import
com.netflix.loadbalancer.AvailabilityPredicate
;
import
com.netflix.loadbalancer.AvailabilityPredicate
;
import
com.netflix.loadbalancer.CompositePredicate
;
import
com.netflix.loadbalancer.CompositePredicate
;
public
class
DiscoveryEnabled
Rule
extends
ZoneAvoidance
RuleDecorator
{
public
class
DiscoveryEnabled
BaseRule
extends
PredicateBased
RuleDecorator
{
private
final
CompositePredicate
p
redicate
;
private
CompositePredicate
compositeP
redicate
;
private
final
DiscoveryEnabled
Predicate
discoveryEnabledPredicate
;
private
DiscoveryEnabledBase
Predicate
discoveryEnabledPredicate
;
public
DiscoveryEnabledRule
()
{
public
DiscoveryEnabledBaseRule
()
{
this
(
new
DiscoveryEnabledPredicate
());
discoveryEnabledPredicate
=
new
DiscoveryEnabledBasePredicate
();
AvailabilityPredicate
availabilityPredicate
=
new
AvailabilityPredicate
(
this
,
null
);
compositePredicate
=
createCompositePredicate
(
discoveryEnabledPredicate
,
availabilityPredicate
);
}
}
p
ublic
DiscoveryEnabledRule
(
DiscoveryEnabledPredicate
discoveryEnabled
Predicate
)
{
p
rivate
CompositePredicate
createCompositePredicate
(
DiscoveryEnabledBasePredicate
discoveryEnabledPredicate
,
AvailabilityPredicate
availability
Predicate
)
{
Assert
.
notNull
(
discoveryEnabledPredicate
,
"Parameter 'discoveryEnabledPredicate' can't be null"
);
return
CompositePredicate
.
withPredicates
(
discoveryEnabledPredicate
,
availabilityPredicate
)
// .addFallbackPredicate(availabilityPredicate)
this
.
predicate
=
createCompositePredicate
(
discoveryEnabledPredicate
,
new
AvailabilityPredicate
(
this
,
null
));
// .addFallbackPredicate(AbstractServerPredicate.alwaysTrue())
this
.
discoveryEnabledPredicate
=
discoveryEnabledPredicate
;
.
build
()
;
}
}
@Override
@Override
public
AbstractServerPredicate
getPredicate
()
{
public
AbstractServerPredicate
getPredicate
()
{
return
p
redicate
;
return
compositeP
redicate
;
}
}
public
DiscoveryEnabledPredicate
getDiscoveryEnabledPredicate
()
{
public
DiscoveryEnabled
Base
Predicate
getDiscoveryEnabledPredicate
()
{
return
discoveryEnabledPredicate
;
return
discoveryEnabledPredicate
;
}
}
private
CompositePredicate
createCompositePredicate
(
DiscoveryEnabledPredicate
discoveryEnabledPredicate
,
AvailabilityPredicate
availabilityPredicate
)
{
return
CompositePredicate
.
withPredicates
(
discoveryEnabledPredicate
,
availabilityPredicate
).
build
();
}
}
}
\ No newline at end of file
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/discovery/DiscoveryEnabledZoneAvoidancePredicate.java
0 → 100644
View file @
d4b03539
package
com
.
nepxion
.
discovery
.
plugin
.
strategy
.
discovery
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
java.util.Map
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.loadbalancer.IRule
;
import
com.netflix.loadbalancer.LoadBalancerStats
;
import
com.netflix.loadbalancer.PredicateKey
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ZoneAvoidancePredicate
;
public
class
DiscoveryEnabledZoneAvoidancePredicate
extends
ZoneAvoidancePredicate
{
protected
PluginAdapter
pluginAdapter
;
protected
DiscoveryEnabledAdapter
discoveryEnabledAdapter
;
public
DiscoveryEnabledZoneAvoidancePredicate
(
IRule
rule
,
IClientConfig
clientConfig
)
{
super
(
rule
,
clientConfig
);
}
public
DiscoveryEnabledZoneAvoidancePredicate
(
LoadBalancerStats
lbStats
,
IClientConfig
clientConfig
)
{
super
(
lbStats
,
clientConfig
);
}
@Override
public
boolean
apply
(
PredicateKey
input
)
{
boolean
enabled
=
super
.
apply
(
input
);
if
(!
enabled
)
{
return
false
;
}
return
apply
(
input
.
getServer
());
}
protected
boolean
apply
(
Server
server
)
{
Map
<
String
,
String
>
metadata
=
pluginAdapter
.
getServerMetadata
(
server
);
return
discoveryEnabledAdapter
.
apply
(
server
,
metadata
);
}
public
void
setPluginAdapter
(
PluginAdapter
pluginAdapter
)
{
this
.
pluginAdapter
=
pluginAdapter
;
}
public
void
setDiscoveryEnabledAdapter
(
DiscoveryEnabledAdapter
discoveryEnabledAdapter
)
{
this
.
discoveryEnabledAdapter
=
discoveryEnabledAdapter
;
}
}
\ No newline at end of file
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/discovery/DiscoveryEnabledZoneAvoidanceRule.java
0 → 100644
View file @
d4b03539
package
com
.
nepxion
.
discovery
.
plugin
.
strategy
.
discovery
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
com.nepxion.discovery.plugin.framework.decorator.ZoneAvoidanceRuleDecorator
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.loadbalancer.AbstractServerPredicate
;
import
com.netflix.loadbalancer.AvailabilityPredicate
;
import
com.netflix.loadbalancer.CompositePredicate
;
public
class
DiscoveryEnabledZoneAvoidanceRule
extends
ZoneAvoidanceRuleDecorator
{
private
CompositePredicate
compositePredicate
;
private
DiscoveryEnabledZoneAvoidancePredicate
discoveryEnabledPredicate
;
public
DiscoveryEnabledZoneAvoidanceRule
()
{
super
();
discoveryEnabledPredicate
=
new
DiscoveryEnabledZoneAvoidancePredicate
(
this
,
null
);
AvailabilityPredicate
availabilityPredicate
=
new
AvailabilityPredicate
(
this
,
null
);
compositePredicate
=
createCompositePredicate
(
discoveryEnabledPredicate
,
availabilityPredicate
);
}
private
CompositePredicate
createCompositePredicate
(
DiscoveryEnabledZoneAvoidancePredicate
discoveryEnabledPredicate
,
AvailabilityPredicate
availabilityPredicate
)
{
return
CompositePredicate
.
withPredicates
(
discoveryEnabledPredicate
,
availabilityPredicate
)
// .addFallbackPredicate(availabilityPredicate)
// .addFallbackPredicate(AbstractServerPredicate.alwaysTrue())
.
build
();
}
@Override
public
void
initWithNiwsConfig
(
IClientConfig
clientConfig
)
{
discoveryEnabledPredicate
=
new
DiscoveryEnabledZoneAvoidancePredicate
(
this
,
clientConfig
);
AvailabilityPredicate
availabilityPredicate
=
new
AvailabilityPredicate
(
this
,
clientConfig
);
compositePredicate
=
createCompositePredicate
(
discoveryEnabledPredicate
,
availabilityPredicate
);
}
@Override
public
AbstractServerPredicate
getPredicate
()
{
return
compositePredicate
;
}
public
DiscoveryEnabledZoneAvoidancePredicate
getDiscoveryEnabledPredicate
()
{
return
discoveryEnabledPredicate
;
}
}
\ 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