diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteCache.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteCache.java index 7e7c8458..e5bbb756 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteCache.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteCache.java @@ -1,7 +1,5 @@ package com.gitee.sop.gatewaycommon.gateway.route; -import com.gitee.sop.gatewaycommon.bean.GatewayFilterDefinition; -import com.gitee.sop.gatewaycommon.bean.GatewayPredicateDefinition; import com.gitee.sop.gatewaycommon.bean.RouteDefinition; import com.gitee.sop.gatewaycommon.bean.ServiceRouteInfo; import com.gitee.sop.gatewaycommon.manager.BaseRouteCache; @@ -11,9 +9,9 @@ import org.springframework.cloud.gateway.filter.FilterDefinition; import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition; import java.net.URI; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import java.util.stream.Collectors; /** * @author tanghc @@ -25,45 +23,37 @@ public class GatewayRouteCache extends BaseRouteCache { } @Override - protected GatewayTargetRoute buildRouteDefinition(ServiceRouteInfo serviceRouteInfo, RouteDefinition gatewayRouteDefinition) { - org.springframework.cloud.gateway.route.RouteDefinition routeDefinition = new org.springframework.cloud.gateway.route.RouteDefinition(); - routeDefinition.setId(gatewayRouteDefinition.getId()); - routeDefinition.setUri(URI.create(gatewayRouteDefinition.getUri() + "#" + gatewayRouteDefinition.getPath())); - routeDefinition.setOrder(gatewayRouteDefinition.getOrder()); - List filterDefinitionList = new ArrayList<>(gatewayRouteDefinition.getFilters().size()); - LinkedList predicateDefinitionList = new LinkedList<>(); - for (GatewayFilterDefinition filter : gatewayRouteDefinition.getFilters()) { - FilterDefinition filterDefinition = new FilterDefinition(); - BeanUtils.copyProperties(filter, filterDefinition); - filterDefinitionList.add(filterDefinition); - } + protected GatewayTargetRoute buildTargetRoute(ServiceRouteInfo serviceRouteInfo, RouteDefinition routeDefinition) { + org.springframework.cloud.gateway.route.RouteDefinition targetRoute = new org.springframework.cloud.gateway.route.RouteDefinition(); + targetRoute.setId(routeDefinition.getId()); + targetRoute.setUri(URI.create(routeDefinition.getUri() + "#" + routeDefinition.getPath())); + targetRoute.setOrder(routeDefinition.getOrder()); + // 添加过滤器 + List filterDefinitionList = routeDefinition.getFilters() + .stream() + .map(filter -> { + FilterDefinition filterDefinition = new FilterDefinition(); + BeanUtils.copyProperties(filter, filterDefinition); + return filterDefinition; + }) + .collect(Collectors.toList()); - for (GatewayPredicateDefinition predicate : gatewayRouteDefinition.getPredicates()) { - PredicateDefinition predicateDefinition = new PredicateDefinition(); - BeanUtils.copyProperties(predicate, predicateDefinition); - predicateDefinitionList.add(predicateDefinition); - } - this.addPredicate(predicateDefinitionList, "NameVersion", gatewayRouteDefinition.getId()); - this.addPredicate(predicateDefinitionList, "ReadBody", ""); - routeDefinition.setFilters(filterDefinitionList); - routeDefinition.setPredicates(predicateDefinitionList); - return new GatewayTargetRoute(serviceRouteInfo, gatewayRouteDefinition, routeDefinition); - } - /** - * 添加断言 - * - * @param predicateDefinitionList - * @param name 断言名称 - * @param args 断言参数 - */ - protected void addPredicate(LinkedList predicateDefinitionList, String name, String args) { - for (PredicateDefinition predicateDefinition : predicateDefinitionList) { - // 如果已经存在,直接返回 - if (predicateDefinition.getName().equals(name)) { - return; - } - } - predicateDefinitionList.addFirst(new PredicateDefinition(name + "=" + args)); + LinkedList predicateDefinitionList = routeDefinition.getPredicates() + .stream() + .map(predicate -> { + PredicateDefinition predicateDefinition = new PredicateDefinition(); + BeanUtils.copyProperties(predicate, predicateDefinition); + return predicateDefinition; + }) + .collect(Collectors.toCollection(LinkedList::new)); + // 添加下面两个自定义的断言到顶部 + predicateDefinitionList.addFirst(new PredicateDefinition("NameVersion=" + routeDefinition.getId())); + predicateDefinitionList.addFirst(new PredicateDefinition("ReadBody=")); + + targetRoute.setFilters(filterDefinitionList); + targetRoute.setPredicates(predicateDefinitionList); + return new GatewayTargetRoute(serviceRouteInfo, routeDefinition, targetRoute); } + } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java index b0b21075..8a5661c7 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java @@ -6,7 +6,6 @@ import com.gitee.sop.gatewaycommon.manager.RouteRepository; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.cloud.gateway.event.PredicateArgsEvent; -import org.springframework.cloud.gateway.event.RefreshRoutesEvent; import org.springframework.cloud.gateway.route.RouteDefinitionRepository; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; @@ -40,7 +39,6 @@ public class GatewayRouteRepository implements ApplicationEventPublisherAware, public Flux getRouteDefinitions() { List list = routes.values().parallelStream() .map(TargetRoute::getTargetRouteDefinition) - .filter(routeDefinition -> !routeDefinition.getId().contains("_first.route_")) .collect(Collectors.toList()); return Flux.fromIterable(list); } @@ -76,9 +74,8 @@ public class GatewayRouteRepository implements ApplicationEventPublisherAware, */ @Override public String add(GatewayTargetRoute targetRoute) { - RouteDefinition baseRouteDefinition = targetRoute.getRouteDefinition(); - routes.put(baseRouteDefinition.getId(), targetRoute); - this.publisher.publishEvent(new RefreshRoutesEvent(this)); + RouteDefinition routeDefinition = targetRoute.getRouteDefinition(); + routes.put(routeDefinition.getId(), targetRoute); return "success"; } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java index cb8a0a86..bd0f9b0c 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java @@ -17,6 +17,9 @@ import java.util.function.Predicate; /** * 此断言决定执行哪个路由 * + * 使用地方: + * @see com.gitee.sop.gatewaycommon.gateway.route.GatewayRouteCache + * * @author tanghc */ @Slf4j diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/ReadBodyRoutePredicateFactory.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/ReadBodyRoutePredicateFactory.java index ba459f29..d8f48b1c 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/ReadBodyRoutePredicateFactory.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/ReadBodyRoutePredicateFactory.java @@ -36,6 +36,10 @@ import static org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilt * * @see org.springframework.cloud.gateway.handler.predicate.ReadBodyPredicateFactory * 详见:https://blog.51cto.com/thinklili/2329184 + * + * 使用地方: + * @see com.gitee.sop.gatewaycommon.gateway.route.GatewayRouteCache + * * @author tanghc */ public class ReadBodyRoutePredicateFactory extends AbstractRoutePredicateFactory { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/BaseRouteCache.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/BaseRouteCache.java index 814c37de..4160f5dd 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/BaseRouteCache.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/BaseRouteCache.java @@ -1,5 +1,6 @@ package com.gitee.sop.gatewaycommon.manager; +import com.alibaba.fastjson.JSON; import com.gitee.sop.gatewaycommon.bean.RouteDefinition; import com.gitee.sop.gatewaycommon.bean.ServiceRouteInfo; import com.gitee.sop.gatewaycommon.bean.TargetRoute; @@ -31,7 +32,7 @@ public abstract class BaseRouteCache implements RouteLoad * @param gatewayRouteDefinition 路由对象 * @return 返回目标路由对象 */ - protected abstract T buildRouteDefinition(ServiceRouteInfo serviceRouteInfo, RouteDefinition gatewayRouteDefinition); + protected abstract T buildTargetRoute(ServiceRouteInfo serviceRouteInfo, RouteDefinition gatewayRouteDefinition); public BaseRouteCache(RouteRepository routeRepository) { this.routeRepository = routeRepository; @@ -48,9 +49,12 @@ public abstract class BaseRouteCache implements RouteLoad } serviceIdMd5Map.put(serviceId, newMd5); List routeDefinitionList = serviceRouteInfo.getRouteDefinitionList(); - for (RouteDefinition gatewayRouteDefinition : routeDefinitionList) { - T routeDefinition = this.buildRouteDefinition(serviceRouteInfo, gatewayRouteDefinition); - routeRepository.add(routeDefinition); + for (RouteDefinition routeDefinition : routeDefinitionList) { + T targetRoute = this.buildTargetRoute(serviceRouteInfo, routeDefinition); + routeRepository.add(targetRoute); + if (log.isDebugEnabled()) { + log.debug("新增路由:{}", JSON.toJSONString(routeDefinition)); + } } callback.accept(null); } catch (Exception e) { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java index 1824af84..09021c1f 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java @@ -41,12 +41,6 @@ public class ServiceRoutesLoader { @Autowired private NacosDiscoveryProperties nacosDiscoveryProperties; -// @NacosInjected -// private NamingService namingService; - -// @Autowired -// private NacosConfigProperties nacosConfigProperties; - @NacosInjected private ConfigService configService; diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteCache.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteCache.java index 9b4d3485..df6c64b7 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteCache.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteCache.java @@ -20,7 +20,7 @@ public class ZuulRouteCache extends BaseRouteCache { } @Override - protected ZuulTargetRoute buildRouteDefinition(ServiceRouteInfo serviceRouteInfo, RouteDefinition gatewayRouteDefinition) { + protected ZuulTargetRoute buildTargetRoute(ServiceRouteInfo serviceRouteInfo, RouteDefinition gatewayRouteDefinition) { Route route = new Route( gatewayRouteDefinition.getId() , gatewayRouteDefinition.getPath() diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteInfoBuilder.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteInfoBuilder.java index fba5ae55..13784204 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteInfoBuilder.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteInfoBuilder.java @@ -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 apis = serviceApiInfo.getApis(); - List routeDefinitionList = new ArrayList<>(apis.size()); - routeDefinitionList.add(this.buildReadBodyRouteDefinition(serviceApiInfo)); + List 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 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) { diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/RouteDefinition.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/RouteDefinition.java index 2f12cda8..5cd8a343 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/RouteDefinition.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/RouteDefinition.java @@ -2,7 +2,6 @@ package com.gitee.sop.servercommon.route; import lombok.Data; -import java.util.ArrayList; import java.util.List; /** @@ -28,12 +27,12 @@ public class RouteDefinition { /** * 路由断言集合配置 */ - private List predicates = new ArrayList<>(); + private List predicates; /** * 路由过滤器集合配置 */ - private List filters = new ArrayList<>(); + private List filters; /** * 路由规则转发的目标uri diff --git a/sop-gateway/src/main/resources/application-dev.properties b/sop-gateway/src/main/resources/application-dev.properties index a1dc0b30..f2312fb7 100644 --- a/sop-gateway/src/main/resources/application-dev.properties +++ b/sop-gateway/src/main/resources/application-dev.properties @@ -57,3 +57,5 @@ spring.servlet.multipart.max-request-size=20MB spring.zipkin.base-url=${zipkin.url} # 设置sleuth收集信息的比率,默认0.1,最大是1,数字越大越耗性能 spring.sleuth.sampler.probability=1 + +logging.level.com.gitee=debug \ No newline at end of file diff --git a/sop-test/src/test/java/com/gitee/sop/test/SpringCloudGatewayClientPostTest.java b/sop-test/src/test/java/com/gitee/sop/test/SpringCloudGatewayClientPostTest.java index 395ae027..fee40b44 100644 --- a/sop-test/src/test/java/com/gitee/sop/test/SpringCloudGatewayClientPostTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/SpringCloudGatewayClientPostTest.java @@ -126,6 +126,45 @@ biz_content String 是 请求参数的集合,最大长度不限,除公共 bizContent.put("name", "葫芦娃"); // bizContent.put("name", "葫芦娃1234567890葫芦娃1234567890"); // 超出长度 + params.put("biz_content", JSON.toJSONString(bizContent)); + + System.out.println("----------- 请求信息 -----------"); + System.out.println("请求参数:" + buildParamQuery(params)); + System.out.println("商户秘钥:" + privateKey); + String content = AlipaySignature.getSignContent(params); + System.out.println("待签名内容:" + content); + String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8"); + System.out.println("签名(sign):" + sign); + + params.put("sign", sign); + + System.out.println("----------- 返回结果 -----------"); + String responseData = post(url, params);// 发送请求 + System.out.println(responseData); + } + + public void testPost2() throws Exception { + + // 公共请求参数 + Map params = new HashMap(); + params.put("app_id", appId); + params.put("method", "alipay.category.get"); + params.put("format", "json"); + params.put("charset", "utf-8"); + params.put("sign_type", "RSA2"); + params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); + params.put("version", "1.0"); + + // 业务参数 + Map bizContent = new HashMap<>(); + bizContent.put("categoryName", "啊啊啊"); + Map storyParam = new HashMap<>(); + storyParam.put("id", "1"); + storyParam.put("name", "葫芦娃啊啊啊"); + bizContent.put("storyParam", storyParam); + + + params.put("biz_content", JSON.toJSONString(bizContent)); System.out.println("----------- 请求信息 -----------");