javascript
Spring Cloud Gateway Predicate.Path过滤分析
源碼位置
spring-cloud-gateway-core-2.0.1.RELEASE-sources.jar!/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicateFactory.java
過濾方法
?? ?@Overridepublic Predicate<ServerWebExchange> apply(Config config) {synchronized (this.pathPatternParser) {//僅在第一次調用路由時執行,將pattern字符串轉PathPattern對象config.pathPattern = this.pathPatternParser.parse(config.pattern);}return exchange -> {PathContainer path = parsePath(exchange.getRequest().getURI().getPath());//path(url)轉PathContainer對象 ?? //在此位置設置斷點,每次走到需要predicate.Path過濾的路徑都會進入到這里boolean match = config.pathPattern.matches(path);//路徑表達式匹配判斷traceMatch("Pattern", config.pathPattern.getPatternString(), path, match);if (match) {PathMatchInfo uriTemplateVariables = config.pathPattern.matchAndExtract(path);exchange.getAttributes().put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);return true;} else {return false;}};}手動測試表達式效果
new PathPatternParser().parse("/sys*/**").matches(path)
匹配/sys-aaa/sadf、/sysf3s/sa等格式路徑
?
進入邏輯:
onNext:reactor-core-3.1.10.RELEASE-sources.jar!/reactor/core/publisher/MonoFilterWhen.java
從這里進入gateway
lookupRoute:spring-cloud-gateway-core-2.0.1.RELEASE-sources.jar!/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMapping.java
進入
toAsyncPredicate:spring-cloud-gateway-core-2.0.1.RELEASE-sources.jar!/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java
最后進入
lambda$apply$:spring-cloud-gateway-core-2.0.1.RELEASE-sources.jar!/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicateFactory.java
總結
以上是生活随笔為你收集整理的Spring Cloud Gateway Predicate.Path过滤分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络抓包分析
- 下一篇: PN512使用的一些小技巧