Spring-Cloud-Gateway 源码解析 —— 处理器 (3.3) 之 FilteringWebHandler 创建过滤器链

x33g5p2x  于2021-12-20 转载在 其他  
字(1.6k)|赞(0)|评价(0)|浏览(378)

1. 概述

本文主要分享 FilteringWebHandler

在 《Spring-Cloud-Gateway 源码解析 —— 处理器 (3.2) 之 RoutePredicateHandlerMapping 路由匹配 》「2.1 SimpleHandlerAdapter」 里,我们看到 SimpleHandlerAdapter#handle(ServerWebExchange, Object) 调用 FilteringWebHandler#handle(ServerWebExchange) 方法,处理请求。

FilteringWebHandler 通过创建请求对应的 Route 对应的 GatewayFilterChain 进行处理。

2. FilteringWebHandler

org.springframework.cloud.gateway.handler.FilteringWebHandler ,#handle(ServerWebExchange) 代码如下 :

1: public class FilteringWebHandler implements WebHandler {
 2: 
 3: 	/**
 4: 	 * 全局过滤器
 5: 	*/
 6: 	private final List<GatewayFilter> globalFilters;
 7: 
 8: 	@Override
 9: 	public Mono<Void> handle(ServerWebExchange exchange) {
10: 	    // 获得 Route
11: 		Route route = exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR);
12: 		// 获得 GatewayFilter
13: 		List<GatewayFilter> gatewayFilters = route.getFilters();
14: 		List<GatewayFilter> combined = new ArrayList<>(this.globalFilters);
15: 		combined.addAll(gatewayFilters);
16: 
17: 		// 排序
18: 		//TODO: needed or cached?
19: 		AnnotationAwareOrderComparator.sort(combined);
20: 		logger.debug("Sorted gatewayFilterFactories: "+ combined);
21: 
22: 		// 创建 DefaultGatewayFilterChain
23: 		return new DefaultGatewayFilterChain(combined).filter(exchange);
24: 	}
25: }

相关文章