pull/1/MERGE
tanghc 5 years ago
parent 0d3489f17e
commit 69131929c7
  1. 15
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java
  2. 5
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/controller/GatewayController.java
  3. 9
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java
  4. 13
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayForwardChooser.java
  5. 10
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/ApiParam.java
  6. 11
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseForwardChooser.java
  7. 6
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ForwardInfo.java

@ -43,8 +43,8 @@ import java.util.function.Consumer;
@Slf4j
public class ServerWebExchangeUtil {
private static final String THROWABLE_KEY = "sop.throwable";
private static final String UNKNOWN_PATH = "/sop/unknown";
private static final String VALIDATE_ERROR_PATH = "/sop/validateError";
private static final String REST_PATH = "/rest";
private static FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
@ -63,8 +63,9 @@ public class ServerWebExchangeUtil {
* @return 返回新的ServerWebExchange配合chain.filter(newExchange);使用
*/
public static ServerWebExchange getForwardExchange(ServerWebExchange exchange, ForwardInfo forwardInfo) {
ServerHttpRequest newRequest = exchange.getRequest()
.mutate()
ServerHttpRequest.Builder builder = exchange.getRequest()
.mutate();
ServerHttpRequest newRequest = builder
.header(ParamNames.HEADER_VERSION_NAME, forwardInfo.getVersion())
.path(forwardInfo.getPath()).build();
return exchange.mutate().request(newRequest).build();
@ -153,6 +154,14 @@ public class ServerWebExchangeUtil {
return apiParam;
}
public static void setThrowable(ServerWebExchange exchange, Throwable throwable) {
exchange.getAttributes().put(THROWABLE_KEY, throwable);
}
public static Throwable getThrowable(ServerWebExchange exchange) {
return (Throwable)exchange.getAttribute(THROWABLE_KEY);
}
/**
* 获取请求参数
*

@ -4,7 +4,6 @@ import com.gitee.sop.gatewaycommon.bean.ApiContext;
import com.gitee.sop.gatewaycommon.exception.ApiException;
import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil;
import com.gitee.sop.gatewaycommon.message.ErrorEnum;
import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.result.ResultExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -25,10 +24,10 @@ public class GatewayController {
*/
@RequestMapping("/sop/validateError")
public Mono<String> validateError(ServerWebExchange exchange) {
ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange);
Throwable throwable = ServerWebExchangeUtil.getThrowable(exchange);
// 合并微服务传递过来的结果,变成最终结果
ResultExecutor<ServerWebExchange, String> resultExecutor = ApiContext.getApiConfig().getGatewayResultExecutor();
String gatewayResult = resultExecutor.buildErrorResult(exchange, apiParam.getThrowable());
String gatewayResult = resultExecutor.buildErrorResult(exchange, throwable);
return Mono.just(gatewayResult);
}

@ -79,7 +79,7 @@ public class IndexFilter implements WebFilter {
// 构建ApiParam
ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange, body);
// 签名验证
doValidate(apiParam);
doValidate(exchange, apiParam);
return Mono.just(body);
});
BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, (Class) String.class);
@ -108,7 +108,8 @@ public class IndexFilter implements WebFilter {
// 构建ApiParam
ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange, originalQuery);
// 签名验证
doValidate(apiParam);
doValidate(exchange, apiParam);
ForwardInfo forwardInfo = gatewayForwardChooser.getForwardInfo(exchange);
ServerWebExchange forwardExchange = ServerWebExchangeUtil.getForwardExchange(exchange, forwardInfo);
return chain.filter(forwardExchange);
@ -118,12 +119,12 @@ public class IndexFilter implements WebFilter {
}
}
private void doValidate(ApiParam apiParam) {
private void doValidate(ServerWebExchange exchange, ApiParam apiParam) {
try {
validator.validate(apiParam);
} catch (ApiException e) {
log.error("验证失败,ip:{}, params:{}, errorMsg:{}", apiParam.fetchIp(), apiParam.toJSONString(), e.getMessage());
apiParam.setThrowable(e);
ServerWebExchangeUtil.setThrowable(exchange, e);
}
}

@ -3,14 +3,27 @@ package com.gitee.sop.gatewaycommon.gateway.route;
import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil;
import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.route.BaseForwardChooser;
import com.gitee.sop.gatewaycommon.route.ForwardInfo;
import org.springframework.web.server.ServerWebExchange;
/**
* @author tanghc
*/
public class GatewayForwardChooser extends BaseForwardChooser<ServerWebExchange> {
private static final String VALIDATE_ERROR_PATH = "/sop/validateError";
@Override
protected ApiParam getApiParam(ServerWebExchange exchange) {
return ServerWebExchangeUtil.getApiParam(exchange);
}
@Override
public ForwardInfo getForwardInfo(ServerWebExchange exchange) {
// 如果有异常,直接跳转到异常处理
if (ServerWebExchangeUtil.getThrowable(exchange) != null) {
return new ForwardInfo(VALIDATE_ERROR_PATH, "");
}
return super.getForwardInfo(exchange);
}
}

@ -14,7 +14,7 @@ import java.util.Map;
*/
public class ApiParam extends JSONObject implements Param {
private static final String THROWABLE_KEY = "Throwable";
public ApiParam() {
}
@ -33,14 +33,6 @@ public class ApiParam extends JSONObject implements Param {
private transient UploadContext uploadContext;
public void setThrowable(Throwable throwable) {
this.put(THROWABLE_KEY, throwable);
}
public Throwable getThrowable() {
return (Throwable)get(THROWABLE_KEY);
}
public void fitNameVersion() {
if (restName != null) {
this.put(ParamNames.API_NAME, restName);

@ -21,12 +21,11 @@ public abstract class BaseForwardChooser<T> implements ForwardChooser<T> {
@Override
public ForwardInfo getForwardInfo(T t) {
ApiParam apiParam = getApiParam(t);
ForwardInfo forwardInfo = new ForwardInfo();
String nameVersion = apiParam.fetchNameVersion();
TargetRoute targetRoute = RouteRepositoryContext.getRouteRepository().get(nameVersion);
RouteDefinition routeDefinitionOrig = targetRoute.getRouteDefinition();
String path = routeDefinitionOrig.getPath();
String versionInHead = apiParam.fetchVersion();
String version = apiParam.fetchVersion();
String serviceId = targetRoute.getServiceRouteInfo().fetchServiceIdLowerCase();
// 如果服务在灰度阶段,返回一个灰度版本号
String grayVersion = envGrayManager.getVersion(serviceId, nameVersion);
@ -36,7 +35,7 @@ public abstract class BaseForwardChooser<T> implements ForwardChooser<T> {
TargetRoute targetRouteDest = RouteRepositoryContext.getRouteRepository().get(newNameVersion);
if (targetRouteDest != null) {
if (BooleanUtils.toBoolean(routeDefinitionOrig.getCompatibleMode())) {
versionInHead = grayVersion;
version = grayVersion;
} else {
// 获取灰度接口
RouteDefinition routeDefinition = targetRouteDest.getRouteDefinition();
@ -44,11 +43,7 @@ public abstract class BaseForwardChooser<T> implements ForwardChooser<T> {
}
}
}
forwardInfo.setPath(path);
forwardInfo.setVersion(versionInHead);
return forwardInfo;
return new ForwardInfo(path, version);
}
}

@ -10,4 +10,10 @@ public class ForwardInfo {
private String path;
private String version;
private String domain;
public ForwardInfo(String path, String version) {
this.path = path;
this.version = version;
}
}

Loading…
Cancel
Save