Commit 5d6c43fa by Nepxion

不仅支持ipAddress过滤,也支持hostName过滤

parent d034238f
...@@ -34,7 +34,7 @@ import com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity; ...@@ -34,7 +34,7 @@ import com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity;
import com.nepxion.discovery.plugin.framework.entity.DiscoveryServiceEntity; import com.nepxion.discovery.plugin.framework.entity.DiscoveryServiceEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterHolderEntity; import com.nepxion.discovery.plugin.framework.entity.FilterHolderEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterType; import com.nepxion.discovery.plugin.framework.entity.FilterType;
import com.nepxion.discovery.plugin.framework.entity.IpAddressFilterEntity; import com.nepxion.discovery.plugin.framework.entity.HostFilterEntity;
import com.nepxion.discovery.plugin.framework.entity.RegisterEntity; import com.nepxion.discovery.plugin.framework.entity.RegisterEntity;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.entity.VersionFilterEntity; import com.nepxion.discovery.plugin.framework.entity.VersionFilterEntity;
...@@ -115,9 +115,9 @@ public class ConfigParser extends Dom4JParser implements PluginConfigParser { ...@@ -115,9 +115,9 @@ public class ConfigParser extends Dom4JParser implements PluginConfigParser {
Element childElement = (Element) childElementObject; Element childElement = (Element) childElementObject;
if (StringUtils.equals(childElement.getName(), ConfigConstant.BLACKLIST_ELEMENT_NAME)) { if (StringUtils.equals(childElement.getName(), ConfigConstant.BLACKLIST_ELEMENT_NAME)) {
parseIpAddressFilter(childElement, ConfigConstant.BLACKLIST_ELEMENT_NAME, registerEntity); parseHostFilter(childElement, ConfigConstant.BLACKLIST_ELEMENT_NAME, registerEntity);
} else if (StringUtils.equals(childElement.getName(), ConfigConstant.WHITELIST_ELEMENT_NAME)) { } else if (StringUtils.equals(childElement.getName(), ConfigConstant.WHITELIST_ELEMENT_NAME)) {
parseIpAddressFilter(childElement, ConfigConstant.WHITELIST_ELEMENT_NAME, registerEntity); parseHostFilter(childElement, ConfigConstant.WHITELIST_ELEMENT_NAME, registerEntity);
} else if (StringUtils.equals(childElement.getName(), ConfigConstant.COUNT_ELEMENT_NAME)) { } else if (StringUtils.equals(childElement.getName(), ConfigConstant.COUNT_ELEMENT_NAME)) {
parseCountFilter(childElement, registerEntity); parseCountFilter(childElement, registerEntity);
} }
...@@ -133,9 +133,9 @@ public class ConfigParser extends Dom4JParser implements PluginConfigParser { ...@@ -133,9 +133,9 @@ public class ConfigParser extends Dom4JParser implements PluginConfigParser {
Element childElement = (Element) childElementObject; Element childElement = (Element) childElementObject;
if (StringUtils.equals(childElement.getName(), ConfigConstant.BLACKLIST_ELEMENT_NAME)) { if (StringUtils.equals(childElement.getName(), ConfigConstant.BLACKLIST_ELEMENT_NAME)) {
parseIpAddressFilter(childElement, ConfigConstant.BLACKLIST_ELEMENT_NAME, discoveryEntity); parseHostFilter(childElement, ConfigConstant.BLACKLIST_ELEMENT_NAME, discoveryEntity);
} else if (StringUtils.equals(childElement.getName(), ConfigConstant.WHITELIST_ELEMENT_NAME)) { } else if (StringUtils.equals(childElement.getName(), ConfigConstant.WHITELIST_ELEMENT_NAME)) {
parseIpAddressFilter(childElement, ConfigConstant.WHITELIST_ELEMENT_NAME, discoveryEntity); parseHostFilter(childElement, ConfigConstant.WHITELIST_ELEMENT_NAME, discoveryEntity);
} else if (StringUtils.equals(childElement.getName(), ConfigConstant.VERSION_ELEMENT_NAME)) { } else if (StringUtils.equals(childElement.getName(), ConfigConstant.VERSION_ELEMENT_NAME)) {
parseVersionFilter(childElement, discoveryEntity); parseVersionFilter(childElement, discoveryEntity);
} }
...@@ -144,23 +144,23 @@ public class ConfigParser extends Dom4JParser implements PluginConfigParser { ...@@ -144,23 +144,23 @@ public class ConfigParser extends Dom4JParser implements PluginConfigParser {
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private void parseIpAddressFilter(Element element, String filterTypeValue, FilterHolderEntity filterHolderEntity) { private void parseHostFilter(Element element, String filterTypeValue, FilterHolderEntity filterHolderEntity) {
IpAddressFilterEntity ipAddressFilterEntity = filterHolderEntity.getIpAddressFilterEntity(); HostFilterEntity hostFilterEntity = filterHolderEntity.getHostFilterEntity();
if (ipAddressFilterEntity != null) { if (hostFilterEntity != null) {
throw new PluginException("Allow only one filter element to be configed, [" + ConfigConstant.BLACKLIST_ELEMENT_NAME + "] or [" + ConfigConstant.WHITELIST_ELEMENT_NAME + "]"); throw new PluginException("Allow only one filter element to be configed, [" + ConfigConstant.BLACKLIST_ELEMENT_NAME + "] or [" + ConfigConstant.WHITELIST_ELEMENT_NAME + "]");
} }
ipAddressFilterEntity = new IpAddressFilterEntity(); hostFilterEntity = new HostFilterEntity();
ipAddressFilterEntity.setFilterType(FilterType.fromString(filterTypeValue)); hostFilterEntity.setFilterType(FilterType.fromString(filterTypeValue));
Attribute globalFilterAttribute = element.attribute(ConfigConstant.FILTER_VALUE_ATTRIBUTE_NAME); Attribute globalFilterAttribute = element.attribute(ConfigConstant.FILTER_VALUE_ATTRIBUTE_NAME);
if (globalFilterAttribute != null) { if (globalFilterAttribute != null) {
String globalFilterValue = globalFilterAttribute.getData().toString().trim(); String globalFilterValue = globalFilterAttribute.getData().toString().trim();
List<String> globalFilterValueList = parseList(globalFilterValue); List<String> globalFilterValueList = parseList(globalFilterValue);
ipAddressFilterEntity.setFilterValueList(globalFilterValueList); hostFilterEntity.setFilterValueList(globalFilterValueList);
} }
Map<String, List<String>> filterMap = ipAddressFilterEntity.getFilterMap(); Map<String, List<String>> filterMap = hostFilterEntity.getFilterMap();
for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) { for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) {
Object childElementObject = elementIterator.next(); Object childElementObject = elementIterator.next();
if (childElementObject instanceof Element) { if (childElementObject instanceof Element) {
...@@ -184,7 +184,7 @@ public class ConfigParser extends Dom4JParser implements PluginConfigParser { ...@@ -184,7 +184,7 @@ public class ConfigParser extends Dom4JParser implements PluginConfigParser {
} }
} }
filterHolderEntity.setIpAddressFilterEntity(ipAddressFilterEntity); filterHolderEntity.setHostFilterEntity(hostFilterEntity);
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
......
...@@ -20,13 +20,13 @@ import com.nepxion.discovery.plugin.framework.event.PluginEventWapper; ...@@ -20,13 +20,13 @@ import com.nepxion.discovery.plugin.framework.event.PluginEventWapper;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher; import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
import com.nepxion.discovery.plugin.framework.event.PluginSubscriber; import com.nepxion.discovery.plugin.framework.event.PluginSubscriber;
import com.nepxion.discovery.plugin.framework.listener.discovery.DiscoveryListenerExecutor; import com.nepxion.discovery.plugin.framework.listener.discovery.DiscoveryListenerExecutor;
import com.nepxion.discovery.plugin.framework.listener.discovery.IpAddressFilterDiscoveryListener; import com.nepxion.discovery.plugin.framework.listener.discovery.HostFilterDiscoveryListener;
import com.nepxion.discovery.plugin.framework.listener.discovery.VersionFilterDiscoveryListener; import com.nepxion.discovery.plugin.framework.listener.discovery.VersionFilterDiscoveryListener;
import com.nepxion.discovery.plugin.framework.listener.loadbalance.IpAddressFilterLoadBalanceListener; import com.nepxion.discovery.plugin.framework.listener.loadbalance.HostFilterLoadBalanceListener;
import com.nepxion.discovery.plugin.framework.listener.loadbalance.LoadBalanceListenerExecutor; import com.nepxion.discovery.plugin.framework.listener.loadbalance.LoadBalanceListenerExecutor;
import com.nepxion.discovery.plugin.framework.listener.loadbalance.VersionFilterLoadBalanceListener; import com.nepxion.discovery.plugin.framework.listener.loadbalance.VersionFilterLoadBalanceListener;
import com.nepxion.discovery.plugin.framework.listener.register.CountFilterRegisterListener; import com.nepxion.discovery.plugin.framework.listener.register.CountFilterRegisterListener;
import com.nepxion.discovery.plugin.framework.listener.register.IpAddressFilterRegisterListener; import com.nepxion.discovery.plugin.framework.listener.register.HostFilterRegisterListener;
import com.nepxion.discovery.plugin.framework.listener.register.RegisterListenerExecutor; import com.nepxion.discovery.plugin.framework.listener.register.RegisterListenerExecutor;
@Configuration @Configuration
...@@ -82,8 +82,8 @@ public class PluginAutoConfiguration { ...@@ -82,8 +82,8 @@ public class PluginAutoConfiguration {
} }
@Bean @Bean
public IpAddressFilterRegisterListener ipAddressFilterRegisterListener() { public HostFilterRegisterListener hostFilterRegisterListener() {
return new IpAddressFilterRegisterListener(); return new HostFilterRegisterListener();
} }
@Bean @Bean
...@@ -92,8 +92,8 @@ public class PluginAutoConfiguration { ...@@ -92,8 +92,8 @@ public class PluginAutoConfiguration {
} }
@Bean @Bean
public IpAddressFilterDiscoveryListener ipAddressFilterDiscoveryListener() { public HostFilterDiscoveryListener hostFilterDiscoveryListener() {
return new IpAddressFilterDiscoveryListener(); return new HostFilterDiscoveryListener();
} }
@Bean @Bean
...@@ -102,8 +102,8 @@ public class PluginAutoConfiguration { ...@@ -102,8 +102,8 @@ public class PluginAutoConfiguration {
} }
@Bean @Bean
public IpAddressFilterLoadBalanceListener ipAddressFilterLoadBalanceListener() { public HostFilterLoadBalanceListener hostFilterLoadBalanceListener() {
return new IpAddressFilterLoadBalanceListener(); return new HostFilterLoadBalanceListener();
} }
@Bean @Bean
......
...@@ -19,18 +19,18 @@ import org.apache.commons.lang3.builder.ToStringStyle; ...@@ -19,18 +19,18 @@ import org.apache.commons.lang3.builder.ToStringStyle;
public class FilterHolderEntity implements Serializable { public class FilterHolderEntity implements Serializable {
private static final long serialVersionUID = 8767022123685151416L; private static final long serialVersionUID = 8767022123685151416L;
private IpAddressFilterEntity ipAddressFilterEntity; private HostFilterEntity hostFilterEntity;
public FilterHolderEntity() { public FilterHolderEntity() {
} }
public IpAddressFilterEntity getIpAddressFilterEntity() { public HostFilterEntity getHostFilterEntity() {
return ipAddressFilterEntity; return hostFilterEntity;
} }
public void setIpAddressFilterEntity(IpAddressFilterEntity ipAddressFilterEntity) { public void setHostFilterEntity(HostFilterEntity hostFilterEntity) {
this.ipAddressFilterEntity = ipAddressFilterEntity; this.hostFilterEntity = hostFilterEntity;
} }
@Override @Override
......
...@@ -19,14 +19,14 @@ import org.apache.commons.lang3.builder.HashCodeBuilder; ...@@ -19,14 +19,14 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.commons.lang3.builder.ToStringStyle;
public class IpAddressFilterEntity implements Serializable { public class HostFilterEntity implements Serializable {
private static final long serialVersionUID = 3830016495318834467L; private static final long serialVersionUID = 3830016495318834467L;
private FilterType filterType; private FilterType filterType;
private List<String> filterValueList; private List<String> filterValueList;
private Map<String, List<String>> filterMap = new LinkedHashMap<String, List<String>>(); private Map<String, List<String>> filterMap = new LinkedHashMap<String, List<String>>();
public IpAddressFilterEntity() { public HostFilterEntity() {
} }
......
...@@ -18,14 +18,14 @@ public class RegisterFailureEvent implements Serializable { ...@@ -18,14 +18,14 @@ public class RegisterFailureEvent implements Serializable {
private String eventDescription; private String eventDescription;
private String serviceId; private String serviceId;
private String ipAddress; private String host;
private int port; private int port;
public RegisterFailureEvent(String eventType, String eventDescription, String serviceId, String ipAddress, int port) { public RegisterFailureEvent(String eventType, String eventDescription, String serviceId, String host, int port) {
this.eventType = eventType; this.eventType = eventType;
this.eventDescription = eventDescription; this.eventDescription = eventDescription;
this.serviceId = serviceId; this.serviceId = serviceId;
this.ipAddress = ipAddress; this.host = host;
this.port = port; this.port = port;
} }
...@@ -41,8 +41,8 @@ public class RegisterFailureEvent implements Serializable { ...@@ -41,8 +41,8 @@ public class RegisterFailureEvent implements Serializable {
return serviceId; return serviceId;
} }
public String getIpAddress() { public String getHost() {
return ipAddress; return host;
} }
public int getPort() { public int getPort() {
......
...@@ -17,7 +17,7 @@ import org.springframework.cloud.client.ServiceInstance; ...@@ -17,7 +17,7 @@ import org.springframework.cloud.client.ServiceInstance;
// 因为内置监听触发的时候,需要优先过滤,所以顺序执行 // 因为内置监听触发的时候,需要优先过滤,所以顺序执行
public class DiscoveryListenerExecutor { public class DiscoveryListenerExecutor {
@Autowired @Autowired
private IpAddressFilterDiscoveryListener ipAddressFilterDiscoveryListener; private HostFilterDiscoveryListener hostFilterDiscoveryListener;
@Autowired @Autowired
private VersionFilterDiscoveryListener versionFilterDiscoveryListener; private VersionFilterDiscoveryListener versionFilterDiscoveryListener;
...@@ -26,22 +26,22 @@ public class DiscoveryListenerExecutor { ...@@ -26,22 +26,22 @@ public class DiscoveryListenerExecutor {
private List<DiscoveryListener> discoveryListenerList; private List<DiscoveryListener> discoveryListenerList;
public void onGetInstances(String serviceId, List<ServiceInstance> instances) { public void onGetInstances(String serviceId, List<ServiceInstance> instances) {
ipAddressFilterDiscoveryListener.onGetInstances(serviceId, instances); hostFilterDiscoveryListener.onGetInstances(serviceId, instances);
versionFilterDiscoveryListener.onGetInstances(serviceId, instances); versionFilterDiscoveryListener.onGetInstances(serviceId, instances);
for (DiscoveryListener discoveryListener : discoveryListenerList) { for (DiscoveryListener discoveryListener : discoveryListenerList) {
if (discoveryListener != ipAddressFilterDiscoveryListener && discoveryListener != versionFilterDiscoveryListener) { if (discoveryListener != hostFilterDiscoveryListener && discoveryListener != versionFilterDiscoveryListener) {
discoveryListener.onGetInstances(serviceId, instances); discoveryListener.onGetInstances(serviceId, instances);
} }
} }
} }
public void onGetServices(List<String> services) { public void onGetServices(List<String> services) {
ipAddressFilterDiscoveryListener.onGetServices(services); hostFilterDiscoveryListener.onGetServices(services);
versionFilterDiscoveryListener.onGetServices(services); versionFilterDiscoveryListener.onGetServices(services);
for (DiscoveryListener discoveryListener : discoveryListenerList) { for (DiscoveryListener discoveryListener : discoveryListenerList) {
if (discoveryListener != ipAddressFilterDiscoveryListener && discoveryListener != versionFilterDiscoveryListener) { if (discoveryListener != hostFilterDiscoveryListener && discoveryListener != versionFilterDiscoveryListener) {
discoveryListener.onGetServices(services); discoveryListener.onGetServices(services);
} }
} }
......
...@@ -20,16 +20,16 @@ import org.springframework.cloud.client.ServiceInstance; ...@@ -20,16 +20,16 @@ import org.springframework.cloud.client.ServiceInstance;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant; import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity; import com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterType; import com.nepxion.discovery.plugin.framework.entity.FilterType;
import com.nepxion.discovery.plugin.framework.entity.IpAddressFilterEntity; import com.nepxion.discovery.plugin.framework.entity.HostFilterEntity;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
public class IpAddressFilterDiscoveryListener extends AbstractDiscoveryListener { public class HostFilterDiscoveryListener extends AbstractDiscoveryListener {
@Override @Override
public void onGetInstances(String serviceId, List<ServiceInstance> instances) { public void onGetInstances(String serviceId, List<ServiceInstance> instances) {
applyIpAddressFilter(serviceId, instances); applyHostFilter(serviceId, instances);
} }
private void applyIpAddressFilter(String providerServiceId, List<ServiceInstance> instances) { private void applyHostFilter(String providerServiceId, List<ServiceInstance> instances) {
RuleEntity ruleEntity = ruleCache.get(PluginConstant.RULE); RuleEntity ruleEntity = ruleCache.get(PluginConstant.RULE);
if (ruleEntity == null) { if (ruleEntity == null) {
return; return;
...@@ -40,15 +40,15 @@ public class IpAddressFilterDiscoveryListener extends AbstractDiscoveryListener ...@@ -40,15 +40,15 @@ public class IpAddressFilterDiscoveryListener extends AbstractDiscoveryListener
return; return;
} }
IpAddressFilterEntity ipAddressFilterEntity = discoveryEntity.getIpAddressFilterEntity(); HostFilterEntity hostFilterEntity = discoveryEntity.getHostFilterEntity();
if (ipAddressFilterEntity == null) { if (hostFilterEntity == null) {
return; return;
} }
FilterType filterType = ipAddressFilterEntity.getFilterType(); FilterType filterType = hostFilterEntity.getFilterType();
List<String> globalFilterValueList = ipAddressFilterEntity.getFilterValueList(); List<String> globalFilterValueList = hostFilterEntity.getFilterValueList();
Map<String, List<String>> filterMap = ipAddressFilterEntity.getFilterMap(); Map<String, List<String>> filterMap = hostFilterEntity.getFilterMap();
List<String> filterValueList = filterMap.get(providerServiceId); List<String> filterValueList = filterMap.get(providerServiceId);
List<String> allFilterValueList = new ArrayList<String>(); List<String> allFilterValueList = new ArrayList<String>();
...@@ -79,9 +79,9 @@ public class IpAddressFilterDiscoveryListener extends AbstractDiscoveryListener ...@@ -79,9 +79,9 @@ public class IpAddressFilterDiscoveryListener extends AbstractDiscoveryListener
} }
} }
private boolean validateBlacklist(List<String> allFilterValueList, String ipAddress) { private boolean validateBlacklist(List<String> allFilterValueList, String host) {
for (String filterValue : allFilterValueList) { for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) { if (host.startsWith(filterValue)) {
return true; return true;
} }
} }
...@@ -89,10 +89,10 @@ public class IpAddressFilterDiscoveryListener extends AbstractDiscoveryListener ...@@ -89,10 +89,10 @@ public class IpAddressFilterDiscoveryListener extends AbstractDiscoveryListener
return false; return false;
} }
private boolean validateWhitelist(List<String> allFilterValueList, String ipAddress) { private boolean validateWhitelist(List<String> allFilterValueList, String host) {
boolean matched = true; boolean matched = true;
for (String filterValue : allFilterValueList) { for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) { if (host.startsWith(filterValue)) {
matched = false; matched = false;
break; break;
} }
......
...@@ -19,17 +19,17 @@ import org.apache.commons.collections4.CollectionUtils; ...@@ -19,17 +19,17 @@ import org.apache.commons.collections4.CollectionUtils;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant; import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity; import com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterType; import com.nepxion.discovery.plugin.framework.entity.FilterType;
import com.nepxion.discovery.plugin.framework.entity.IpAddressFilterEntity; import com.nepxion.discovery.plugin.framework.entity.HostFilterEntity;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
public class IpAddressFilterLoadBalanceListener extends AbstractLoadBalanceListener { public class HostFilterLoadBalanceListener extends AbstractLoadBalanceListener {
@Override @Override
public void onGetServers(String serviceId, List<? extends Server> servers) { public void onGetServers(String serviceId, List<? extends Server> servers) {
applyIpAddressFilter(serviceId, servers); applyHostFilter(serviceId, servers);
} }
private void applyIpAddressFilter(String providerServiceId, List<? extends Server> servers) { private void applyHostFilter(String providerServiceId, List<? extends Server> servers) {
RuleEntity ruleEntity = ruleCache.get(PluginConstant.RULE); RuleEntity ruleEntity = ruleCache.get(PluginConstant.RULE);
if (ruleEntity == null) { if (ruleEntity == null) {
return; return;
...@@ -40,15 +40,15 @@ public class IpAddressFilterLoadBalanceListener extends AbstractLoadBalanceListe ...@@ -40,15 +40,15 @@ public class IpAddressFilterLoadBalanceListener extends AbstractLoadBalanceListe
return; return;
} }
IpAddressFilterEntity ipAddressFilterEntity = discoveryEntity.getIpAddressFilterEntity(); HostFilterEntity hostFilterEntity = discoveryEntity.getHostFilterEntity();
if (ipAddressFilterEntity == null) { if (hostFilterEntity == null) {
return; return;
} }
FilterType filterType = ipAddressFilterEntity.getFilterType(); FilterType filterType = hostFilterEntity.getFilterType();
List<String> globalFilterValueList = ipAddressFilterEntity.getFilterValueList(); List<String> globalFilterValueList = hostFilterEntity.getFilterValueList();
Map<String, List<String>> filterMap = ipAddressFilterEntity.getFilterMap(); Map<String, List<String>> filterMap = hostFilterEntity.getFilterMap();
List<String> filterValueList = filterMap.get(providerServiceId); List<String> filterValueList = filterMap.get(providerServiceId);
List<String> allFilterValueList = new ArrayList<String>(); List<String> allFilterValueList = new ArrayList<String>();
...@@ -79,9 +79,9 @@ public class IpAddressFilterLoadBalanceListener extends AbstractLoadBalanceListe ...@@ -79,9 +79,9 @@ public class IpAddressFilterLoadBalanceListener extends AbstractLoadBalanceListe
} }
} }
private boolean validateBlacklist(List<String> allFilterValueList, String ipAddress) { private boolean validateBlacklist(List<String> allFilterValueList, String host) {
for (String filterValue : allFilterValueList) { for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) { if (host.startsWith(filterValue)) {
return true; return true;
} }
} }
...@@ -89,10 +89,10 @@ public class IpAddressFilterLoadBalanceListener extends AbstractLoadBalanceListe ...@@ -89,10 +89,10 @@ public class IpAddressFilterLoadBalanceListener extends AbstractLoadBalanceListe
return false; return false;
} }
private boolean validateWhitelist(List<String> allFilterValueList, String ipAddress) { private boolean validateWhitelist(List<String> allFilterValueList, String host) {
boolean matched = true; boolean matched = true;
for (String filterValue : allFilterValueList) { for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) { if (host.startsWith(filterValue)) {
matched = false; matched = false;
break; break;
} }
......
...@@ -19,7 +19,7 @@ import com.netflix.loadbalancer.ZoneAwareLoadBalancer; ...@@ -19,7 +19,7 @@ import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
// 因为内置监听触发的时候,需要优先过滤,所以顺序执行 // 因为内置监听触发的时候,需要优先过滤,所以顺序执行
public class LoadBalanceListenerExecutor { public class LoadBalanceListenerExecutor {
@Autowired @Autowired
private IpAddressFilterLoadBalanceListener ipAddressFilterLoadBalanceListener; private HostFilterLoadBalanceListener hostFilterLoadBalanceListener;
@Autowired @Autowired
private VersionFilterLoadBalanceListener versionFilterLoadBalanceListener; private VersionFilterLoadBalanceListener versionFilterLoadBalanceListener;
...@@ -27,7 +27,7 @@ public class LoadBalanceListenerExecutor { ...@@ -27,7 +27,7 @@ public class LoadBalanceListenerExecutor {
private ZoneAwareLoadBalancer<?> loadBalancer; private ZoneAwareLoadBalancer<?> loadBalancer;
public void onGetServers(String serviceId, List<? extends Server> servers) { public void onGetServers(String serviceId, List<? extends Server> servers) {
ipAddressFilterLoadBalanceListener.onGetServers(serviceId, servers); hostFilterLoadBalanceListener.onGetServers(serviceId, servers);
versionFilterLoadBalanceListener.onGetServers(serviceId, servers); versionFilterLoadBalanceListener.onGetServers(serviceId, servers);
} }
......
...@@ -29,13 +29,13 @@ public class CountFilterRegisterListener extends AbstractRegisterListener { ...@@ -29,13 +29,13 @@ public class CountFilterRegisterListener extends AbstractRegisterListener {
@Override @Override
public void onRegister(Registration registration) { public void onRegister(Registration registration) {
String serviceId = registration.getServiceId(); String serviceId = registration.getServiceId();
String ipAddress = pluginAdapter.getHost(registration); String host = pluginAdapter.getHost(registration);
int port = pluginAdapter.getPort(registration); int port = pluginAdapter.getPort(registration);
applyCountFilter(serviceId, ipAddress, port); applyCountFilter(serviceId, host, port);
} }
private void applyCountFilter(String serviceId, String ipAddress, int port) { private void applyCountFilter(String serviceId, String host, int port) {
RuleEntity ruleEntity = ruleCache.get(PluginConstant.RULE); RuleEntity ruleEntity = ruleCache.get(PluginConstant.RULE);
if (ruleEntity == null) { if (ruleEntity == null) {
return; return;
...@@ -69,16 +69,16 @@ public class CountFilterRegisterListener extends AbstractRegisterListener { ...@@ -69,16 +69,16 @@ public class CountFilterRegisterListener extends AbstractRegisterListener {
int count = discoveryClient.getRealInstances(serviceId).size(); int count = discoveryClient.getRealInstances(serviceId).size();
if (count >= maxCount) { if (count >= maxCount) {
onRegisterFailure(maxCount, serviceId, ipAddress, port); onRegisterFailure(maxCount, serviceId, host, port);
} }
} }
private void onRegisterFailure(int maxCount, String serviceId, String ipAddress, int port) { private void onRegisterFailure(int maxCount, String serviceId, String host, int port) {
String description = ipAddress + " isn't allowed to register to Register server, reach max limited count=" + maxCount; String description = host + " isn't allowed to register to Register server, reach max limited count=" + maxCount;
Boolean registerFailureEventEnabled = pluginContextAware.getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_REGISTER_FAILURE_EVENT_ENABLED, Boolean.class, Boolean.FALSE); Boolean registerFailureEventEnabled = pluginContextAware.getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_REGISTER_FAILURE_EVENT_ENABLED, Boolean.class, Boolean.FALSE);
if (registerFailureEventEnabled) { if (registerFailureEventEnabled) {
pluginPublisher.asyncPublish(new RegisterFailureEvent(PluginConstant.REACH_MAX_LIMITED_COUNT, description, serviceId, ipAddress, port)); pluginPublisher.asyncPublish(new RegisterFailureEvent(PluginConstant.REACH_MAX_LIMITED_COUNT, description, serviceId, host, port));
} }
throw new PluginException(description); throw new PluginException(description);
......
...@@ -18,23 +18,23 @@ import org.springframework.cloud.client.serviceregistry.Registration; ...@@ -18,23 +18,23 @@ import org.springframework.cloud.client.serviceregistry.Registration;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant; import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.entity.FilterType; import com.nepxion.discovery.plugin.framework.entity.FilterType;
import com.nepxion.discovery.plugin.framework.entity.IpAddressFilterEntity; import com.nepxion.discovery.plugin.framework.entity.HostFilterEntity;
import com.nepxion.discovery.plugin.framework.entity.RegisterEntity; import com.nepxion.discovery.plugin.framework.entity.RegisterEntity;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.RegisterFailureEvent; import com.nepxion.discovery.plugin.framework.event.RegisterFailureEvent;
import com.nepxion.discovery.plugin.framework.exception.PluginException; import com.nepxion.discovery.plugin.framework.exception.PluginException;
public class IpAddressFilterRegisterListener extends AbstractRegisterListener { public class HostFilterRegisterListener extends AbstractRegisterListener {
@Override @Override
public void onRegister(Registration registration) { public void onRegister(Registration registration) {
String serviceId = registration.getServiceId(); String serviceId = registration.getServiceId();
String ipAddress = pluginAdapter.getHost(registration); String host = pluginAdapter.getHost(registration);
int port = pluginAdapter.getPort(registration); int port = pluginAdapter.getPort(registration);
applyIpAddressFilter(serviceId, ipAddress, port); applyHostFilter(serviceId, host, port);
} }
private void applyIpAddressFilter(String serviceId, String ipAddress, int port) { private void applyHostFilter(String serviceId, String host, int port) {
RuleEntity ruleEntity = ruleCache.get(PluginConstant.RULE); RuleEntity ruleEntity = ruleCache.get(PluginConstant.RULE);
if (ruleEntity == null) { if (ruleEntity == null) {
return; return;
...@@ -45,15 +45,15 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener { ...@@ -45,15 +45,15 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
return; return;
} }
IpAddressFilterEntity ipAddressFilterEntity = registerEntity.getIpAddressFilterEntity(); HostFilterEntity hostFilterEntity = registerEntity.getHostFilterEntity();
if (ipAddressFilterEntity == null) { if (hostFilterEntity == null) {
return; return;
} }
FilterType filterType = ipAddressFilterEntity.getFilterType(); FilterType filterType = hostFilterEntity.getFilterType();
List<String> globalFilterValueList = ipAddressFilterEntity.getFilterValueList(); List<String> globalFilterValueList = hostFilterEntity.getFilterValueList();
Map<String, List<String>> filterMap = ipAddressFilterEntity.getFilterMap(); Map<String, List<String>> filterMap = hostFilterEntity.getFilterMap();
List<String> filterValueList = filterMap.get(serviceId); List<String> filterValueList = filterMap.get(serviceId);
List<String> allFilterValueList = new ArrayList<String>(); List<String> allFilterValueList = new ArrayList<String>();
...@@ -67,42 +67,42 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener { ...@@ -67,42 +67,42 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
switch (filterType) { switch (filterType) {
case BLACKLIST: case BLACKLIST:
validateBlacklist(filterType, allFilterValueList, serviceId, ipAddress, port); validateBlacklist(filterType, allFilterValueList, serviceId, host, port);
break; break;
case WHITELIST: case WHITELIST:
validateWhitelist(filterType, allFilterValueList, serviceId, ipAddress, port); validateWhitelist(filterType, allFilterValueList, serviceId, host, port);
break; break;
} }
} }
private void validateBlacklist(FilterType filterType, List<String> allFilterValueList, String serviceId, String ipAddress, int port) { private void validateBlacklist(FilterType filterType, List<String> allFilterValueList, String serviceId, String host, int port) {
for (String filterValue : allFilterValueList) { for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) { if (host.startsWith(filterValue)) {
onRegisterFailure(filterType, allFilterValueList, serviceId, ipAddress, port); onRegisterFailure(filterType, allFilterValueList, serviceId, host, port);
} }
} }
} }
private void validateWhitelist(FilterType filterType, List<String> allFilterValueList, String serviceId, String ipAddress, int port) { private void validateWhitelist(FilterType filterType, List<String> allFilterValueList, String serviceId, String host, int port) {
boolean matched = true; boolean matched = true;
for (String filterValue : allFilterValueList) { for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) { if (host.startsWith(filterValue)) {
matched = false; matched = false;
break; break;
} }
} }
if (matched) { if (matched) {
onRegisterFailure(filterType, allFilterValueList, serviceId, ipAddress, port); onRegisterFailure(filterType, allFilterValueList, serviceId, host, port);
} }
} }
private void onRegisterFailure(FilterType filterType, List<String> allFilterValueList, String serviceId, String ipAddress, int port) { private void onRegisterFailure(FilterType filterType, List<String> allFilterValueList, String serviceId, String host, int port) {
String description = ipAddress + " isn't allowed to register to Register server, not match IP address " + filterType + "=" + allFilterValueList; String description = host + " isn't allowed to register to Register server, not match host " + filterType + "=" + allFilterValueList;
Boolean registerFailureEventEnabled = pluginContextAware.getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_REGISTER_FAILURE_EVENT_ENABLED, Boolean.class, Boolean.FALSE); Boolean registerFailureEventEnabled = pluginContextAware.getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_REGISTER_FAILURE_EVENT_ENABLED, Boolean.class, Boolean.FALSE);
if (registerFailureEventEnabled) { if (registerFailureEventEnabled) {
pluginPublisher.asyncPublish(new RegisterFailureEvent(filterType.toString(), description, serviceId, ipAddress, port)); pluginPublisher.asyncPublish(new RegisterFailureEvent(filterType.toString(), description, serviceId, host, port));
} }
throw new PluginException(description); throw new PluginException(description);
......
...@@ -20,52 +20,52 @@ public class RegisterListenerExecutor { ...@@ -20,52 +20,52 @@ public class RegisterListenerExecutor {
private CountFilterRegisterListener countFilterRegisterListener; private CountFilterRegisterListener countFilterRegisterListener;
@Autowired @Autowired
private IpAddressFilterRegisterListener ipAddressFilterRegisterListener; private HostFilterRegisterListener hostFilterRegisterListener;
@Autowired @Autowired
private List<RegisterListener> registerListenerList; private List<RegisterListener> registerListenerList;
public void onRegister(Registration registration) { public void onRegister(Registration registration) {
for (RegisterListener registerListener : registerListenerList) { for (RegisterListener registerListener : registerListenerList) {
if (registerListener != countFilterRegisterListener && registerListener != ipAddressFilterRegisterListener) { if (registerListener != countFilterRegisterListener && registerListener != hostFilterRegisterListener) {
registerListener.onRegister(registration); registerListener.onRegister(registration);
} }
} }
countFilterRegisterListener.onRegister(registration); countFilterRegisterListener.onRegister(registration);
ipAddressFilterRegisterListener.onRegister(registration); hostFilterRegisterListener.onRegister(registration);
} }
public void onDeregister(Registration registration) { public void onDeregister(Registration registration) {
for (RegisterListener registerListener : registerListenerList) { for (RegisterListener registerListener : registerListenerList) {
if (registerListener != countFilterRegisterListener && registerListener != ipAddressFilterRegisterListener) { if (registerListener != countFilterRegisterListener && registerListener != hostFilterRegisterListener) {
registerListener.onDeregister(registration); registerListener.onDeregister(registration);
} }
} }
countFilterRegisterListener.onDeregister(registration); countFilterRegisterListener.onDeregister(registration);
ipAddressFilterRegisterListener.onDeregister(registration); hostFilterRegisterListener.onDeregister(registration);
} }
public void onSetStatus(Registration registration, String status) { public void onSetStatus(Registration registration, String status) {
for (RegisterListener registerListener : registerListenerList) { for (RegisterListener registerListener : registerListenerList) {
if (registerListener != countFilterRegisterListener && registerListener != ipAddressFilterRegisterListener) { if (registerListener != countFilterRegisterListener && registerListener != hostFilterRegisterListener) {
registerListener.onSetStatus(registration, status); registerListener.onSetStatus(registration, status);
} }
} }
countFilterRegisterListener.onSetStatus(registration, status); countFilterRegisterListener.onSetStatus(registration, status);
ipAddressFilterRegisterListener.onSetStatus(registration, status); hostFilterRegisterListener.onSetStatus(registration, status);
} }
public void onClose() { public void onClose() {
for (RegisterListener registerListener : registerListenerList) { for (RegisterListener registerListener : registerListenerList) {
if (registerListener != countFilterRegisterListener && registerListener != ipAddressFilterRegisterListener) { if (registerListener != countFilterRegisterListener && registerListener != hostFilterRegisterListener) {
registerListener.onClose(); registerListener.onClose();
} }
} }
countFilterRegisterListener.onClose(); countFilterRegisterListener.onClose();
ipAddressFilterRegisterListener.onClose(); hostFilterRegisterListener.onClose();
} }
} }
\ No newline at end of file
...@@ -17,6 +17,6 @@ import com.nepxion.eventbus.annotation.EventBus; ...@@ -17,6 +17,6 @@ import com.nepxion.eventbus.annotation.EventBus;
public class MySubscriber { public class MySubscriber {
@Subscribe @Subscribe
public void onRegisterFailure(RegisterFailureEvent registerFailureEvent) { public void onRegisterFailure(RegisterFailureEvent registerFailureEvent) {
System.out.println("========== 注册失败:eventType=" + registerFailureEvent.getEventType() + ", eventDescription=" + registerFailureEvent.getEventDescription() + ", serviceId=" + registerFailureEvent.getServiceId() + ", ipAddress=" + registerFailureEvent.getIpAddress() + ", port=" + registerFailureEvent.getPort()); System.out.println("========== 注册失败:eventType=" + registerFailureEvent.getEventType() + ", eventDescription=" + registerFailureEvent.getEventDescription() + ", serviceId=" + registerFailureEvent.getServiceId() + ", host=" + registerFailureEvent.getHost() + ", port=" + registerFailureEvent.getPort());
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment