From 69131929c771c82fb585b6ddde6dae8c54fe02a1 Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 14 Jan 2020 11:22:22 +0800 Subject: [PATCH] 3.0.0 --- .../gateway/ServerWebExchangeUtil.java | 15 ++++++++++++--- .../gateway/controller/GatewayController.java | 5 ++--- .../gatewaycommon/gateway/filter/IndexFilter.java | 9 +++++---- .../gateway/route/GatewayForwardChooser.java | 13 +++++++++++++ .../gitee/sop/gatewaycommon/param/ApiParam.java | 10 +--------- .../gatewaycommon/route/BaseForwardChooser.java | 11 +++-------- .../sop/gatewaycommon/route/ForwardInfo.java | 6 ++++++ 7 files changed, 42 insertions(+), 27 deletions(-) diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java index e76ec24a..6a7a1bba 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java @@ -43,8 +43,8 @@ import java.util.function.Consumer; @Slf4j public class ServerWebExchangeUtil { + private static final String THROWABLE_KEY = "sop.throwable"; private static final String UNKNOWN_PATH = "/sop/unknown"; - private static final String VALIDATE_ERROR_PATH = "/sop/validateError"; private static final String REST_PATH = "/rest"; private static FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter(); @@ -63,8 +63,9 @@ public class ServerWebExchangeUtil { * @return 返回新的ServerWebExchange,配合chain.filter(newExchange);使用 */ public static ServerWebExchange getForwardExchange(ServerWebExchange exchange, ForwardInfo forwardInfo) { - ServerHttpRequest newRequest = exchange.getRequest() - .mutate() + ServerHttpRequest.Builder builder = exchange.getRequest() + .mutate(); + ServerHttpRequest newRequest = builder .header(ParamNames.HEADER_VERSION_NAME, forwardInfo.getVersion()) .path(forwardInfo.getPath()).build(); return exchange.mutate().request(newRequest).build(); @@ -153,6 +154,14 @@ public class ServerWebExchangeUtil { return apiParam; } + public static void setThrowable(ServerWebExchange exchange, Throwable throwable) { + exchange.getAttributes().put(THROWABLE_KEY, throwable); + } + + public static Throwable getThrowable(ServerWebExchange exchange) { + return (Throwable)exchange.getAttribute(THROWABLE_KEY); + } + /** * 获取请求参数 * diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/controller/GatewayController.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/controller/GatewayController.java index c67c4a19..8836ad13 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/controller/GatewayController.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/controller/GatewayController.java @@ -4,7 +4,6 @@ import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.exception.ApiException; import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil; import com.gitee.sop.gatewaycommon.message.ErrorEnum; -import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.result.ResultExecutor; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -25,10 +24,10 @@ public class GatewayController { */ @RequestMapping("/sop/validateError") public Mono validateError(ServerWebExchange exchange) { - ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange); + Throwable throwable = ServerWebExchangeUtil.getThrowable(exchange); // 合并微服务传递过来的结果,变成最终结果 ResultExecutor resultExecutor = ApiContext.getApiConfig().getGatewayResultExecutor(); - String gatewayResult = resultExecutor.buildErrorResult(exchange, apiParam.getThrowable()); + String gatewayResult = resultExecutor.buildErrorResult(exchange, throwable); return Mono.just(gatewayResult); } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java index 5f2d8797..5441eb1d 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java @@ -79,7 +79,7 @@ public class IndexFilter implements WebFilter { // 构建ApiParam ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange, body); // 签名验证 - doValidate(apiParam); + doValidate(exchange, apiParam); return Mono.just(body); }); BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, (Class) String.class); @@ -108,7 +108,8 @@ public class IndexFilter implements WebFilter { // 构建ApiParam ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange, originalQuery); // 签名验证 - doValidate(apiParam); + doValidate(exchange, apiParam); + ForwardInfo forwardInfo = gatewayForwardChooser.getForwardInfo(exchange); ServerWebExchange forwardExchange = ServerWebExchangeUtil.getForwardExchange(exchange, forwardInfo); return chain.filter(forwardExchange); @@ -118,12 +119,12 @@ public class IndexFilter implements WebFilter { } } - private void doValidate(ApiParam apiParam) { + private void doValidate(ServerWebExchange exchange, ApiParam apiParam) { try { validator.validate(apiParam); } catch (ApiException e) { log.error("验证失败,ip:{}, params:{}, errorMsg:{}", apiParam.fetchIp(), apiParam.toJSONString(), e.getMessage()); - apiParam.setThrowable(e); + ServerWebExchangeUtil.setThrowable(exchange, e); } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayForwardChooser.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayForwardChooser.java index 5f2f73c3..219b1ec4 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayForwardChooser.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayForwardChooser.java @@ -3,14 +3,27 @@ package com.gitee.sop.gatewaycommon.gateway.route; import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil; import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.route.BaseForwardChooser; +import com.gitee.sop.gatewaycommon.route.ForwardInfo; import org.springframework.web.server.ServerWebExchange; /** * @author tanghc */ public class GatewayForwardChooser extends BaseForwardChooser { + + private static final String VALIDATE_ERROR_PATH = "/sop/validateError"; + @Override protected ApiParam getApiParam(ServerWebExchange exchange) { return ServerWebExchangeUtil.getApiParam(exchange); } + + @Override + public ForwardInfo getForwardInfo(ServerWebExchange exchange) { + // 如果有异常,直接跳转到异常处理 + if (ServerWebExchangeUtil.getThrowable(exchange) != null) { + return new ForwardInfo(VALIDATE_ERROR_PATH, ""); + } + return super.getForwardInfo(exchange); + } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/ApiParam.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/ApiParam.java index e0a38f81..4d48d78f 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/ApiParam.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/ApiParam.java @@ -14,7 +14,7 @@ import java.util.Map; */ public class ApiParam extends JSONObject implements Param { - private static final String THROWABLE_KEY = "Throwable"; + public ApiParam() { } @@ -33,14 +33,6 @@ public class ApiParam extends JSONObject implements Param { private transient UploadContext uploadContext; - public void setThrowable(Throwable throwable) { - this.put(THROWABLE_KEY, throwable); - } - - public Throwable getThrowable() { - return (Throwable)get(THROWABLE_KEY); - } - public void fitNameVersion() { if (restName != null) { this.put(ParamNames.API_NAME, restName); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseForwardChooser.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseForwardChooser.java index bfebdd77..254560c9 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseForwardChooser.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseForwardChooser.java @@ -21,12 +21,11 @@ public abstract class BaseForwardChooser implements ForwardChooser { @Override public ForwardInfo getForwardInfo(T t) { ApiParam apiParam = getApiParam(t); - ForwardInfo forwardInfo = new ForwardInfo(); String nameVersion = apiParam.fetchNameVersion(); TargetRoute targetRoute = RouteRepositoryContext.getRouteRepository().get(nameVersion); RouteDefinition routeDefinitionOrig = targetRoute.getRouteDefinition(); String path = routeDefinitionOrig.getPath(); - String versionInHead = apiParam.fetchVersion(); + String version = apiParam.fetchVersion(); String serviceId = targetRoute.getServiceRouteInfo().fetchServiceIdLowerCase(); // 如果服务在灰度阶段,返回一个灰度版本号 String grayVersion = envGrayManager.getVersion(serviceId, nameVersion); @@ -36,7 +35,7 @@ public abstract class BaseForwardChooser implements ForwardChooser { TargetRoute targetRouteDest = RouteRepositoryContext.getRouteRepository().get(newNameVersion); if (targetRouteDest != null) { if (BooleanUtils.toBoolean(routeDefinitionOrig.getCompatibleMode())) { - versionInHead = grayVersion; + version = grayVersion; } else { // 获取灰度接口 RouteDefinition routeDefinition = targetRouteDest.getRouteDefinition(); @@ -44,11 +43,7 @@ public abstract class BaseForwardChooser implements ForwardChooser { } } } - - forwardInfo.setPath(path); - forwardInfo.setVersion(versionInHead); - - return forwardInfo; + return new ForwardInfo(path, version); } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ForwardInfo.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ForwardInfo.java index f36932c1..00c9333d 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ForwardInfo.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ForwardInfo.java @@ -10,4 +10,10 @@ public class ForwardInfo { private String path; private String version; private String domain; + + public ForwardInfo(String path, String version) { + this.path = path; + this.version = version; + } + }