Commit 31738bd2 by Nepxion

重构策略算法

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