RouteContext新增isv对象

This commit is contained in:
六如
2025-03-06 09:46:26 +08:00
parent 7ffb3b8d9c
commit 8c5b0e2349
8 changed files with 52 additions and 30 deletions

View File

@@ -1,6 +1,7 @@
package com.gitee.sop.gateway.common; package com.gitee.sop.gateway.common;
import com.gitee.sop.gateway.request.ApiRequestContext; import com.gitee.sop.gateway.request.ApiRequestContext;
import com.gitee.sop.gateway.service.manager.dto.IsvDTO;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@@ -13,5 +14,6 @@ public class RouteContext {
private ApiRequestContext apiRequestContext; private ApiRequestContext apiRequestContext;
private ApiInfoDTO apiInfo; private ApiInfoDTO apiInfo;
private IsvDTO isv;
} }

View File

@@ -1,10 +1,10 @@
package com.gitee.sop.gateway.service; package com.gitee.sop.gateway.service;
import com.gitee.sop.gateway.common.ApiInfoDTO; import com.gitee.sop.gateway.common.RouteContext;
import com.gitee.sop.gateway.request.ApiRequestContext;
import com.gitee.sop.gateway.response.ApiResponse;
import com.gitee.sop.gateway.response.Response; import com.gitee.sop.gateway.response.Response;
import java.util.Optional;
/** /**
* 结果包裹 * 结果包裹
* *
@@ -12,10 +12,6 @@ import com.gitee.sop.gateway.response.Response;
*/ */
public interface ResultWrapper { public interface ResultWrapper {
Response wrap(ApiRequestContext context, ApiInfoDTO apiInfoDTO, Object result); Response wrap(Optional<RouteContext> routeContextOpt, Object result);
default Response wrap(ApiRequestContext context, ApiResponse apiResponse) {
return wrap(context, null, apiResponse);
}
} }

View File

