合并结果

1.x
tanghc 6 years ago
parent 1aee96151d
commit e56787fc55
  1. 5
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/BaseRouteDefinition.java
  2. 2
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java
  3. 36
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java
  4. 5
      sop-service-common/src/main/java/com/gitee/sop/servercommon/annotation/ApiAbility.java
  5. 5
      sop-service-common/src/main/java/com/gitee/sop/servercommon/annotation/ApiMapping.java
  6. 2
      sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceApiInfo.java
  7. 26
      sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceConfig.java
  8. 1
      sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java
  9. 1
      sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java
  10. 7
      sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java
  11. 1
      sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingInfo.java
  12. 5
      sop-service-common/src/main/java/com/gitee/sop/servercommon/route/GatewayRouteDefinition.java

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

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

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

@ -25,4 +25,9 @@ public @interface ApiAbility {
* 忽略验证业务参数除外
*/
boolean ignoreValidate() default false;
/**
* 告诉网关是否对结果进行合并默认合并设置为false客户端将直接收到微服务端的结果
*/
boolean mergeResult() default true;
}

@ -32,6 +32,11 @@ public @interface ApiMapping {
*/
boolean ignoreValidate() default false;
/**
* 告诉网关是否对结果进行合并默认合并设置为false客户端将直接收到微服务端的结果
*/
boolean mergeResult() default true;
/**
* 接口名
* Alias for {@link RequestMapping#value}.

@ -24,6 +24,8 @@ public class ServiceApiInfo {
private String version;
/** 是否忽略验证 */
private boolean ignoreValidate;
/** 是否合并结果 */
private boolean mergeResult;
public ApiMeta() {
}

@ -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;
}
}

@ -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;

@ -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;
}

@ -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);
}

@ -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;

@ -49,4 +49,9 @@ public class GatewayRouteDefinition {
* 是否禁用
*/
private boolean disabled;
/**
* 是否合并结果
*/
private boolean mergeResult;
}
Loading…
Cancel
Save