Commit 31738bd2 by Nepxion

重构策略算法

parent 1af9e1e4
...@@ -25,12 +25,14 @@ import com.nepxion.discovery.plugin.framework.cache.PluginCache; ...@@ -25,12 +25,14 @@ import com.nepxion.discovery.plugin.framework.cache.PluginCache;
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.DiscoveryServiceEntity; import com.nepxion.discovery.plugin.framework.entity.DiscoveryServiceEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterEntity;
import com.nepxion.discovery.plugin.framework.entity.FilterType;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.entity.VersionEntity; import com.nepxion.discovery.plugin.framework.entity.VersionEntity;
public class DiscoveryControlStrategy { public class DiscoveryControlStrategy {
@Autowired @Autowired
private RuleEntity pluginEntity; private RuleEntity ruleEntity;
@Autowired @Autowired
private PluginCache pluginCache; private PluginCache pluginCache;
...@@ -51,24 +53,53 @@ public class DiscoveryControlStrategy { ...@@ -51,24 +53,53 @@ public class DiscoveryControlStrategy {
} }
private void applyIpAddressFilter(String providerServiceId, List<ServiceInstance> instances) { private void applyIpAddressFilter(String providerServiceId, List<ServiceInstance> instances) {
String filterIpAddress = pluginCache.get(providerServiceId); DiscoveryEntity discoveryEntity = ruleEntity.getDiscoveryEntity();
if (StringUtils.isNotEmpty(filterIpAddress)) { if (discoveryEntity == null) {
return;
}
FilterEntity filterEntity = discoveryEntity.getFilterEntity();
if (filterEntity == null) {
return;
}
FilterType filterType = filterEntity.getFilterType();
List<String> globalFilterValueList = filterEntity.getFilterValueList();
Map<String, List<String>> filterMap = filterEntity.getFilterMap();
List<String> filterValueList = filterMap.get(providerServiceId);
List<String> allFilterValueList = new ArrayList<String>();
if (CollectionUtils.isNotEmpty(globalFilterValueList)) {
allFilterValueList.addAll(globalFilterValueList);
}
if (CollectionUtils.isNotEmpty(filterValueList)) {
allFilterValueList.addAll(filterValueList);
}
Iterator<ServiceInstance> iterator = instances.iterator(); Iterator<ServiceInstance> iterator = instances.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
ServiceInstance serviceInstance = iterator.next(); ServiceInstance serviceInstance = iterator.next();
String host = serviceInstance.getHost(); String host = serviceInstance.getHost();
boolean valid = validateBlacklist(filterIpAddress, host); switch (filterType) {
if (valid) { case BLACKLIST:
if (validateBlacklist(allFilterValueList, host)) {
iterator.remove();
}
break;
case WHITELIST:
if (validateWhitelist(allFilterValueList, host)) {
iterator.remove(); iterator.remove();
} }
break;
} }
} }
} }
private boolean validateBlacklist(String filterIpAddress, String ipAddress) { private boolean validateBlacklist(List<String> allFilterValueList, String ipAddress) {
String[] filterArray = StringUtils.split(ipAddress, PluginConstant.SEPARATE); for (String filterValue : allFilterValueList) {
for (String filter : filterArray) { if (ipAddress.startsWith(filterValue)) {
if (ipAddress.startsWith(filter)) {
return true; return true;
} }
} }
...@@ -76,13 +107,25 @@ public class DiscoveryControlStrategy { ...@@ -76,13 +107,25 @@ public class DiscoveryControlStrategy {
return false; return false;
} }
private boolean validateWhitelist(List<String> allFilterValueList, String ipAddress) {
boolean matched = true;
for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) {
matched = false;
break;
}
}
return matched;
}
private void applyVersionFilter(String consumerServiceId, String consumerServiceVersion, String providerServiceId, List<ServiceInstance> instances) { private void applyVersionFilter(String consumerServiceId, String consumerServiceVersion, String providerServiceId, List<ServiceInstance> instances) {
// 如果消费端未配置版本号,那么它可以调用提供端所有服务,需要符合规范,极力避免该情况发生 // 如果消费端未配置版本号,那么它可以调用提供端所有服务,需要符合规范,极力避免该情况发生
if (StringUtils.isEmpty(consumerServiceVersion)) { if (StringUtils.isEmpty(consumerServiceVersion)) {
return; return;
} }
DiscoveryEntity discoveryEntity = pluginEntity.getDiscoveryEntity(); DiscoveryEntity discoveryEntity = ruleEntity.getDiscoveryEntity();
if (discoveryEntity == null) { if (discoveryEntity == null) {
return; return;
} }
......
...@@ -83,8 +83,8 @@ public class RegisterControlStrategy { ...@@ -83,8 +83,8 @@ public class RegisterControlStrategy {
private void validateBlacklist(List<String> allFilterValueList, String ipAddress) { private void validateBlacklist(List<String> allFilterValueList, String ipAddress) {
LOG.info("********** IP address blacklist={}, current ip address={} **********", allFilterValueList, ipAddress); LOG.info("********** IP address blacklist={}, current ip address={} **********", allFilterValueList, ipAddress);
for (String filter : allFilterValueList) { for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filter)) { if (ipAddress.startsWith(filterValue)) {
throw new PluginException(ipAddress + " isn't allowed to register to Eureka server, because it is in blacklist"); throw new PluginException(ipAddress + " isn't allowed to register to Eureka server, because it is in blacklist");
} }
} }
...@@ -93,15 +93,15 @@ public class RegisterControlStrategy { ...@@ -93,15 +93,15 @@ public class RegisterControlStrategy {
private void validateWhitelist(List<String> allFilterValueList, String ipAddress) { private void validateWhitelist(List<String> allFilterValueList, String ipAddress) {
LOG.info("********** IP address whitelist={}, current ip address={} **********", allFilterValueList, ipAddress); LOG.info("********** IP address whitelist={}, current ip address={} **********", allFilterValueList, ipAddress);
boolean valid = false; boolean matched = true;
for (String filter : allFilterValueList) { for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filter)) { if (ipAddress.startsWith(filterValue)) {
valid = true; matched = false;
break; break;
} }
} }
if (!valid) { if (matched) {
throw new PluginException(ipAddress + " isn't allowed to register to Eureka server, because it isn't in whitelist"); throw new PluginException(ipAddress + " isn't allowed to register to Eureka server, because it isn't in whitelist");
} }
} }
......
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