From d2fab766c427e83d9dedde8a74c2b58b606c9d00 Mon Sep 17 00:00:00 2001 From: tanghc Date: Wed, 21 Aug 2019 13:55:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E9=9D=A2=E4=BD=BF=E7=94=A8nacos?= =?UTF-8?q?=EF=BC=8C=E8=88=8D=E5=BC=83zookeeper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/docs/files/10110_预发布灰度发布.md | 2 +- .../BaseGatewayConfiguration.java | 5 ++ .../gateway/filter/EnvGrayFilter.java | 46 +++++++++++++++++++ .../gatewaycommon/gateway/filter/Orders.java | 4 +- .../manager/AbstractConfiguration.java | 2 +- .../configuration/BaseZuulConfiguration.java | 6 +-- .../zuul/filter/BaseZuulFilter.java | 4 +- ...isionFilter.java => PreEnvGrayFilter.java} | 6 ++- 8 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/EnvGrayFilter.java rename sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/{PreVersionDecisionFilter.java => PreEnvGrayFilter.java} (92%) diff --git a/doc/docs/files/10110_预发布灰度发布.md b/doc/docs/files/10110_预发布灰度发布.md index fd214d0a..1ec34aff 100644 --- a/doc/docs/files/10110_预发布灰度发布.md +++ b/doc/docs/files/10110_预发布灰度发布.md @@ -42,5 +42,5 @@ pre.domain=openpre.domain.com 参考类: -- PreVersionDecisionFilter.java +- PreEnvGrayFilter.java - EnvironmentServerChooser.java \ No newline at end of file diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java index bb7b50e0..bab2e239 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java @@ -1,6 +1,7 @@ package com.gitee.sop.gatewaycommon.gateway.configuration; import com.gitee.sop.gatewaycommon.bean.ApiConfig; +import com.gitee.sop.gatewaycommon.gateway.filter.EnvGrayFilter; import com.gitee.sop.gatewaycommon.gateway.filter.GatewayModifyResponseGatewayFilter; import com.gitee.sop.gatewaycommon.gateway.filter.LoadBalancerClientExtFilter; import com.gitee.sop.gatewaycommon.gateway.filter.ParameterFormatterFilter; @@ -110,4 +111,8 @@ public class BaseGatewayConfiguration extends AbstractConfiguration { return new ParameterFormatterFilter(); } + @Bean + EnvGrayFilter envGrayFilter() { + return new EnvGrayFilter(); + } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/EnvGrayFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/EnvGrayFilter.java new file mode 100644 index 00000000..38c9aebe --- /dev/null +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/EnvGrayFilter.java @@ -0,0 +1,46 @@ +package com.gitee.sop.gatewaycommon.gateway.filter; + +import com.gitee.sop.gatewaycommon.bean.TargetRoute; +import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil; +import com.gitee.sop.gatewaycommon.manager.EnvGrayManager; +import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; +import com.gitee.sop.gatewaycommon.param.ApiParam; +import com.gitee.sop.gatewaycommon.param.ParamNames; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +/** + * @author tanghc + */ +public class EnvGrayFilter implements GlobalFilter, Ordered { + + @Autowired + private EnvGrayManager envGrayManager; + + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange); + String nameVersion = apiParam.fetchNameVersion(); + TargetRoute targetRoute = RouteRepositoryContext.getRouteRepository().get(nameVersion); + if (targetRoute == null) { + return null; + } + String serviceId = targetRoute.getServiceRouteInfo().fetchServiceIdLowerCase(); + // 如果服务在灰度阶段,返回一个灰度版本号 + String version = envGrayManager.getVersion(serviceId, nameVersion); + if (version != null && envGrayManager.containsKey(serviceId, apiParam.fetchAppKey())) { + ServerWebExchange serverWebExchange = ServerWebExchangeUtil.addHeaders(exchange, httpHeaders -> httpHeaders.set(ParamNames.HEADER_VERSION_NAME, version)); + return chain.filter(serverWebExchange); + } + return chain.filter(exchange); + } + + @Override + public int getOrder() { + return Orders.ENV_GRAY_FILTER_ORDER; + } +} diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/Orders.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/Orders.java index cd3db928..4e561a40 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/Orders.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/Orders.java @@ -18,7 +18,7 @@ public class Orders { /** 验证拦截器order */ public static final int LIMIT_FILTER_ORDER = PRE_ROUTE_PERMISSION_FILTER_ORDER + 100; - /** 决定版本号过滤器 */ - public static final int VERSION_DECISION_FILTER_ORDER = LIMIT_FILTER_ORDER + 100; + /** 灰度发布过滤器 */ + public static final int ENV_GRAY_FILTER_ORDER = LIMIT_FILTER_ORDER + 100; } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java index 34af307c..c13dd47f 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java @@ -105,7 +105,7 @@ public class AbstractConfiguration implements ApplicationContextAware, Applicati @Bean @ConditionalOnMissingBean - EnvGrayManager userKeyManager() { + EnvGrayManager envGrayManager() { return ApiConfig.getInstance().getUserKeyManager(); } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/configuration/BaseZuulConfiguration.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/configuration/BaseZuulConfiguration.java index 07811339..8328813b 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/configuration/BaseZuulConfiguration.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/configuration/BaseZuulConfiguration.java @@ -12,7 +12,7 @@ import com.gitee.sop.gatewaycommon.zuul.filter.PreHttpServletRequestWrapperFilte import com.gitee.sop.gatewaycommon.zuul.filter.PreLimitFilter; import com.gitee.sop.gatewaycommon.zuul.filter.PreParameterFormatterFilter; import com.gitee.sop.gatewaycommon.zuul.filter.PreValidateFilter; -import com.gitee.sop.gatewaycommon.zuul.filter.PreVersionDecisionFilter; +import com.gitee.sop.gatewaycommon.zuul.filter.PreEnvGrayFilter; import com.gitee.sop.gatewaycommon.zuul.filter.Servlet30WrapperFilterExt; import com.gitee.sop.gatewaycommon.zuul.route.SopRouteLocator; import com.gitee.sop.gatewaycommon.zuul.route.ZuulRouteCache; @@ -119,8 +119,8 @@ public class BaseZuulConfiguration extends AbstractConfiguration { * 决定版本号 */ @Bean - PreVersionDecisionFilter preVersionDecisionFilter() { - return new PreVersionDecisionFilter(); + PreEnvGrayFilter preEnvGrayFilter() { + return new PreEnvGrayFilter(); } /** diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/BaseZuulFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/BaseZuulFilter.java index 77851c7b..5ea6fbbd 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/BaseZuulFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/BaseZuulFilter.java @@ -32,8 +32,8 @@ public abstract class BaseZuulFilter extends ZuulFilter { /** 限流过滤 */ public static final int PRE_LIMIT_FILTER_ORDER = PRE_ROUTE_PERMISSION_FILTER_ORDER + 100; - /** 决定版本号过滤器 */ - public static final int PRE_VERSION_DECISION_FILTER_ORDER = PRE_LIMIT_FILTER_ORDER + 100; + /** 灰度发布过滤器 */ + public static final int PRE_ENV_GRAY_FILTER_ORDER = PRE_LIMIT_FILTER_ORDER + 100; private Integer filterOrder; diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreVersionDecisionFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreEnvGrayFilter.java similarity index 92% rename from sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreVersionDecisionFilter.java rename to sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreEnvGrayFilter.java index 72bd4b7b..7245cb74 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreVersionDecisionFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreEnvGrayFilter.java @@ -11,9 +11,11 @@ import com.netflix.zuul.exception.ZuulException; import org.springframework.beans.factory.annotation.Autowired; /** + * 灰度发布判断,改变版本号 + * * @author tanghc */ -public class PreVersionDecisionFilter extends BaseZuulFilter { +public class PreEnvGrayFilter extends BaseZuulFilter { @Autowired private EnvGrayManager envGrayManager; @@ -25,7 +27,7 @@ public class PreVersionDecisionFilter extends BaseZuulFilter { @Override protected int getFilterOrder() { - return PRE_VERSION_DECISION_FILTER_ORDER; + return PRE_ENV_GRAY_FILTER_ORDER; } @Override