mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
合并结果
This commit is contained in:
@@ -37,4 +37,9 @@ public class BaseRouteDefinition {
|
|||||||
* 是否禁用
|
* 是否禁用
|
||||||
*/
|
*/
|
||||||
private boolean disabled;
|
private boolean disabled;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否合并结果
|
||||||
|
*/
|
||||||
|
private boolean mergeResult;
|
||||||
}
|
}
|
||||||
|
@@ -59,7 +59,7 @@ public class NameVersionRoutePredicateFactory extends AbstractRoutePredicateFact
|
|||||||
}
|
}
|
||||||
|
|
||||||
String nameVersion = config.param;
|
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, "");
|
String version = params.getOrDefault(ParamNames.VERSION_NAME, "");
|
||||||
boolean match = (name + version).equals(nameVersion);
|
boolean match = (name + version).equals(nameVersion);
|
||||||
if (match) {
|
if (match) {
|
||||||
|
@@ -4,12 +4,11 @@ import com.alibaba.fastjson.JSON;
|
|||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.gitee.sop.gatewaycommon.bean.ApiContext;
|
import com.gitee.sop.gatewaycommon.bean.ApiContext;
|
||||||
import com.gitee.sop.gatewaycommon.bean.SopConstants;
|
import com.gitee.sop.gatewaycommon.bean.SopConstants;
|
||||||
import com.gitee.sop.gatewaycommon.exception.ApiException;
|
import com.gitee.sop.gatewaycommon.bean.TargetRoute;
|
||||||
import com.gitee.sop.gatewaycommon.message.Error;
|
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
|
||||||
import com.gitee.sop.gatewaycommon.message.ErrorEnum;
|
import com.gitee.sop.gatewaycommon.message.ErrorEnum;
|
||||||
import com.gitee.sop.gatewaycommon.message.ErrorMeta;
|
import com.gitee.sop.gatewaycommon.message.ErrorMeta;
|
||||||
import com.gitee.sop.gatewaycommon.param.ParamNames;
|
import com.gitee.sop.gatewaycommon.param.ParamNames;
|
||||||
import com.netflix.zuul.exception.ZuulException;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@@ -49,7 +48,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String mergeResult(T request, String serviceResult) {
|
public String mergeResult(T request, String serviceResult) {
|
||||||
boolean isMergeResult = ApiContext.getApiConfig().isMergeResult();
|
boolean isMergeResult = this.isRouteMergeResult(request);
|
||||||
if (!isMergeResult) {
|
if (!isMergeResult) {
|
||||||
return serviceResult;
|
return serviceResult;
|
||||||
}
|
}
|
||||||
@@ -76,6 +75,33 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
|||||||
return this.merge(request, jsonObjectService);
|
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) {
|
protected String wrapResult(String serviceResult) {
|
||||||
if (serviceResult == null) {
|
if (serviceResult == null) {
|
||||||
serviceResult = "";
|
serviceResult = "";
|
||||||
@@ -92,7 +118,6 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
|||||||
|
|
||||||
public String merge(T exchange, JSONObject jsonObjectService) {
|
public String merge(T exchange, JSONObject jsonObjectService) {
|
||||||
JSONObject ret = new JSONObject();
|
JSONObject ret = new JSONObject();
|
||||||
// 点换成下划线
|
|
||||||
Map<String, ?> params = this.getApiParam(exchange);
|
Map<String, ?> params = this.getApiParam(exchange);
|
||||||
Object name = params.get(ParamNames.API_NAME);
|
Object name = params.get(ParamNames.API_NAME);
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
@@ -102,6 +127,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
|||||||
if (sign == null) {
|
if (sign == null) {
|
||||||
sign = "";
|
sign = "";
|
||||||
}
|
}
|
||||||
|
// 点换成下划线
|
||||||
String method = String.valueOf(name).replace(DOT, UNDERLINE);
|
String method = String.valueOf(name).replace(DOT, UNDERLINE);
|
||||||
ret.put(method + DATA_SUFFIX, jsonObjectService);
|
ret.put(method + DATA_SUFFIX, jsonObjectService);
|
||||||
ret.put(ParamNames.SIGN_NAME, sign);
|
ret.put(ParamNames.SIGN_NAME, sign);
|
||||||
|
@@ -25,4 +25,9 @@ public @interface ApiAbility {
|
|||||||
* 忽略验证,业务参数除外
|
* 忽略验证,业务参数除外
|
||||||
*/
|
*/
|
||||||
boolean ignoreValidate() default false;
|
boolean ignoreValidate() default false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 告诉网关是否对结果进行合并,默认合并。设置为false,客户端将直接收到微服务端的结果。
|
||||||
|
*/
|
||||||
|
boolean mergeResult() default true;
|
||||||
}
|
}
|
||||||
|
@@ -32,6 +32,11 @@ public @interface ApiMapping {
|
|||||||
*/
|
*/
|
||||||
boolean ignoreValidate() default false;
|
boolean ignoreValidate() default false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 告诉网关是否对结果进行合并,默认合并。设置为false,客户端将直接收到微服务端的结果。
|
||||||
|
*/
|
||||||
|
boolean mergeResult() default true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 接口名
|
* 接口名
|
||||||
* Alias for {@link RequestMapping#value}.
|
* Alias for {@link RequestMapping#value}.
|
||||||
|
@@ -24,6 +24,8 @@ public class ServiceApiInfo {
|
|||||||
private String version;
|
private String version;
|
||||||
/** 是否忽略验证 */
|
/** 是否忽略验证 */
|
||||||
private boolean ignoreValidate;
|
private boolean ignoreValidate;
|
||||||
|
/** 是否合并结果 */
|
||||||
|
private boolean mergeResult;
|
||||||
|
|
||||||
public ApiMeta() {
|
public ApiMeta() {
|
||||||
}
|
}
|
||||||
|
@@ -22,10 +22,13 @@ public class ServiceConfig {
|
|||||||
private ServiceConfig() {
|
private ServiceConfig() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static ServiceConfig getInstance() {
|
||||||
* 默认版本号
|
return instance;
|
||||||
*/
|
}
|
||||||
private String defaultVersion = "";
|
|
||||||
|
public static void setInstance(ServiceConfig instance) {
|
||||||
|
ServiceConfig.instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 错误模块
|
* 错误模块
|
||||||
@@ -47,16 +50,19 @@ public class ServiceConfig {
|
|||||||
*/
|
*/
|
||||||
private GlobalExceptionHandler globalExceptionHandler = new DefaultGlobalExceptionHandler();
|
private GlobalExceptionHandler globalExceptionHandler = new DefaultGlobalExceptionHandler();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认版本号
|
||||||
|
*/
|
||||||
|
private String defaultVersion = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置为true,所有接口不进行校验
|
* 设置为true,所有接口不进行校验
|
||||||
*/
|
*/
|
||||||
private boolean ignoreValidate;
|
private boolean ignoreValidate;
|
||||||
|
|
||||||
public static ServiceConfig getInstance() {
|
/**
|
||||||
return instance;
|
* 是否对结果进行合并
|
||||||
}
|
*/
|
||||||
|
private boolean mergeResult = true;
|
||||||
|
|
||||||
public static void setInstance(ServiceConfig instance) {
|
|
||||||
ServiceConfig.instance = instance;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -93,6 +93,7 @@ public class DefaultRequestMappingEvent implements RequestMappingEvent {
|
|||||||
}
|
}
|
||||||
ServiceApiInfo.ApiMeta apiMeta = new ServiceApiInfo.ApiMeta(name, path, version);
|
ServiceApiInfo.ApiMeta apiMeta = new ServiceApiInfo.ApiMeta(name, path, version);
|
||||||
apiMeta.setIgnoreValidate(apiMappingInfo.isIgnoreValidate());
|
apiMeta.setIgnoreValidate(apiMappingInfo.isIgnoreValidate());
|
||||||
|
apiMeta.setMergeResult(apiMappingInfo.isMergeResult());
|
||||||
return apiMeta;
|
return apiMeta;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@@ -111,6 +111,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
|
|||||||
gatewayRouteDefinition.setUri(uri);
|
gatewayRouteDefinition.setUri(uri);
|
||||||
gatewayRouteDefinition.setPath(path);
|
gatewayRouteDefinition.setPath(path);
|
||||||
gatewayRouteDefinition.setIgnoreValidate(apiMeta.isIgnoreValidate());
|
gatewayRouteDefinition.setIgnoreValidate(apiMeta.isIgnoreValidate());
|
||||||
|
gatewayRouteDefinition.setMergeResult(apiMeta.isMergeResult());
|
||||||
return gatewayRouteDefinition;
|
return gatewayRouteDefinition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -44,16 +44,19 @@ public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping imple
|
|||||||
String name = null;
|
String name = null;
|
||||||
String version;
|
String version;
|
||||||
boolean ignoreValidate = false;
|
boolean ignoreValidate = false;
|
||||||
|
boolean mergeResult = true;
|
||||||
ApiMapping apiMapping = method.getAnnotation(ApiMapping.class);
|
ApiMapping apiMapping = method.getAnnotation(ApiMapping.class);
|
||||||
if (apiMapping != null) {
|
if (apiMapping != null) {
|
||||||
name = apiMapping.value()[0];
|
name = apiMapping.value()[0];
|
||||||
version = apiMapping.version();
|
version = apiMapping.version();
|
||||||
ignoreValidate = apiMapping.ignoreValidate();
|
ignoreValidate = apiMapping.ignoreValidate();
|
||||||
|
mergeResult = apiMapping.mergeResult();
|
||||||
} else {
|
} else {
|
||||||
ApiAbility apiAbility = this.findApiAbilityAnnotation(method);
|
ApiAbility apiAbility = this.findApiAbilityAnnotation(method);
|
||||||
if (apiAbility != null) {
|
if (apiAbility != null) {
|
||||||
version = apiAbility.version();
|
version = apiAbility.version();
|
||||||
ignoreValidate = apiAbility.ignoreValidate();
|
ignoreValidate = apiAbility.ignoreValidate();
|
||||||
|
mergeResult = apiAbility.mergeResult();
|
||||||
} else {
|
} else {
|
||||||
return super.getCustomMethodCondition(method);
|
return super.getCustomMethodCondition(method);
|
||||||
}
|
}
|
||||||
@@ -66,7 +69,11 @@ public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping imple
|
|||||||
if (!ignoreValidate) {
|
if (!ignoreValidate) {
|
||||||
ignoreValidate = ServiceConfig.getInstance().isIgnoreValidate();
|
ignoreValidate = ServiceConfig.getInstance().isIgnoreValidate();
|
||||||
}
|
}
|
||||||
|
if (mergeResult) {
|
||||||
|
mergeResult = ServiceConfig.getInstance().isMergeResult();
|
||||||
|
}
|
||||||
apiMappingInfo.setIgnoreValidate(ignoreValidate);
|
apiMappingInfo.setIgnoreValidate(ignoreValidate);
|
||||||
|
apiMappingInfo.setMergeResult(mergeResult);
|
||||||
logger.info("注册接口,method:" + method + ", version:" + version);
|
logger.info("注册接口,method:" + method + ", version:" + version);
|
||||||
return new ApiMappingRequestCondition(apiMappingInfo);
|
return new ApiMappingRequestCondition(apiMappingInfo);
|
||||||
}
|
}
|
||||||
|
@@ -10,6 +10,7 @@ public class ApiMappingInfo {
|
|||||||
private String name;
|
private String name;
|
||||||
private String version;
|
private String version;
|
||||||
private boolean ignoreValidate;
|
private boolean ignoreValidate;
|
||||||
|
private boolean mergeResult;
|
||||||
|
|
||||||
public ApiMappingInfo(String name, String version) {
|
public ApiMappingInfo(String name, String version) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@@ -49,4 +49,9 @@ public class GatewayRouteDefinition {
|
|||||||
* 是否禁用
|
* 是否禁用
|
||||||
*/
|
*/
|
||||||
private boolean disabled;
|
private boolean disabled;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否合并结果
|
||||||
|
*/
|
||||||
|
private boolean mergeResult;
|
||||||
}
|
}
|
Reference in New Issue
Block a user