eureka实现黑名单功能

eureka类图结构关系如下:

1 重新定义注册类PeerAwareInstanceRegistryImpl

package com.cyb.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.eureka.EurekaServerConfig;
import com.netflix.eureka.resources.ServerCodecs;

@Component
public class MyFactory implements BeanPostProcessor,ApplicationContextAware{


	public ApplicationContext ac;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.ac = applicationContext;
	}
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if("peerAwareInstanceRegistry".equals(beanName)){//bean.getClass().toString().contains("InstanceRegistry")
			System.out.println(bean+","+beanName);
			MyInstanceRegistryDecorator mird = new MyInstanceRegistryDecorator(
					ac.getBean(EurekaServerConfig.class), 
					ac.getBean(EurekaClientConfig.class),  
					ac.getBean(ServerCodecs.class), 
					ac.getBean(EurekaClient.class));
			mird.setSetting(ac.getBean(ClientBlackSetting.class));
			mird.setApplicationContext(ac);
			return mird;
		}
		/*if(beanName.contains("person")){
			Person p = (Person)bean;
			System.out.println("before: "+p.getName()+","+bean);
			Person newBean = new Person();
			newBean.setName("chenyuanbao");
			bean = newBean;
		}*/
		return bean;
	}
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		/*if(beanName.contains("person")){
			Person p = (Person)bean;
			System.out.println("after: "+p.getName()+","+bean);
		}*/
		return bean;
	}
}

  2 定义自己的装饰模式的注册类

package com.kiiik.config;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.cloud.netflix.eureka.server.InstanceRegistry;
import org.springframework.core.env.Environment;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.eureka.EurekaServerConfig;
import com.netflix.eureka.resources.ServerCodecs;

public class MyInstanceRegistryDecorator extends InstanceRegistry {
	private ClientBlackSetting setting;

	public MyInstanceRegistryDecorator(EurekaServerConfig serverConfig, EurekaClientConfig clientConfig,
			ServerCodecs serverCodecs, EurekaClient eurekaClient
	/* int expectedNumberOfRenewsPerMin,int defaultOpenForTrafficCount */) {
		super(serverConfig, clientConfig, serverCodecs, eurekaClient, 1, 1);
	}

	public void setSetting(ClientBlackSetting setting) {
		this.setting = setting;
	}

	@Override
	public void register(InstanceInfo info, int leaseDuration, boolean isReplication) {
		System.out.println("============my=============");
		if (checkRegister(info)) {
			super.register(info, leaseDuration, isReplication);
		}

	}

	@Override
	public void register(final InstanceInfo info, final boolean isReplication) {
		System.out.println("============my=============");
		if (checkRegister(info)) {
			super.register(info, isReplication);
		}
	}

	// pub 方法
	public boolean checkRegister(InstanceInfo clientInstance) {
		boolean allow = true;
		if(setting.isOpen()){//开启黑名单功能
			if (setting.getIps().contains(clientInstance.getIPAddr())) {
				StringBuffer sb = new StringBuffer().append("客户端[" + clientInstance.getAppName())
						.append("," + clientInstance.getInstanceId()).append("," + clientInstance.getIPAddr())
						.append("," + clientInstance.getPort() + "被拒绝注册!");
				System.err.println(sb.toString());
				allow =  false;//不允许注册
			}
			if(allow){//查看匹配规则
				for(String p:setting.getPattern()){
					Pattern pattern = Pattern.compile(p);
					Matcher matcher = pattern.matcher(clientInstance.getIPAddr());
					if(matcher.find()){
						allow = false;
						break;//中断校验
					}
				}
			}else{
			}
		}else{
		}
		return allow;
		/*
		 * if(){//正则表达式校验
		 * 
		 * }
		 */
		// 判断配置文件是否开启eureka黑白名单的过滤机制,默认开启
		// Boolean registerControlEnabled =
		// isRegisterControlEnabled(environment);
		// registration.getEurekaClient().getEurekaClientConfig().
		/*StringBuffer sb = new StringBuffer().append("," + clientInstance.getAppName())
				.append("," + clientInstance.getInstanceId()).append("," + clientInstance.getIPAddr())
				.append("," + clientInstance.getPort());
		System.err.println(sb.toString());*/
		// return true;

	}

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

3 查看注册情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值