在Spring-Cloud-Gateway之请求处理流程文中我们了解最终网关是将请求交给过滤器链表进行处理,接下来我们阅读Spring-Cloud-Gateway的整个过滤器类结构以及主要功能
通过源码可以看到Spring-Cloud-Gateway的filter包中吉接口有如下三个,GatewayFilter,GlobalFilter,GatewayFilterChain,下来我依次阅读接口的主要实现功能。
GatewayFilterChain
类图
代码
/** * 网关过滤链表接口 * 用于过滤器的链式调用 * Contract to allow a {@link WebFilter} to delegate to the next in the chain. * * @author Rossen Stoyanchev * @since 5.0 */public interface GatewayFilterChain { /** * 链表启动调用入口方法 * Delegate to the next {@code WebFilter} in the chain. * @param exchange the current server exchange * @return {@code Mono<Void>} to indicate when request handling is complete */ Mono<Void> filter(ServerWebExchange exchange);} /** * 网关过滤的链表,用于过滤器的链式调用 * 过滤器链表接口的默认实现, * 包含2个构建函数: * 1.集合参数构建用于初始化吧构建链表 * 2. index,parent参数用于构建当前执行过滤对应的下次执行的链表 */ private static class DefaultGatewayFilterChain implements GatewayFilterChain { /** * 当前过滤执行过滤器在集合中索引 */ private final int index; /** * 过滤器集合 */ private final List<GatewayFilter> filters; public DefaultGatewayFilterChain(List<GatewayFilter> filters) { this.filters = filters; this.index = 0; } /** * 构建 * @param parent 上一个执行过滤器对应的FilterChain * @param index 当前要执行过滤器的索引 */ private DefaultGatewayFilterChain(DefaultGatewayFilterChain parent, int index) { this.filters = parent.getFilters(); this.index = index; } public List<GatewayFilter> getFilters() { return filters; } /** * @param exchange the current server exchange * @return */ @Override public Mono<Void> filter(ServerWebExchange exchange) { return Mono.defer(() -> { if (this.index < filters.size()) { //获取当前索引的过滤器 GatewayFilter filter = filters.get(this.index); //构建当前索引的下一个过滤器的FilterChain DefaultGatewayFilterChain chain = new DefaultGatewayFilterChain(this, this.index + 1); //调用过滤器的filter方法执行过滤器 return filter.filter(exchange, chain); } else { //当前索引大于等于过滤集合大小,标识所有链表都已执行完毕,返回空 return Mono.empty(); // complete } }); } }过滤器的GatewayFilterChain 执行顺序
GatewayFilter
类图
/** * 网关路由过滤器, * Contract for interception-style, chained processing of Web requests that may * be used to implement cross-cutting, application-agnostic requirements such * as security, timeouts, and others. Specific to a Gateway * * Copied from WebFilter * * @author Rossen Stoyanchev * @since 5.0 */public interface GatewayFilter extends ShortcutConfigurable { String NAME_KEY = "name"; String VALUE_KEY = "value"; /** * 过滤器执行方法 * Process the Web request and (optionally) delegate to the next * {@code WebFilter} through the given {@link GatewayFilterChain}. * @param exchange the current server exchange * @param chain provides a way to delegate to the next filter * @return {@code Mono<Void>} to indicate when request processing is complete */ Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);}网关过滤器接口,有且只有一个方法filter,执行当前过滤器,并在此方法中决定过滤器链表是否继续往下执行,接下来我们看下几个主要的功能实现类
OrderedGatewayFilter
OrderedGatewayFilter实现类主要目的是为了将目标过滤器包装成可排序的对象类型。是目标过滤器的包装类
GatewayFilterAdapter
GatewayFilterAdapter实现类主要目的是为了将GlobalFilter过滤器包装成GatewayFilter类型的对应。是GlobalFilter过滤器的包装类
GlobalFilter
GlobalFilter 为请求业务以及路由的URI转换为真实业务服务的请求地址的核心过滤器,不需要配置,模式系统初始化时加载,并作用在每个路由上。
初始化加载,通过GatewayAutoConfiguration自动创建
GatewayAutoConfiguration 类
GatewayLoadBalancerClientAutoConfiguration 类
GlobalFilter转换成GatewayFilter,并作用于每个路由上,在FilteringWebHandler实现
FilteringWebHandler类
loadFilters方法是将全局路由使用GatewayFilterAdapter包装成GatewayFilter
handle方法
- 获取当前请求使用的路由Route
- 获取路由配置的过滤器集合route.getFilters()
- 合并全过滤器与路由配置过滤器combined
- 对过滤器排序AnnotationAwareOrderComparator.sort
- 通过过滤器集合构建顶级链表DefaultGatewayFilterChain,并对其当前请求调用链表的filter方法。
==备注==:
Spring-Cloud-Gateway的过滤器接口分为两种:
- GlobalFilter : 全局过滤器,不需要在配置文件中配置,作用在所有的路由上,最终通过GatewayFilterAdapter包装成GatewayFilterChain可识别的过滤器
- GatewayFilter : 需要通过spring.cloud.routes.filters 配置在具体路由下,只作用在当前路由上或通过spring.cloud.default-filters配置在全局,作用在所有路由上
至此,网关过滤器的整个结构以及加载使用流程源码已经阅读完毕,下篇重点学习下路由配置的过滤器加载创建流程
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。