Gateway(四)Filter

2020年4月18日 | 作者 Siran | 800字 | 阅读大约需要2分钟
归档于 gateway | 标签 #gateway

1. 概述

Gateway(三)Predicate 一文中,如果Handler Mapping 匹配成功则会通过 GatewayWebHandler 创建请求对应的Route 对应的Filter Chain 来进行处理请求


2. FilteringWebHandler

org.springframework.cloud.gateway.handler.FilteringWebHandler,代码如下:

public class FilteringWebHandler implements WebHandler {

	protected static final Log logger = LogFactory.getLog(FilteringWebHandler.class);

	/**
	 * 全局过滤器
	 */
	private final List<GatewayFilter> globalFilters;

	public FilteringWebHandler(List<GlobalFilter> globalFilters) {
		this.globalFilters = loadFilters(globalFilters);
	}
  //① 组成过滤链
	private static List<GatewayFilter> loadFilters(List<GlobalFilter> filters) {
		return filters.stream().map(filter -> {
			GatewayFilterAdapter gatewayFilter = new GatewayFilterAdapter(filter);
			//当 GlobalFilter 子类实现了 org.springframework.core.Ordered 接口,在委托一层 OrderedGatewayFilter 。
			//这样 AnnotationAwareOrderComparator#sort(List) 方法好排序。
			if (filter instanceof Ordered) {
				int order = ((Ordered) filter).getOrder();
				return new OrderedGatewayFilter(gatewayFilter, order);
			}
			return gatewayFilter;
		}).collect(Collectors.toList());
	}
  
  //② 按照过滤链的顺序依次执行
 	@Override
	public Mono<Void> handle(ServerWebExchange exchange) {
		// 获得 Route
		Route route = exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR);
		// 获得 GatewayFilter
		List<GatewayFilter> gatewayFilters = route.getFilters();

		List<GatewayFilter> combined = new ArrayList<>(this.globalFilters);
		combined.addAll(gatewayFilters);
		// TODO: needed or cached?  排序
		AnnotationAwareOrderComparator.sort(combined);

		if (logger.isDebugEnabled()) {
			logger.debug("Sorted gatewayFilterFactories: " + combined);
		}
		// 创建 DefaultGatewayFilterChain
		return new DefaultGatewayFilterChain(combined).filter(exchange);
	}
}
  • ① 组成过滤链
  • ② 按照过滤链的顺序依次执行

2.1 GatewayFilter与 GlobalFilter 的关系

	private final List<GatewayFilter> globalFilters;

	public FilteringWebHandler(List<GlobalFilter> globalFilters) {
		this.globalFilters = loadFilters(globalFilters);
	}
  • GatewayFilter : 网关过滤器接口

    public interface GatewayFilter {
      Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
    }
    

    GatewayFilter 有三种类型的子类实现。

    • OrderedGatewayFilter有序的网关过滤器实现类。在 FilterChain 里,过滤器数组首先会按照 order 升序排序,按照顺序过滤请求

      public class OrderedGatewayFilter implements GatewayFilter, Ordered {
        //委托的 GatewayFilter 。
          private final GatewayFilter delegate;
        //order代表顺序
          private final int order;
          
          public OrderedGatewayFilter(GatewayFilter delegate, int order) {
              this.delegate = delegate;
              this.order = order;
          }
          
          @Override
          public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
              return this.delegate.filter(exchange, chain);
          }
          
          @Override
          public int getOrder() {
              return this.order;
          }
      }
      
    • ThrottleGatewayFilter:用于限流的。

    • GatewayFilterAdapter:网关过滤器适配器。在 GatewayFilterChain 使用 GatewayFilter 过滤请求,所以通过 GatewayFilterAdapter 将 GlobalFilter 适配成 GatewayFilter 。GatewayFilterAdapter 代码如下 :

      private static class GatewayFilterAdapter implements GatewayFilter {
          //委托的 GlobalFilter 。
          private final GlobalFilter delegate;
              
          public GatewayFilterAdapter(GlobalFilter delegate) {
              this.delegate = delegate;
          }
              
          @Override
          public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
              return this.delegate.filter(exchange, chain);
          }
      }
      
  • GlobalFilter:全局过滤器接口,会作用到所有的Route上。

    public interface GlobalFilter {
      Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
      
    }
    

GatewayFilterFactory

Gateway(三)Predicate 中,已经阐述了PredicateFactory的各种子类实现。GatewayFilterFactory 也一样,这里就不分析了。