@@ -1,8 +1,8 @@
package com.gitee.sop.gateway.service.impl; package com.gitee.sop.gateway.service.impl;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.gitee.sop.gateway.common.ApiInfoDTO;
import com.gitee.sop.gateway.request.ApiRequestContext; import com.gitee.sop.gateway.request.ApiRequestContext;
import com.gitee.sop.gateway.service.validate.ValidateReturn;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
@@ -12,7 +12,7 @@ import org.springframework.stereotype.Service;
public class RestRouteServiceImpl extends RouteServiceImpl { public class RestRouteServiceImpl extends RouteServiceImpl {
@Override @Override
protected ApiInfoDTO validate(ApiRequestContext apiRequestContext) { protected ValidateReturn validate(ApiRequestContext apiRequestContext) {
return validator.validateRest(apiRequestContext); return validator.validateRest(apiRequestContext);
} }

View File

@@ -1,9 +1,9 @@
package com.gitee.sop.gateway.service.impl; package com.gitee.sop.gateway.service.impl;
import com.gitee.sop.gateway.common.ApiInfoDTO; import com.gitee.sop.gateway.common.ApiInfoDTO;
import com.gitee.sop.gateway.common.RouteContext;
import com.gitee.sop.gateway.common.enums.YesOrNoEnum; import com.gitee.sop.gateway.common.enums.YesOrNoEnum;
import com.gitee.sop.gateway.config.ApiConfig; import com.gitee.sop.gateway.config.ApiConfig;
import com.gitee.sop.gateway.request.ApiRequestContext;
import com.gitee.sop.gateway.response.ApiResponse; import com.gitee.sop.gateway.response.ApiResponse;
import com.gitee.sop.gateway.response.NoCommonResponse; import com.gitee.sop.gateway.response.NoCommonResponse;
import com.gitee.sop.gateway.response.Response; import com.gitee.sop.gateway.response.Response;
@@ -11,6 +11,8 @@ import com.gitee.sop.gateway.service.ResultWrapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Optional;
/** /**
* @author 六如 * @author 六如
*/ */
@@ -21,11 +23,11 @@ public class ResultWrapperImpl implements ResultWrapper {
private ApiConfig apiConfig; private ApiConfig apiConfig;
@Override @Override
public Response wrap(ApiRequestContext context, ApiInfoDTO apiInfoDTO, Object result) { public Response wrap(Optional<RouteContext> routeContextOpt, Object result) {
Integer hasCommonResponse = apiInfoDTO == null ? boolean needNotWrap = routeContextOpt.map(RouteContext::getApiInfo)
YesOrNoEnum.YES.getValue() .map(ApiInfoDTO::getHasCommonResponse)
: apiInfoDTO.getHasCommonResponse(); .map(YesOrNoEnum::of)
boolean needNotWrap = YesOrNoEnum.of(hasCommonResponse) == YesOrNoEnum.NO; .orElse(YesOrNoEnum.YES) == YesOrNoEnum.NO;
if (result instanceof ApiResponse) { if (result instanceof ApiResponse) {
ApiResponse apiResponse = (ApiResponse) result; ApiResponse apiResponse = (ApiResponse) result;
return executeApiResponse(apiResponse, needNotWrap); return executeApiResponse(apiResponse, needNotWrap);

View File

@@ -18,6 +18,7 @@ import com.gitee.sop.gateway.service.GenericServiceInvoker;
import com.gitee.sop.gateway.service.ResultWrapper; import com.gitee.sop.gateway.service.ResultWrapper;
import com.gitee.sop.gateway.service.RouteService; import com.gitee.sop.gateway.service.RouteService;
import com.gitee.sop.gateway.service.Serde; import com.gitee.sop.gateway.service.Serde;
import com.gitee.sop.gateway.service.validate.ValidateReturn;
import com.gitee.sop.gateway.service.validate.Validator; import com.gitee.sop.gateway.service.validate.Validator;
import com.gitee.sop.gateway.util.ClassUtil; import com.gitee.sop.gateway.util.ClassUtil;
import com.gitee.sop.support.constant.SopConstants; import com.gitee.sop.support.constant.SopConstants;
@@ -87,28 +88,34 @@ public class RouteServiceImpl implements RouteService {
log.info("收到客户端请求, ip={}, apiRequest={}", apiRequestContext.getIp(), apiRequest); log.info("收到客户端请求, ip={}, apiRequest={}", apiRequestContext.getIp(), apiRequest);
try { try {
// 接口校验 // 接口校验
ApiInfoDTO apiInfoDTO = validate(apiRequestContext); ValidateReturn validateReturn = validate(apiRequestContext);
RouteContext routeContext = new RouteContext(apiRequestContext, apiInfoDTO); RouteContext routeContext = new RouteContext(
apiRequestContext,
validateReturn.getApiInfoDTO(),
validateReturn.getIsvDTO()
);
// 执行拦截器前置动作 // 执行拦截器前置动作
this.doPreRoute(routeContext); this.doPreRoute(routeContext);
// 执行路由,返回微服务结果 // 执行路由,返回微服务结果
Object result = doRoute(apiRequestContext, apiInfoDTO); Object result = doRoute(routeContext);
// 执行拦截器后置动作 // 执行拦截器后置动作
result = this.doAfterRoute(routeContext, result); result = this.doAfterRoute(routeContext, result);
// 结果处理 // 结果处理
return resultWrapper.wrap(apiRequestContext, apiInfoDTO, result); return resultWrapper.wrap(Optional.of(routeContext), result);
} catch (Exception e) { } catch (Exception e) {
log.error("接口请求报错, , ip={}, apiRequest={}", apiRequestContext.getIp(), apiRequest, e); log.error("接口请求报错, , ip={}, apiRequest={}", apiRequestContext.getIp(), apiRequest, e);
ApiResponse apiResponse = exceptionExecutor.executeException(apiRequestContext, e); ApiResponse apiResponse = exceptionExecutor.executeException(apiRequestContext, e);
return resultWrapper.wrap(apiRequestContext, apiResponse); return resultWrapper.wrap(Optional.empty(), apiResponse);
} }
} }
protected ApiInfoDTO validate(ApiRequestContext apiRequestContext) { protected ValidateReturn validate(ApiRequestContext apiRequestContext) {
return validator.validate(apiRequestContext); return validator.validate(apiRequestContext);
} }
protected Object doRoute(ApiRequestContext apiRequestContext, ApiInfoDTO apiInfo) { protected Object doRoute(RouteContext routeContext) {
ApiRequestContext apiRequestContext = routeContext.getApiRequestContext();
ApiInfoDTO apiInfo = routeContext.getApiInfo();
String tag = apiRequestContext.getTag(); String tag = apiRequestContext.getTag();
// 设置隔离环境 // 设置隔离环境
RpcContextAttachment clientAttachment = RpcContext.getClientAttachment(); RpcContextAttachment clientAttachment = RpcContext.getClientAttachment();

View File

@@ -83,7 +83,7 @@ public class ApiValidator implements Validator {
private DateTimeFormatter dateTimeFormatter; private DateTimeFormatter dateTimeFormatter;
@Override @Override
public ApiInfoDTO validate(ApiRequestContext apiRequestContext) { public ValidateReturn validate(ApiRequestContext apiRequestContext) {
ApiRequest apiRequest = apiRequestContext.getApiRequest(); ApiRequest apiRequest = apiRequestContext.getApiRequest();
ApiInfoDTO apiInfo = apiManager.get(apiRequest.getMethod(), apiRequest.getVersion()); ApiInfoDTO apiInfo = apiManager.get(apiRequest.getMethod(), apiRequest.getVersion());
// 检查接口信息 // 检查接口信息
@@ -106,11 +106,11 @@ public class ApiValidator implements Validator {
checkUploadFile(apiRequestContext); checkUploadFile(apiRequestContext);
// 检查token // 检查token
checkToken(apiRequestContext, apiInfo); checkToken(apiRequestContext, apiInfo);
return apiInfo; return new ValidateReturn(apiInfo, isvDTO);
} }
@Override @Override
public ApiInfoDTO validateRest(ApiRequestContext apiRequestContext) { public ValidateReturn validateRest(ApiRequestContext apiRequestContext) {
ApiRequest apiRequest = apiRequestContext.getApiRequest(); ApiRequest apiRequest = apiRequestContext.getApiRequest();
ApiInfoDTO apiInfo = apiManager.get(apiRequest.getMethod(), apiRequest.getVersion()); ApiInfoDTO apiInfo = apiManager.get(apiRequest.getMethod(), apiRequest.getVersion());
// 检查接口信息 // 检查接口信息
@@ -126,7 +126,7 @@ public class ApiValidator implements Validator {
checkUploadFile(apiRequestContext); checkUploadFile(apiRequestContext);
// 检查token // 检查token
checkToken(apiRequestContext, apiInfo); checkToken(apiRequestContext, apiInfo);
return apiInfo; return new ValidateReturn(apiInfo, null);
} }
public void checkApiInfo(ApiRequestContext apiRequestContext, ApiInfoDTO apiInfoDTO) { public void checkApiInfo(ApiRequestContext apiRequestContext, ApiInfoDTO apiInfoDTO) {

View File

@@ -0,0 +1,16 @@
package com.gitee.sop.gateway.service.validate;
import com.gitee.sop.gateway.common.ApiInfoDTO;
import com.gitee.sop.gateway.service.manager.dto.IsvDTO;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author 六如
*/
@AllArgsConstructor
@Getter
public class ValidateReturn {
private ApiInfoDTO apiInfoDTO;
private IsvDTO isvDTO;
}

View File

@@ -1,6 +1,5 @@
package com.gitee.sop.gateway.service.validate; package com.gitee.sop.gateway.service.validate;
import com.gitee.sop.gateway.common.ApiInfoDTO;
import com.gitee.sop.gateway.request.ApiRequestContext; import com.gitee.sop.gateway.request.ApiRequestContext;
/** /**
@@ -15,7 +14,7 @@ public interface Validator {
* @param apiRequestContext 请求内容 * @param apiRequestContext 请求内容
* @return 校验通过返回路由信息 * @return 校验通过返回路由信息
*/ */
ApiInfoDTO validate(ApiRequestContext apiRequestContext); ValidateReturn validate(ApiRequestContext apiRequestContext);
/** /**
* 校验rest * 校验rest
@@ -23,6 +22,6 @@ public interface Validator {
* @param apiRequestContext 请求内容 * @param apiRequestContext 请求内容
* @return 校验通过返回路由信息 * @return 校验通过返回路由信息
*/ */
ApiInfoDTO validateRest(ApiRequestContext apiRequestContext); ValidateReturn validateRest(ApiRequestContext apiRequestContext);
} }