合并结果

This commit is contained in:
tanghc
2019-03-20 21:18:03 +08:00
parent 1aee96151d
commit e56787fc55
12 changed files with 80 additions and 16 deletions

View File

@@ -37,4 +37,9 @@ public class BaseRouteDefinition {
* 是否禁用
*/
private boolean disabled;
/**
* 是否合并结果
*/
private boolean mergeResult;
}

View File

@@ -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) {

View File

@@ -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<T, R> implements ResultExecutor<T, R>
@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<T, R> implements ResultExecutor<T, R>
return this.merge(request, jsonObjectService);
}
/**
* 该路由是否合并结果
* @param request
* @return
*/
protected boolean isRouteMergeResult(T request) {
boolean defaultSetting = ApiContext.getApiConfig().isMergeResult();
boolean isMergeResult = true;
Map<String, ?> 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<T, R> implements ResultExecutor<T, R>
public String merge(T exchange, JSONObject jsonObjectService) {
JSONObject ret = new JSONObject();
// 点换成下划线
Map<String, ?> params = this.getApiParam(exchange);
Object name = params.get(ParamNames.API_NAME);
if (name == null) {
@@ -102,6 +127,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
if (sign == null) {
sign = "";
}
// 点换成下划线
String method = String.valueOf(name).replace(DOT, UNDERLINE);
ret.put(method + DATA_SUFFIX, jsonObjectService);
ret.put(ParamNames.SIGN_NAME, sign);