diff --git a/discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/listener/loadbalance/EnvironmentFilterLoadBalanceListener.java b/discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/listener/loadbalance/EnvironmentFilterLoadBalanceListener.java index e737be2614..13ddd27371 100644 --- a/discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/listener/loadbalance/EnvironmentFilterLoadBalanceListener.java +++ b/discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/listener/loadbalance/EnvironmentFilterLoadBalanceListener.java @@ -15,6 +15,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import com.nepxion.discovery.common.constant.DiscoveryConstant; import com.nepxion.discovery.plugin.framework.adapter.EnvironmentRouteAdapter; import com.netflix.loadbalancer.Server; @@ -29,6 +30,10 @@ public void onGetServers(String serviceId, List servers) { private void applyEnvironmentFilter(String providerServiceId, List servers) { String environment = pluginAdapter.getEnvironment(); + if (StringUtils.equals(environment, DiscoveryConstant.DEFAULT)) { + return; + } + boolean validated = validate(servers, environment); Iterator iterator = servers.iterator(); while (iterator.hasNext()) { diff --git a/discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/configuration/GatewayStrategyAutoConfiguration.java b/discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/configuration/GatewayStrategyAutoConfiguration.java index 9a07f4a35e..73bfbd2c37 100644 --- a/discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/configuration/GatewayStrategyAutoConfiguration.java +++ b/discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/configuration/GatewayStrategyAutoConfiguration.java @@ -16,8 +16,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import com.nepxion.discovery.common.constant.DiscoveryConstant; import com.nepxion.discovery.plugin.strategy.adapter.DefaultDiscoveryEnabledAdapter; +import com.nepxion.discovery.plugin.strategy.adapter.DefaultEnvironmentDiscoveryEnabledStrategy; import com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter; +import com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy; import com.nepxion.discovery.plugin.strategy.constant.StrategyConstant; import com.nepxion.discovery.plugin.strategy.gateway.filter.DefaultGatewayStrategyClearFilter; import com.nepxion.discovery.plugin.strategy.gateway.filter.DefaultGatewayStrategyRouteFilter; @@ -57,6 +60,12 @@ public DiscoveryEnabledAdapter discoveryEnabledAdapter() { return new DefaultDiscoveryEnabledAdapter(); } + @Bean + @ConditionalOnProperty(value = DiscoveryConstant.SPRING_APPLICATION_ENVIRONMENT_ISOLATION_ENABLED, matchIfMissing = false) + public DiscoveryEnabledStrategy environmentDiscoveryEnabledStrategy() { + return new DefaultEnvironmentDiscoveryEnabledStrategy(); + } + @Bean @ConditionalOnProperty(value = StrategyConstant.SPRING_APPLICATION_STRATEGY_HYSTRIX_THREADLOCAL_SUPPORTED, matchIfMissing = false) public CallableWrapper callableWrapper() { diff --git a/discovery-plugin-strategy-starter-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/zuul/configuration/ZuulStrategyAutoConfiguration.java b/discovery-plugin-strategy-starter-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/zuul/configuration/ZuulStrategyAutoConfiguration.java index e49afbae41..c6eb8f573e 100644 --- a/discovery-plugin-strategy-starter-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/zuul/configuration/ZuulStrategyAutoConfiguration.java +++ b/discovery-plugin-strategy-starter-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/zuul/configuration/ZuulStrategyAutoConfiguration.java @@ -16,8 +16,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import com.nepxion.discovery.common.constant.DiscoveryConstant; import com.nepxion.discovery.plugin.strategy.adapter.DefaultDiscoveryEnabledAdapter; +import com.nepxion.discovery.plugin.strategy.adapter.DefaultEnvironmentDiscoveryEnabledStrategy; import com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter; +import com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy; import com.nepxion.discovery.plugin.strategy.constant.StrategyConstant; import com.nepxion.discovery.plugin.strategy.wrapper.CallableWrapper; import com.nepxion.discovery.plugin.strategy.zuul.filter.DefaultZuulStrategyClearFilter; @@ -58,6 +61,12 @@ public DiscoveryEnabledAdapter discoveryEnabledAdapter() { return new DefaultDiscoveryEnabledAdapter(); } + @Bean + @ConditionalOnProperty(value = DiscoveryConstant.SPRING_APPLICATION_ENVIRONMENT_ISOLATION_ENABLED, matchIfMissing = false) + public DiscoveryEnabledStrategy environmentDiscoveryEnabledStrategy() { + return new DefaultEnvironmentDiscoveryEnabledStrategy(); + } + @Bean @ConditionalOnProperty(value = StrategyConstant.SPRING_APPLICATION_STRATEGY_HYSTRIX_THREADLOCAL_SUPPORTED, matchIfMissing = false) public CallableWrapper callableWrapper() { diff --git a/discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DefaultDiscoveryEnabledAdapter.java b/discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DefaultDiscoveryEnabledAdapter.java index 1ae7282936..a280ccdd07 100644 --- a/discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DefaultDiscoveryEnabledAdapter.java +++ b/discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DefaultDiscoveryEnabledAdapter.java @@ -14,6 +14,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.CollectionUtils; import com.nepxion.discovery.common.constant.DiscoveryConstant; import com.nepxion.discovery.common.util.JsonUtil; @@ -25,7 +26,7 @@ public class DefaultDiscoveryEnabledAdapter implements DiscoveryEnabledAdapter { @Autowired(required = false) - private DiscoveryEnabledStrategy discoveryEnabledStrategy; + private List discoveryEnabledStrategyList; @Autowired private DiscoveryMatcherStrategy discoveryMatcherStrategy; @@ -164,11 +165,18 @@ private boolean applyAddress(Server server) { } private boolean applyStrategy(Server server) { - if (discoveryEnabledStrategy == null) { + if (CollectionUtils.isEmpty(discoveryEnabledStrategyList)) { return true; } - return discoveryEnabledStrategy.apply(server); + for (DiscoveryEnabledStrategy discoveryEnabledStrategy : discoveryEnabledStrategyList) { + boolean enabled = discoveryEnabledStrategy.apply(server); + if (!enabled) { + return false; + } + } + + return true; } public PluginAdapter getPluginAdapter() { diff --git a/discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DefaultEnvironmentDiscoveryEnabledStrategy.java b/discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DefaultEnvironmentDiscoveryEnabledStrategy.java new file mode 100644 index 0000000000..f17e84e2e6 --- /dev/null +++ b/discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DefaultEnvironmentDiscoveryEnabledStrategy.java @@ -0,0 +1,37 @@ +package com.nepxion.discovery.plugin.strategy.adapter; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import org.apache.commons.lang3.StringUtils; + +import com.nepxion.discovery.common.constant.DiscoveryConstant; +import com.netflix.loadbalancer.Server; + +public class DefaultEnvironmentDiscoveryEnabledStrategy extends DefaultDiscoveryEnabledStrategy { + @Override + public boolean apply(Server server) { + String environment = pluginAdapter.getEnvironment(); + // 本地配置了environment,说明本服务(一般来说是网关)也处在子环境里,则由EnvironmentFilterLoadBalanceListener方式去执行环境隔离,返回true + if (!StringUtils.equals(environment, DiscoveryConstant.DEFAULT)) { + return true; + } + + String headerEnvironment = strategyContextHolder.getHeader(DiscoveryConstant.ENVIRONMENT); + // 传入headerEnvironment为空,返回true + if (StringUtils.isEmpty(headerEnvironment)) { + return true; + } + + String serverEnvironment = pluginAdapter.getServerEnvironment(server); + + // 本服务(一般来说是网关)不隶属任何子环境,由它来调度子环境的服务调用 + return StringUtils.equals(headerEnvironment, serverEnvironment); + } +} \ No newline at end of file