APP下载

spring-cloud-gateway之GatewayFilterFactory

消息来源:baojiabao.com 作者: 发布时间:2024-05-17

报价宝综合消息spring-cloud-gateway之GatewayFilterFactory

对于有些非通用的功能或者面向使用者自定义的功能采用全域性过滤器实现显然是不合理的,基于此出发开源作者变换了思路,采用工厂模式来即时生产一个自定义或者配置的过滤器来过滤当前的请求。

工厂界面与工厂类依赖图

自上而下分析

gateway声明了一个工厂界面GatewayFilterFactory,此界面继承了ShortcutConfigurable,Configurable界面。预设不支援Configurable界面的操作,需要继承AbstractNameValueGatewayFilterFactory才具备Configuable界面行为。

GatewayFilterFactory

Configurable界面是为了实现一些配置资讯(key-value)的使用方便而宣告的一个interface,具体作用体现在AbstractNameValueGatewayFilterFactory的子类实现中。

FunctionalInterface函式式界面宣告注解,将GatewayFilterFactory宣告为一个函式式界面。

ShortcutConfigurable是gateway实现的一个支援工具类,用于引数解析。包含两个列举实现类,DEFAULT和GATHER_LIST,gateway预设使用的DEFAULT;RouteDefinitionRouteLocator#lookup方法中解析断言引数时使用了此列举解析;在RouteDefinitionRouteLocator#loadGatewayFilters方法中载入配置过滤器时也使用此列举解析。

AbstractGatewayFilterFactory是GatewayFilterFactory界面的直接抽象实现类,作者意图在于实现扩充套件性,并且标明是一个会被release的类。

AbstractNameValueGatewayFilterFactory是AbstractGatewayFilterFactory的其中一个抽象子类,重点在于其泛型Config类是一个键值对型别的,被若干个键值对配置的子类继承,如AddRequestHeaderGatewayFilterFactory等。

AbstractChangeRequestUriGatewayFilterFactory是AbstractGatewayFilterFactory的另一个抽象子类,泛型引数与AbstractGatewayFilterFactory一致,只有一个引数名,实现类目前只有RequestHeaderToRequestUriGatewayFilterFactory。

过滤器工厂类分析

AddRequestHeaderGatewayFilterFactory 在request header中新增一个键值对的header引数。AddRequestParameterGatewayFilterFactory 在request query引数列表中新增一个查询引数。AddResponseHeaderGatewayFilterFactory 在response header中新增一个键值对的header引数。DedupeResponseHeaderGatewayFilterFactory 删除response header中重复的资料项,分别有三种资料保留策略。HystrixGatewayFilterFactory 熔断器,集成了Netflix开源的Hystrix框架熔断功能,为了避免服务在故障时引发级联故障,通过Hystrix允许下游服务故障时提供熔断返回或者请求转发操作;主要转发逻辑在RouteHystrixCommand中的resumeWithFallback函式中。

FallbackHeadersGatewayFilterFactory 熔断操作引发原因,宣告熔断异常型别,这在2.0.2之后的版本才支援。下图是官方最新版本文件说明中的截图,从说明中可以明白可以为不同异常配置不同的熔断路径,并标明异常型别。

PrefixPathGatewayFilterFactory 为请求的Uri新增一个字首路径,Restful Api经常以/api作为规范开头路径,则可以为客户端自动新增此规范,客户端则无需遵循此规范。PreserveHostHeaderGatewayFilterFactory 此过滤器没有引数,配置一个宣告属性即可,标明时候对请求Host进行验证,验证操作不发生于此过滤器中。

RedirectToGatewayFilterFactory 将配置所指定的域名请求重定向至所配置地址,并设定当前请求状态码为3XX。

RemoveRequestHeaderGatewayFilterFactory 这个与AddRequestHeaderGatewayFilterFactory呼应,只不过是删除request header中指定的header引数。RemoveResponseHeaderGatewayFilterFactory 与AddResponseHeaderGatewayFilterFactory呼应,删除response header中的指定引数。RequestHeaderToRequestUriGatewayFilterFactory 需要一个NameConfig引数(需要通过此引数在header中获取header属性值),只需要在配置档案中宣告开启,前提需要在request header中存在配置引数名的header属性存在;将当前请求的Uri替换成header中指定的Uri;注意这个工厂类的apply方法在父类中,自身只重写了determineRequestUri方法。RequestRateLimiterGatewayFilterFactory 流量限制过滤器,这个实现比较复杂的,基于redis + lua script实现的。

RequestSizeGatewayFilterFactory 限制请求体的大小,预设是5M;这个工厂类也是在2.0.2版本之后加入的。RetryGatewayFilterFactory 重试机制,可以配置指定重试的错误级别和方法型别。

RewritePathGatewayFilterFactory 重写请求的Uri,配置引数需要遵循正则表示式的规则。RewriteResponseHeaderGatewayFilterFactory 重写response中指定的header引数。SaveSessionGatewayFilterFactory 快取当前请求的Session,呼叫Web Server的方法快取,这里是Http Server的动作。SecureHeadersGatewayFilterFactory 为request header新增若干属性。SetPathGatewayFilterFactory 替换Path断言中的路径。SetRequestHeaderGatewayFilterFactory 与AddRequestHeaderGatewayFilterFactory功能相似,一个是全域性替换,一个是追加,某种情况下作用一致。SetResponseHeaderGatewayFilterFactory 与AddResponseHeaderGatewayFilterFactory功能相似,这里是设定,而AddResponseHeaderGatewayFilterFactory中是往已存在的header属性集合中新增,当header属性为空时两个作用是一样的。SetStatusGatewayFilterFactory 根据配置设定请求的返回状态。StripPrefixGatewayFilterFactory 这个与PrefixPathGatewayFilterFactory类似,只不过是按照配置截掉Uri的部分path。Summary

gateway的过滤器工厂类不同于GlobalFilter,所有的过滤器工厂实现类均在GatewayAutoConfiguration中进行注入BeanFactory中,根据每个路由配置的具体

gatewayFilter从beanFactory中获取工厂类进行配置并注入对应的RouteLocator;GatewayFilter和GlobalFilter一样最终被封装成OrderedGatewayFilter,然后为每个请求分配一个定制的GatewayFilterChain。

2020-02-04 01:10:00

相关文章