All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.loadbalancer.ribbon.predicate.DiscoveryEnabledPredicate Maven / Gradle / Ivy

package org.zodiac.loadbalancer.ribbon.predicate;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerPredicate;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.LoadBalancerStats;
import com.netflix.loadbalancer.PredicateKey;

import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;

/**
 * 过滤服务。
 *
 */
public abstract class DiscoveryEnabledPredicate extends AbstractServerPredicate implements InitializingBean {

    protected final Logger log = SmartSlf4jLoggerFactory.getLogger(getClass());

    private final AtomicBoolean initialized = new AtomicBoolean(false);

    public DiscoveryEnabledPredicate() {
        super();
    }

    public DiscoveryEnabledPredicate(IRule rule, IClientConfig clientConfig) {
        super(rule, clientConfig);
    }

    public DiscoveryEnabledPredicate(IRule rule) {
        super(rule);
    }

    public DiscoveryEnabledPredicate(LoadBalancerStats lbStats, IClientConfig clientConfig) {
        super(lbStats, clientConfig);
    }

    @Override
    public boolean apply(@Nullable PredicateKey input) {
        if (input == null) {
            return false;
        }
        MetadataServer metadataServer = new MetadataServer(input.getServer());
        /*支持 metadata 进行判断。*/
        if (metadataServer.hasMetadata()) {
            return apply(metadataServer);
        }
        return false;
    }

    @Override
    public final void afterPropertiesSet() throws Exception {
        if (initialized.compareAndSet(false, true)) {
            try {
                init();
            } catch (Throwable t) {
                initialized.compareAndSet(true, false);
                log.warn("{}", ExceptionUtil.stackTrace(t));
            }
        }
    }

    protected void init() {
    }

    /**
     * Returns whether the specific {@link MetadataServer} matches this predicate.
     *
     * @param server the discovered server
     * @return whether the server matches the predicate
     */
    protected abstract boolean apply(MetadataServer server);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy