From 8a27cccdc5592e11f656c43d6450f462cb983ab3 Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 8 Aug 2019 15:56:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B7=AF=E7=94=B1404?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BaseGatewayConfiguration.java | 9 +++++ .../ServiceZookeeperApiMetaManager.java | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java index 1224ae05..5a3c2b6e 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java @@ -12,7 +12,9 @@ import com.gitee.sop.gatewaycommon.gateway.route.ReadBodyRoutePredicateFactory; import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration; import com.gitee.sop.gatewaycommon.manager.RouteManager; import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; +import com.gitee.sop.gatewaycommon.param.ParamBuilder; import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; @@ -21,6 +23,7 @@ import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.web.reactive.result.view.ViewResolver; +import org.springframework.web.server.ServerWebExchange; import java.util.Collections; import java.util.List; @@ -54,6 +57,12 @@ public class BaseGatewayConfiguration extends AbstractConfiguration { return jsonExceptionHandler; } + @Bean + @ConditionalOnMissingBean + ParamBuilder paramBuilder() { + return ApiConfig.getInstance().getGatewayParamBuilder(); + } + /** * 处理返回结果 */ diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java index 191b8e54..ac11af39 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; import com.gitee.sop.servercommon.bean.ServiceApiInfo; import com.gitee.sop.servercommon.bean.ZookeeperTool; import com.gitee.sop.servercommon.exception.ZookeeperPathNotExistException; +import com.gitee.sop.servercommon.route.GatewayPredicateDefinition; import com.gitee.sop.servercommon.route.GatewayRouteDefinition; import com.gitee.sop.servercommon.route.ServiceRouteInfo; import lombok.Getter; @@ -17,6 +18,7 @@ import org.springframework.util.StringUtils; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -39,6 +41,15 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager { private static final String DEFAULT_CONTEXT_PATH = "/"; + /** + * NameVersion=alipay.story.get1.0 + * see com.gitee.sop.gatewaycommon.routeDefinition.NameVersionRoutePredicateFactory + */ + private static String QUERY_PREDICATE_DEFINITION_TPL = "NameVersion=%s"; + + private static ServiceApiInfo.ApiMeta FIRST_API_META = new ServiceApiInfo.ApiMeta("_first.route_", "/", "v_000"); + + private Environment environment; private ZookeeperTool zookeeperTool; @@ -94,6 +105,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager { protected ServiceRouteInfo buildServiceGatewayInfo(ServiceApiInfo serviceApiInfo) { List apis = serviceApiInfo.getApis(); List routeDefinitionList = new ArrayList<>(apis.size()); + routeDefinitionList.add(this.buildReadBodyRouteDefinition(serviceApiInfo)); for (ServiceApiInfo.ApiMeta apiMeta : apis) { GatewayRouteDefinition gatewayRouteDefinition = this.buildGatewayRouteDefinition(serviceApiInfo, apiMeta); routeDefinitionList.add(gatewayRouteDefinition); @@ -136,6 +148,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager { BeanUtils.copyProperties(apiMeta, gatewayRouteDefinition); gatewayRouteDefinition.setId(routeId); gatewayRouteDefinition.setFilters(Collections.emptyList()); + gatewayRouteDefinition.setPredicates(this.buildPredicates(apiMeta)); String uri = this.buildUri(serviceApiInfo, apiMeta); String path = this.buildServletPath(serviceApiInfo, apiMeta); gatewayRouteDefinition.setUri(uri); @@ -143,6 +156,30 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager { return gatewayRouteDefinition; } + protected List buildPredicates(ServiceApiInfo.ApiMeta apiMeta) { + GatewayPredicateDefinition gatewayPredicateDefinition = new GatewayPredicateDefinition(); + gatewayPredicateDefinition.setName("ReadBody"); + return Arrays.asList(gatewayPredicateDefinition, this.buildNameVersionPredicateDefinition(apiMeta)); + } + + protected GatewayPredicateDefinition buildNameVersionPredicateDefinition(ServiceApiInfo.ApiMeta apiMeta) { + return new GatewayPredicateDefinition(String.format(QUERY_PREDICATE_DEFINITION_TPL, apiMeta.fetchNameVersion())); + } + + /** + * 添加com.gitee.sop.gatewaycommon.routeDefinition.ReadBodyRoutePredicateFactory,解决form表单获取不到问题 + * + * @return 返回路由定义 + */ + protected GatewayRouteDefinition buildReadBodyRouteDefinition(ServiceApiInfo serviceApiInfo) { + GatewayRouteDefinition readBodyRouteDefinition = this.buildGatewayRouteDefinition(serviceApiInfo, FIRST_API_META); + readBodyRouteDefinition.setOrder(Integer.MIN_VALUE); + + readBodyRouteDefinition.setPredicates(this.buildPredicates(FIRST_API_META)); + + return readBodyRouteDefinition; + } + protected String buildUri(ServiceApiInfo serviceApiInfo, ServiceApiInfo.ApiMeta apiMeta) { return PROTOCOL_LOAD_BALANCE + serviceApiInfo.getServiceId(); }