|
|
|
@ -2,7 +2,6 @@ package com.gitee.sop.servercommon.manager; |
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
import com.gitee.sop.servercommon.bean.ServiceApiInfo; |
|
|
|
|
import com.gitee.sop.servercommon.route.GatewayPredicateDefinition; |
|
|
|
|
import com.gitee.sop.servercommon.route.RouteDefinition; |
|
|
|
|
import com.gitee.sop.servercommon.route.ServiceRouteInfo; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
@ -13,7 +12,6 @@ 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.stream.Collectors; |
|
|
|
@ -33,13 +31,7 @@ public class ServiceRouteInfoBuilder { |
|
|
|
|
|
|
|
|
|
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 static ServiceApiInfo.ApiMeta FIRST_API_META = new ServiceApiInfo.ApiMeta("_first_route_", "/", "1.0.0"); |
|
|
|
|
|
|
|
|
|
private Environment environment; |
|
|
|
|
|
|
|
|
@ -59,8 +51,9 @@ public class ServiceRouteInfoBuilder { |
|
|
|
|
*/ |
|
|
|
|
protected ServiceRouteInfo buildServiceGatewayInfo(ServiceApiInfo serviceApiInfo) { |
|
|
|
|
List<ServiceApiInfo.ApiMeta> apis = serviceApiInfo.getApis(); |
|
|
|
|
List<RouteDefinition> routeDefinitionList = new ArrayList<>(apis.size()); |
|
|
|
|
routeDefinitionList.add(this.buildReadBodyRouteDefinition(serviceApiInfo)); |
|
|
|
|
List<RouteDefinition> routeDefinitionList = new ArrayList<>(apis.size() + 1); |
|
|
|
|
// 在第一个位置放一个没用的路由,SpringCloudGateway会从第二个路由开始找,原因不详
|
|
|
|
|
routeDefinitionList.add(this.getFirstRoute(serviceApiInfo)); |
|
|
|
|
for (ServiceApiInfo.ApiMeta apiMeta : apis) { |
|
|
|
|
RouteDefinition gatewayRouteDefinition = this.buildGatewayRouteDefinition(serviceApiInfo, apiMeta); |
|
|
|
|
routeDefinitionList.add(gatewayRouteDefinition); |
|
|
|
@ -73,6 +66,17 @@ public class ServiceRouteInfoBuilder { |
|
|
|
|
return serviceRouteInfo; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 添加com.gitee.sop.gatewaycommon.routeDefinition.ReadBodyRoutePredicateFactory,解决form表单获取不到问题 |
|
|
|
|
* |
|
|
|
|
* @return 返回路由定义 |
|
|
|
|
*/ |
|
|
|
|
protected RouteDefinition getFirstRoute(ServiceApiInfo serviceApiInfo) { |
|
|
|
|
RouteDefinition firstRoute = this.buildGatewayRouteDefinition(serviceApiInfo, FIRST_API_META); |
|
|
|
|
firstRoute.setOrder(Integer.MIN_VALUE); |
|
|
|
|
return firstRoute; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 构建路由id MD5 |
|
|
|
|
* |
|
|
|
@ -89,43 +93,19 @@ public class ServiceRouteInfoBuilder { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected RouteDefinition buildGatewayRouteDefinition(ServiceApiInfo serviceApiInfo, ServiceApiInfo.ApiMeta apiMeta) { |
|
|
|
|
RouteDefinition gatewayRouteDefinition = new RouteDefinition(); |
|
|
|
|
RouteDefinition routeDefinition = new RouteDefinition(); |
|
|
|
|
// 唯一id规则:接口名 + 版本号
|
|
|
|
|
String routeId = apiMeta.fetchNameVersion(); |
|
|
|
|
this.checkPath(routeId, "接口定义(" + routeId + ")不能有斜杠字符'/'"); |
|
|
|
|
BeanUtils.copyProperties(apiMeta, gatewayRouteDefinition); |
|
|
|
|
gatewayRouteDefinition.setId(routeId); |
|
|
|
|
gatewayRouteDefinition.setFilters(Collections.emptyList()); |
|
|
|
|
gatewayRouteDefinition.setPredicates(this.buildPredicates(apiMeta)); |
|
|
|
|
BeanUtils.copyProperties(apiMeta, routeDefinition); |
|
|
|
|
routeDefinition.setId(routeId); |
|
|
|
|
routeDefinition.setFilters(Collections.emptyList()); |
|
|
|
|
routeDefinition.setPredicates(Collections.emptyList()); |
|
|
|
|
String uri = this.buildUri(serviceApiInfo, apiMeta); |
|
|
|
|
String path = this.buildServletPath(serviceApiInfo, apiMeta); |
|
|
|
|
gatewayRouteDefinition.setUri(uri); |
|
|
|
|
gatewayRouteDefinition.setPath(path); |
|
|
|
|
return gatewayRouteDefinition; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected List<GatewayPredicateDefinition> 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 RouteDefinition buildReadBodyRouteDefinition(ServiceApiInfo serviceApiInfo) { |
|
|
|
|
RouteDefinition readBodyRouteDefinition = this.buildGatewayRouteDefinition(serviceApiInfo, FIRST_API_META); |
|
|
|
|
readBodyRouteDefinition.setOrder(Integer.MIN_VALUE); |
|
|
|
|
|
|
|
|
|
readBodyRouteDefinition.setPredicates(this.buildPredicates(FIRST_API_META)); |
|
|
|
|
|
|
|
|
|
return readBodyRouteDefinition; |
|
|
|
|
routeDefinition.setUri(uri); |
|
|
|
|
routeDefinition.setPath(path); |
|
|
|
|
return routeDefinition; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected String buildUri(ServiceApiInfo serviceApiInfo, ServiceApiInfo.ApiMeta apiMeta) { |
|
|
|
|