From c159d6f85c05102726be791034b7de483e8ed26b Mon Sep 17 00:00:00 2001 From: tanghc Date: Fri, 14 Jun 2019 17:29:19 +0800 Subject: [PATCH] =?UTF-8?q?BUG=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sop-common/pom.xml | 2 +- sop-common/sop-gateway-common/pom.xml | 4 +-- .../manager/DefaultLimitConfigManager.java | 3 ++ .../manager/RouteRepository.java | 2 +- .../sop/gatewaycommon/param/ApiParam.java | 12 ++++++++ .../gatewaycommon/param/BaseParamBuilder.java | 12 ++++++-- .../gatewaycommon/validate/ApiValidator.java | 28 +++++++++---------- .../zuul/route/ZuulRouteRepository.java | 8 +----- .../zuul/route/ZuulZookeeperRouteManager.java | 7 +++-- sop-common/sop-service-common/pom.xml | 4 +-- 10 files changed, 48 insertions(+), 34 deletions(-) diff --git a/sop-common/pom.xml b/sop-common/pom.xml index 31c829ee..059d299b 100644 --- a/sop-common/pom.xml +++ b/sop-common/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gitee.sop sop-common - 1.10.0-SNAPSHOT + 1.10.1-SNAPSHOT pom diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml index 3868b80f..78ead17c 100644 --- a/sop-common/sop-gateway-common/pom.xml +++ b/sop-common/sop-gateway-common/pom.xml @@ -5,11 +5,11 @@ com.gitee.sop sop-common - 1.10.0-SNAPSHOT + 1.10.1-SNAPSHOT ../pom.xml sop-gateway-common - 1.10.0-SNAPSHOT + 1.10.1-SNAPSHOT jar sop-gateway-common diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java index 68e31d1a..54efd924 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java @@ -74,6 +74,9 @@ public class DefaultLimitConfigManager implements LimitConfigManager { @Override public ConfigLimitDto get(String limitKey) { + if (StringUtils.isBlank(limitKey)) { + return null; + } return limitCache.get(limitKey); } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepository.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepository.java index d2a26770..8976090f 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepository.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepository.java @@ -11,7 +11,7 @@ public interface RouteRepository { /** * 获取路由信息 * @param id 路由id - * @return 返回路由信息 + * @return 返回路由信息,找不到返回null */ T get(String id); 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 50a5ea90..3523faed 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 @@ -29,6 +29,15 @@ public class ApiParam extends JSONObject implements Param { private transient ApiUploadContext apiUploadContext; + public void fitNameVersion() { + if (restName != null) { + this.put(ParamNames.API_NAME, restName); + } + if (restVersion != null) { + this.put(ParamNames.VERSION_NAME, restVersion); + } + } + public static ApiParam build(Map map) { ApiParam apiParam = new ApiParam(); for (Map.Entry entry : map.entrySet()) { @@ -95,6 +104,9 @@ public class ApiParam extends JSONObject implements Param { } public static String buildNameVersion(String name, String version) { + if (name == null && version == null) { + return null; + } if (StringUtils.isEmpty(version)) { return name; } else { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java index f4bfc39c..2d38fad2 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java @@ -7,8 +7,10 @@ import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; import com.gitee.sop.gatewaycommon.message.ErrorEnum; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.StringUtils; import java.util.Map; +import java.util.Optional; /** * @author tanghc @@ -34,7 +36,8 @@ public abstract class BaseParamBuilder implements ParamBuilder { } protected void initOtherProperty(ApiParam apiParam) { - if (apiParam.size() == 0) { + String nameVersion = apiParam.fetchNameVersion(); + if (StringUtils.isBlank(nameVersion)) { throw ErrorEnum.ISV_INVALID_METHOD.getErrorMeta().getException(); } RouteRepository routeRepository = RouteRepositoryContext.getRouteRepository(); @@ -42,8 +45,10 @@ public abstract class BaseParamBuilder implements ParamBuilder { log.error("RouteRepositoryContext.setRouteRepository()方法未使用"); throw ErrorEnum.AOP_UNKNOW_ERROR.getErrorMeta().getException(); } - TargetRoute targetRoute = routeRepository.get(apiParam.fetchNameVersion()); - BaseRouteDefinition routeDefinition = targetRoute.getRouteDefinition(); + TargetRoute targetRoute = routeRepository.get(nameVersion); + BaseRouteDefinition routeDefinition = Optional.ofNullable(targetRoute) + .map(t -> t.getRouteDefinition()) + .orElse(null); if (routeDefinition == null) { throw ErrorEnum.ISV_INVALID_METHOD.getErrorMeta().getException(); } @@ -51,3 +56,4 @@ public abstract class BaseParamBuilder implements ParamBuilder { } } + diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java index 627becc0..12b5eede 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java @@ -39,25 +39,23 @@ public class ApiValidator implements Validator { @Override public void validate(ApiParam param) { - ApiConfig apiConfig = ApiContext.getApiConfig(); - if (apiConfig.isIgnoreValidate() || param.fetchIgnoreValidate()) { - if (logger.isDebugEnabled()) { - logger.debug("忽略所有验证(ignoreValidate=true), name:{}, version:{}", param.fetchName(), param.fetchVersion()); - } - return; - } - if (param.fetchIgnoreSign()) { - if (logger.isDebugEnabled()) { - logger.debug("忽略签名验证, name:{}, version:{}", param.fetchName(), param.fetchVersion()); + try { + ApiConfig apiConfig = ApiContext.getApiConfig(); + if (apiConfig.isIgnoreValidate() || param.fetchIgnoreValidate()) { + if (logger.isDebugEnabled()) { + logger.debug("忽略所有验证(ignoreValidate=true), name:{}, version:{}", param.fetchName(), param.fetchVersion()); + } + return; } - } else { - // 需要验证签名,先校验appKey,后校验签名顺序不能变 + // 需要验证签名,先校验appKey,后校验签名,顺序不能变 checkAppKey(param); checkSign(param); + checkTimeout(param); + checkFormat(param); + checkUploadFile(param); + } finally { + param.fitNameVersion(); } - checkUploadFile(param); - checkTimeout(param); - checkFormat(param); } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java index 4f62544f..fe051d20 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java @@ -1,9 +1,7 @@ package com.gitee.sop.gatewaycommon.zuul.route; import com.gitee.sop.gatewaycommon.manager.RouteRepository; -import com.gitee.sop.gatewaycommon.message.ErrorEnum; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; @@ -23,11 +21,7 @@ public class ZuulRouteRepository implements RouteRepository { @Override public ZuulTargetRoute get(String id) { - ZuulTargetRoute route = nameVersionTargetRouteMap.get(id); - if (route == null) { - throw ErrorEnum.ISV_INVALID_METHOD.getErrorMeta().getException(); - } - return route; + return nameVersionTargetRouteMap.get(id); } @Override diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulZookeeperRouteManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulZookeeperRouteManager.java index 3485ad3c..7ccab8f0 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulZookeeperRouteManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulZookeeperRouteManager.java @@ -15,6 +15,9 @@ import org.springframework.core.env.Environment; @Slf4j public class ZuulZookeeperRouteManager extends BaseRouteManager { + /** 路由重试 */ + public static final boolean RETRYABLE = true; + public ZuulZookeeperRouteManager(Environment environment, RouteRepository routeRepository) { super(environment, routeRepository); } @@ -31,9 +34,7 @@ public class ZuulZookeeperRouteManager extends BaseRouteManager com.gitee.sop sop-common - 1.10.0-SNAPSHOT + 1.10.1-SNAPSHOT ../pom.xml sop-service-common - 1.10.0-SNAPSHOT + 1.10.1-SNAPSHOT jar sop-service-common