Skip to content

Commit

Permalink
支持同时多个DiscoveryEnabledStrategy的联合判断;支持网关动态选择子环境的功能;修复子环境过滤为判断元数据中的env…
Browse files Browse the repository at this point in the history
…为空的Bug
  • Loading branch information
HaojunRen committed Dec 31, 2019
1 parent 09b55fb commit ad0f254
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,6 +30,10 @@ public void onGetServers(String serviceId, List<? extends Server> servers) {

private void applyEnvironmentFilter(String providerServiceId, List<? extends Server> servers) {
String environment = pluginAdapter.getEnvironment();
if (StringUtils.equals(environment, DiscoveryConstant.DEFAULT)) {
return;
}

boolean validated = validate(servers, environment);
Iterator<? extends Server> iterator = servers.iterator();
while (iterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +26,7 @@

public class DefaultDiscoveryEnabledAdapter implements DiscoveryEnabledAdapter {
@Autowired(required = false)
private DiscoveryEnabledStrategy discoveryEnabledStrategy;
private List<DiscoveryEnabledStrategy> discoveryEnabledStrategyList;

@Autowired
private DiscoveryMatcherStrategy discoveryMatcherStrategy;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.nepxion.discovery.plugin.strategy.adapter;

/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @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);
}
}

0 comments on commit ad0f254

Please sign in to comment.