org.zodiac.loadbalancer.ribbon.rule.DiscoveryEnabledRule Maven / Gradle / Ivy
package org.zodiac.loadbalancer.ribbon.rule;
import org.zodiac.loadbalancer.ribbon.predicate.DiscoveryEnabledPredicate;
import org.zodiac.sdk.toolkit.util.AssertUtil;
import com.netflix.loadbalancer.AvailabilityPredicate;
import com.netflix.loadbalancer.CompositePredicate;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.PredicateBasedRule;
import com.netflix.loadbalancer.Server;
import java.util.List;
public abstract class DiscoveryEnabledRule extends PredicateBasedRule {
private final CompositePredicate predicate;
public DiscoveryEnabledRule(DiscoveryEnabledPredicate discoveryEnabledPredicate) {
AssertUtil.notNull(discoveryEnabledPredicate, "Parameter 'discoveryEnabledPredicate' can't be null");
this.predicate = createCompositePredicate(discoveryEnabledPredicate, new AvailabilityPredicate(this, null));
}
@Override
public CompositePredicate getPredicate() {
return predicate;
}
@Override
public Server choose(Object key) {
ILoadBalancer lb = getLoadBalancer();
List allServers = lb.getAllServers();
/*过滤服务列表。*/
List serverList = filterServers(allServers);
return getPredicate().chooseRoundRobinAfterFiltering(serverList, key).orNull();
}
/**
* 过滤服务。
*
* @param serverList 服务列表
* @return 服务列表
*/
public abstract List filterServers(List serverList);
private CompositePredicate createCompositePredicate(DiscoveryEnabledPredicate discoveryEnabledPredicate,
AvailabilityPredicate availabilityPredicate) {
CompositePredicate.Builder builder =
CompositePredicate.withPredicates(discoveryEnabledPredicate, availabilityPredicate);
return builder.build();
}
}