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