Commit d4b03539 by Nepxion

支持策略的规则可配置化

parent 0d384df0
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
...@@ -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
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
...@@ -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
...@@ -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 DiscoveryEnabledBasePredicate extends AbstractServerPredicate {
protected PluginAdapter pluginAdapter; protected PluginAdapter pluginAdapter;
protected DiscoveryEnabledAdapter discoveryEnabledAdapter; protected DiscoveryEnabledAdapter discoveryEnabledAdapter;
......
...@@ -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 DiscoveryEnabledRule extends ZoneAvoidanceRuleDecorator { public class DiscoveryEnabledBaseRule extends PredicateBasedRuleDecorator {
private final CompositePredicate predicate; private CompositePredicate compositePredicate;
private final DiscoveryEnabledPredicate discoveryEnabledPredicate; private DiscoveryEnabledBasePredicate discoveryEnabledPredicate;
public DiscoveryEnabledRule() { public DiscoveryEnabledBaseRule() {
this(new DiscoveryEnabledPredicate()); discoveryEnabledPredicate = new DiscoveryEnabledBasePredicate();
AvailabilityPredicate availabilityPredicate = new AvailabilityPredicate(this, null);
compositePredicate = createCompositePredicate(discoveryEnabledPredicate, availabilityPredicate);
} }
public DiscoveryEnabledRule(DiscoveryEnabledPredicate discoveryEnabledPredicate) { private CompositePredicate createCompositePredicate(DiscoveryEnabledBasePredicate discoveryEnabledPredicate, AvailabilityPredicate availabilityPredicate) {
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 predicate; return compositePredicate;
} }
public DiscoveryEnabledPredicate getDiscoveryEnabledPredicate() { public DiscoveryEnabledBasePredicate getDiscoveryEnabledPredicate() {
return discoveryEnabledPredicate; return discoveryEnabledPredicate;
} }
private CompositePredicate createCompositePredicate(DiscoveryEnabledPredicate discoveryEnabledPredicate, AvailabilityPredicate availabilityPredicate) {
return CompositePredicate.withPredicates(discoveryEnabledPredicate, availabilityPredicate).build();
}
} }
\ No newline at end of file
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
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment