diff --git a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/BaseRouteDefinition.java b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/BaseRouteDefinition.java index 7dc34c94..987e78d1 100644 --- a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/BaseRouteDefinition.java +++ b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/BaseRouteDefinition.java @@ -37,4 +37,9 @@ public class BaseRouteDefinition { * 是否禁用 */ private boolean disabled; + + /** + * 是否合并结果 + */ + private boolean mergeResult; } diff --git a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java index 6da345b6..cd94d346 100644 --- a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java +++ b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java @@ -59,7 +59,7 @@ public class NameVersionRoutePredicateFactory extends AbstractRoutePredicateFact } String nameVersion = config.param; - String name = params.getOrDefault(ParamNames.API_NAME, ""); + String name = params.getOrDefault(ParamNames.API_NAME, String.valueOf(System.currentTimeMillis())); String version = params.getOrDefault(ParamNames.VERSION_NAME, ""); boolean match = (name + version).equals(nameVersion); if (match) { diff --git a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java index 26ed6030..1cdf7057 100644 --- a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java +++ b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java @@ -4,12 +4,11 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.bean.SopConstants; -import com.gitee.sop.gatewaycommon.exception.ApiException; -import com.gitee.sop.gatewaycommon.message.Error; +import com.gitee.sop.gatewaycommon.bean.TargetRoute; +import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; import com.gitee.sop.gatewaycommon.message.ErrorEnum; import com.gitee.sop.gatewaycommon.message.ErrorMeta; import com.gitee.sop.gatewaycommon.param.ParamNames; -import com.netflix.zuul.exception.ZuulException; import org.springframework.http.HttpStatus; import org.springframework.util.StringUtils; @@ -49,7 +48,7 @@ public abstract class BaseExecutorAdapter implements ResultExecutor @Override public String mergeResult(T request, String serviceResult) { - boolean isMergeResult = ApiContext.getApiConfig().isMergeResult(); + boolean isMergeResult = this.isRouteMergeResult(request); if (!isMergeResult) { return serviceResult; } @@ -76,6 +75,33 @@ public abstract class BaseExecutorAdapter implements ResultExecutor return this.merge(request, jsonObjectService); } + /** + * 该路由是否合并结果 + * @param request + * @return + */ + protected boolean isRouteMergeResult(T request) { + boolean defaultSetting = ApiContext.getApiConfig().isMergeResult(); + boolean isMergeResult = true; + Map params = this.getApiParam(request); + Object name = params.get(ParamNames.API_NAME); + Object version = params.get(ParamNames.VERSION_NAME); + if(name == null) { + name = System.currentTimeMillis(); + } + TargetRoute targetRoute = RouteRepositoryContext.getRouteRepository().get(String.valueOf(name) + version); + if (targetRoute == null) { + return defaultSetting; + } else { + isMergeResult = targetRoute.getRouteDefinition().isMergeResult(); + } + // 如果路由说合并,还得看网关全局设置,网关全局设置优先级最大 + if (isMergeResult) { + isMergeResult = defaultSetting; + } + return isMergeResult; + } + protected String wrapResult(String serviceResult) { if (serviceResult == null) { serviceResult = ""; @@ -92,7 +118,6 @@ public abstract class BaseExecutorAdapter implements ResultExecutor public String merge(T exchange, JSONObject jsonObjectService) { JSONObject ret = new JSONObject(); - // 点换成下划线 Map params = this.getApiParam(exchange); Object name = params.get(ParamNames.API_NAME); if (name == null) { @@ -102,6 +127,7 @@ public abstract class BaseExecutorAdapter implements ResultExecutor if (sign == null) { sign = ""; } + // 点换成下划线 String method = String.valueOf(name).replace(DOT, UNDERLINE); ret.put(method + DATA_SUFFIX, jsonObjectService); ret.put(ParamNames.SIGN_NAME, sign); diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/annotation/ApiAbility.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/annotation/ApiAbility.java index 07c5ba8c..a1d1afc7 100644 --- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/annotation/ApiAbility.java +++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/annotation/ApiAbility.java @@ -25,4 +25,9 @@ public @interface ApiAbility { * 忽略验证,业务参数除外 */ boolean ignoreValidate() default false; + + /** + * 告诉网关是否对结果进行合并,默认合并。设置为false,客户端将直接收到微服务端的结果。 + */ + boolean mergeResult() default true; } diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/annotation/ApiMapping.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/annotation/ApiMapping.java index b728f9ce..96d4606c 100644 --- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/annotation/ApiMapping.java +++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/annotation/ApiMapping.java @@ -32,6 +32,11 @@ public @interface ApiMapping { */ boolean ignoreValidate() default false; + /** + * 告诉网关是否对结果进行合并,默认合并。设置为false,客户端将直接收到微服务端的结果。 + */ + boolean mergeResult() default true; + /** * 接口名 * Alias for {@link RequestMapping#value}. diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceApiInfo.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceApiInfo.java index 8f6f64cd..7c69ebbc 100644 --- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceApiInfo.java +++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceApiInfo.java @@ -24,6 +24,8 @@ public class ServiceApiInfo { private String version; /** 是否忽略验证 */ private boolean ignoreValidate; + /** 是否合并结果 */ + private boolean mergeResult; public ApiMeta() { } diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceConfig.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceConfig.java index 0d929ce9..83f7f6be 100644 --- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceConfig.java +++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceConfig.java @@ -22,10 +22,13 @@ public class ServiceConfig { private ServiceConfig() { } - /** - * 默认版本号 - */ - private String defaultVersion = ""; + public static ServiceConfig getInstance() { + return instance; + } + + public static void setInstance(ServiceConfig instance) { + ServiceConfig.instance = instance; + } /** * 错误模块 @@ -47,16 +50,19 @@ public class ServiceConfig { */ private GlobalExceptionHandler globalExceptionHandler = new DefaultGlobalExceptionHandler(); + /** + * 默认版本号 + */ + private String defaultVersion = ""; + /** * 设置为true,所有接口不进行校验 */ private boolean ignoreValidate; - public static ServiceConfig getInstance() { - return instance; - } + /** + * 是否对结果进行合并 + */ + private boolean mergeResult = true; - public static void setInstance(ServiceConfig instance) { - ServiceConfig.instance = instance; - } } diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java index 0a40c9b3..14bdb3ac 100644 --- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java +++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java @@ -93,6 +93,7 @@ public class DefaultRequestMappingEvent implements RequestMappingEvent { } ServiceApiInfo.ApiMeta apiMeta = new ServiceApiInfo.ApiMeta(name, path, version); apiMeta.setIgnoreValidate(apiMappingInfo.isIgnoreValidate()); + apiMeta.setMergeResult(apiMappingInfo.isMergeResult()); return apiMeta; } return null; diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java index 12fbe960..9899c75d 100644 --- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java +++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java @@ -111,6 +111,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager { gatewayRouteDefinition.setUri(uri); gatewayRouteDefinition.setPath(path); gatewayRouteDefinition.setIgnoreValidate(apiMeta.isIgnoreValidate()); + gatewayRouteDefinition.setMergeResult(apiMeta.isMergeResult()); return gatewayRouteDefinition; } diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java index 733fe290..4507b001 100644 --- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java +++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java @@ -44,16 +44,19 @@ public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping imple String name = null; String version; boolean ignoreValidate = false; + boolean mergeResult = true; ApiMapping apiMapping = method.getAnnotation(ApiMapping.class); if (apiMapping != null) { name = apiMapping.value()[0]; version = apiMapping.version(); ignoreValidate = apiMapping.ignoreValidate(); + mergeResult = apiMapping.mergeResult(); } else { ApiAbility apiAbility = this.findApiAbilityAnnotation(method); if (apiAbility != null) { version = apiAbility.version(); ignoreValidate = apiAbility.ignoreValidate(); + mergeResult = apiAbility.mergeResult(); } else { return super.getCustomMethodCondition(method); } @@ -66,7 +69,11 @@ public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping imple if (!ignoreValidate) { ignoreValidate = ServiceConfig.getInstance().isIgnoreValidate(); } + if (mergeResult) { + mergeResult = ServiceConfig.getInstance().isMergeResult(); + } apiMappingInfo.setIgnoreValidate(ignoreValidate); + apiMappingInfo.setMergeResult(mergeResult); logger.info("注册接口,method:" + method + ", version:" + version); return new ApiMappingRequestCondition(apiMappingInfo); } diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingInfo.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingInfo.java index 79da7a65..db1b7199 100644 --- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingInfo.java +++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingInfo.java @@ -10,6 +10,7 @@ public class ApiMappingInfo { private String name; private String version; private boolean ignoreValidate; + private boolean mergeResult; public ApiMappingInfo(String name, String version) { this.name = name; diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/GatewayRouteDefinition.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/GatewayRouteDefinition.java index 74995115..f028847d 100644 --- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/GatewayRouteDefinition.java +++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/GatewayRouteDefinition.java @@ -49,4 +49,9 @@ public class GatewayRouteDefinition { * 是否禁用 */ private boolean disabled; + + /** + * 是否合并结果 + */ + private boolean mergeResult; } \ No newline at end of file