Eureka的黑白名单过滤机制(Eureka的注册黑白名单)

参考链接.Eureka的注册黑白名单

不过这篇博文只提供了实现思路和大致,我这边帮忙把完整代码贴出来.

通过springboot的autoconfigure实现.大致思路是用自己定义的Eureka注册包装类替换原来的Eureka注册类.

当Eureka客户端执行注册逻辑时,会先执行EurekaServiceRegistryDecorator类里的register方法,然后再调用父类的register方法.

这样的话,装饰类内部相当于做了一次过滤逻辑的处理,在执行父类逻辑前,过滤,进行黑白名单的检测

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @Author: hkr
 * @Date: 19-6-4 下午4:46
 * @Description
 */
public class EurekaApplicationContextInitializer implements ApplicationContextInitializer {

    public void initialize(final ConfigurableApplicationContext applicationContext) {

        applicationContext.getBeanFactory().addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

                if (bean instanceof EurekaServiceRegistry) {
                    EurekaServiceRegistry eurekaServiceRegistry = (EurekaServiceRegistry) bean;
                    return new EurekaServiceRegistryDecorator(eurekaServiceRegistry, applicationContext);
                }

                return bean;
            }
        });
    }

}
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

/**
 * @Author: hkr
 * @Date: 19-6-4 下午4:50
 * @Description
 */
public class EurekaServiceRegistryDecorator extends EurekaServiceRegistry {

    private EurekaServiceRegistry serviceRegistry;
    private ConfigurableEnvironment environment;

    public EurekaServiceRegistryDecorator(EurekaServiceRegistry serviceRegistry, ConfigurableApplicationContext applicationContext) {
        this.serviceRegistry = serviceRegistry;
        this.environment = applicationContext.getEnvironment();
    }

    @Override
    public void register(EurekaRegistration registration) {
        //判断配置文件是否开启eureka黑白名单的过滤机制,默认开启
        Boolean registerControlEnabled = isRegisterControlEnabled(environment);
        if (registerControlEnabled) {
            EurekaInstanceConfigBean eurekaInstanceConfigBean = (EurekaInstanceConfigBean) registration.getInstanceConfig();
            if(--这里写你自己的逻辑操作--){
                
            }else{
                serviceRegistry.register(registration);
            }
        }else {
            serviceRegistry.register(registration);
        }

    }

    public static Boolean isRegisterControlEnabled(Environment environment) {
        return environment.getProperty("spring.application.register.control.enabled", Boolean.class, Boolean.TRUE);
    }
}

 

你可能感兴趣的:(springcloud)