org.zodiac.loadbalancer.ribbon.predicate.MetadataAwarePredicate Maven / Gradle / Ivy
package org.zodiac.loadbalancer.ribbon.predicate;
import org.zodiac.commons.support.SpringContextHolder;
import org.zodiac.core.application.AppContext;
import org.zodiac.core.bootstrap.discovery.AppDiscoveryStrategyInfo;
import org.zodiac.core.bootstrap.discovery.SameTagPreferredStrategy;
import org.zodiac.loadbalancer.common.constants.LoadBalancerConstants;
import org.zodiac.loadbalancer.ribbon.config.PlatformRibbonRuleInfo;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.LoadBalancerStats;
import java.util.Map;
/**
* 基于 Metadata 的服务筛选。
*
*/
public class MetadataAwarePredicate extends DiscoveryEnabledPredicate {
@Deprecated
public static final MetadataAwarePredicate DEFAULT_INSTANCE = new MetadataAwarePredicate();
private AppDiscoveryStrategyInfo discoveryStrategyInfo;
private PlatformRibbonRuleInfo platfromRibbonRule;
@Deprecated
public MetadataAwarePredicate() {
this(null);
}
public MetadataAwarePredicate(AppDiscoveryStrategyInfo discoveryStrategyInfo) {
this.discoveryStrategyInfo = discoveryStrategyInfo;
}
public MetadataAwarePredicate(AppDiscoveryStrategyInfo discoveryStrategyInfo, PlatformRibbonRuleInfo platfromRibbonRule) {
super();
this.platfromRibbonRule = platfromRibbonRule;
this.discoveryStrategyInfo = discoveryStrategyInfo;
}
public MetadataAwarePredicate(IRule rule, IClientConfig clientConfig, AppDiscoveryStrategyInfo discoveryStrategyInfo, PlatformRibbonRuleInfo platfromRibbonRule) {
super(rule, clientConfig);
this.platfromRibbonRule = platfromRibbonRule;
}
public MetadataAwarePredicate(IRule rule, AppDiscoveryStrategyInfo discoveryStrategyInfo, PlatformRibbonRuleInfo platfromRibbonRule) {
this(rule, null, discoveryStrategyInfo, platfromRibbonRule);
}
public MetadataAwarePredicate(LoadBalancerStats lbStats, IClientConfig clientConfig, AppDiscoveryStrategyInfo discoveryStrategyInfo, PlatformRibbonRuleInfo platfromRibbonRule) {
super(lbStats, clientConfig);
this.discoveryStrategyInfo = discoveryStrategyInfo;
this.platfromRibbonRule = platfromRibbonRule;
}
public PlatformRibbonRuleInfo getPlatfromRibbonRoutingInfo() {
return platfromRibbonRule;
}
@Override
protected void init() {
if (null == this.platfromRibbonRule) {
this.platfromRibbonRule = SpringContextHolder.getBean(PlatformRibbonRuleInfo.class);
}
}
@Override
protected boolean apply(MetadataServer server) {
/*服务里的配置。*/
String localTag = platfromRibbonRule.getTag();
if (StrUtil.isBlank(localTag)) {
return true;
}
Map metadata = server.getMetadata();
/*本地有 tag,服务没有,返回 false。*/
String metaDataTag = metadata.get(LoadBalancerConstants.LOAD_BALANCER_TAG_KEY);
if (StrUtil.isBlank(metaDataTag)) {
/*如果统一发现策略存在,则尝试使用。*/
if (null != discoveryStrategyInfo && discoveryStrategyInfo.isEnabled()) {
SameTagPreferredStrategy sameTagPreferredStrategy = discoveryStrategyInfo.getSameTagPreferred();
if (sameTagPreferredStrategy.isLocalTagEnabled()) {
/*如果开启了local tag,则尝试使用。*/
Map instanceMetadata = obtainInstanceMetadata();
localTag = instanceMetadata.get(sameTagPreferredStrategy.getDefaultTagKey());
}
}
}
if (StrUtil.isBlank(metaDataTag)) {
return false;
}
return metaDataTag.equals(localTag);
}
protected Map obtainInstanceMetadata() {
return AppContext.getInstance().getAppMetadata();
}
}