org.zodiac.loadbalancer.ribbon.rule.WeightAwareRule Maven / Gradle / Ivy
package org.zodiac.loadbalancer.ribbon.rule;
import com.netflix.loadbalancer.Server;
import org.zodiac.core.application.metadata.GenericMetadata;
import org.zodiac.loadbalancer.ribbon.WeightdServerChooser;
import org.zodiac.loadbalancer.ribbon.predicate.DiscoveryEnabledPredicate;
import org.zodiac.loadbalancer.ribbon.predicate.MetadataAwarePredicate;
import org.zodiac.loadbalancer.ribbon.predicate.MetadataServer;
import java.util.Collections;
import java.util.List;
public class WeightAwareRule extends DiscoveryEnabledRule {
@Deprecated
public WeightAwareRule() {
this(null);
}
public WeightAwareRule(DiscoveryEnabledPredicate discoveryEnabledPredicate) {
super(null != discoveryEnabledPredicate ? discoveryEnabledPredicate : MetadataAwarePredicate.DEFAULT_INSTANCE);
}
@Override
public List filterServers(List serverList) {
// Chooser instanceChooser = new Chooser<>("platfrom.ribbon.rule");
//
// List> hostsWithWeight =
// serverList.stream().map(serviceInstance -> new Pair<>(serviceInstance, getWeight(serviceInstance)))
// .collect(Collectors.toList());
//
// instanceChooser.refresh(hostsWithWeight);
// Server server = instanceChooser.randomWithWeight();
WeightdServerChooser chooser = new WeightdServerChooser();
Server server = chooser.choose(serverList);
return Collections.singletonList(server);
}
/**
* Get {@link Server} weight metadata.
*
* @param server Server instance
* @return The weight of the instance.
*/
protected Double getWeight(Server server) {
MetadataServer metadataServer = new MetadataServer(server);
return Double.parseDouble(metadataServer.getMetadata().get(GenericMetadata.WEIGHT));
}
}