1. 首页
  2. >
  3. 技术专题
  4. >
  5. SpringCloud

SpringCloud系列Gateway:过滤器总结

今天带大家看一下Spring Cloud Gateway的过滤器

过滤器分类

Spring Cloud Gateway根据作用范围划分为GatewayFilter和GlobalFilter,二者的区别如下

  • GatewayFilter:网关过滤器,需要通过spring.cloud.routes.filters配置在具体的路由下,只作用在当前特定路由上,也可以通过配置spring.cloud.default-filters让它作用于全局路由上。
  • GlobalFilter:全局过滤器,不需要再配置文件中配置,作用在所有的路由上,最终通过GatewayFilterAdapter包装成GatewayFilterChain能够识别的过滤器。

网关过滤器

网关过滤器用于拦截并链式处理web请求,可以实现横切与应用无关的需求,比如:安全、监控/埋点、限流访问超时设置、对请求和响应做出修改。Spring Cloud Gateway包含了许多内置的网关过滤器工厂,大概有30多个,具体用途大致可以分为以下几类

  1. Header过滤器
  2. Parameter请求参数过滤器
  3. Path路径过滤器
  4. Body请求(响应)体过滤器
  5. Status状态过滤器
  6. Session会话过滤器
  7. Redirect重定向过滤器
  8. Retry重试过滤器
  9. RateLimiter限流过滤器
  10. Hystrix熔断过滤器。

如下图所示列出部分过滤器

SpringCloud系列Gateway:过滤器总结

接下来我们举例说明部分使用

Path路径过滤器

Path路径过滤器可以实现URL重写,通过重写URL可以实现隐藏实际路径提高安全性。

RewritePathGatewayFilterFactory

RewritePath网关过滤器工厂包括路径正则表达式参数和替换后的参数,使用正则表达式来灵活地重写请求路径。如下示例:

spring:   profiles: rewritePath   cloud:     gateway:       routes:         - id: product-service         # 路由ID 唯一           uri: http://localhost:8890  # 目标URL,路由到对应的微服务地址           predicates:             - Path=/api-gateway/**   # 匹配对应URL请求           filters:             - RewritePath=/api-gateway(?<segment>/?.*), $\{segment} # 路径重写, 如/api-gateway/product/重写为/product/1

将/api-gateway/**路径的所有请求重写为/**及去掉/api-gateway前缀发送给下游

  • 请求http://localhost:8892/api-gateway/product/1 会将路径设置为http://localhost:8890/product/1请求下游product-service服务

PrefixPathGatewayFilterFactory

PrefixPath网关过滤器工厂可以在路径前统一增加前缀重写请求路径。如下示例:

spring:   profiles: prefixPath   cloud:     gateway:       routes:         - id: product-service         # 路由ID 唯一           uri: http://localhost:8890  # 目标URL,路由到对应的微服务地址           predicates:             - Path=/**   # 匹配对应URL请求           filters:             - PrefixPath=/product # 在请求路径前统一增加product前缀,  如/1 重写为/product/1

将/product作为所有匹配请求的路径的前缀。因此,当请求/1将被重写为/product/1发送给下游。

  • 请求http://localhost:8892/1 会将路径设置为http://localhost:8890/product/1请求下游product-service服务

StripPrefixGatewayFilterFactory

StripPrefix网关过滤器工厂可以从请求中分割掉部分路径。

spring:   profiles: stripPrefix   cloud:     gateway:       routes:         - id: product-service         # 路由ID 唯一           uri: http://localhost:8890  # 目标URL,路由到对应的微服务地址           predicates:             - Path=/**   # 匹配对应URL请求           filters:             - StripPrefix=2 # 请求中分割掉前两部分路径,  如/abc/123/product/1 重写为/product/1

将请求中的前两个路径分割掉,因此,当请求/abc/123/product/1 重写为/product/1发动给下游。

  • 请求http://localhost:8892/abc/123/product/1 会将路径设置为http://localhost:8890/product/1请求下游product-service服务

SetPathGatewayFilterFactory

setPath网关过滤器工厂采用路径模板参数,它提供了一种通过允许模板化路径来操作请求路径的简单方法,使用Spring Framework中的URI模板,允许多个匹配段

 spring:   profiles: setPath   cloud:     gateway:       routes:         - id: product-service         # 路由ID 唯一           uri: http://localhost:8890  # 目标URL,路由到对应的微服务地址           predicates:             - Path=/api/{segment}   # 匹配对应URL请求,将匹配的请求追加到目标URI之后           filters:             - SetPath=/product/{segment} # 请求中分割掉部分路径,  /api/1 重写为/product/1

对于请求路径/api/1,这会将路径设置为/product/1发出下游请求之前的路径

  • 请求http://localhost:8892/api/1 会将路径设置为http://localhost:8890/product/1请求下游product-service服务

Parameter请求参数过滤器

AddRequestParameterGatewayFilterFactory

AddRequestParameter网关过滤器可以在请求中增加参数

spring:   profiles: addRequestParameter   cloud:     gateway:       routes:         - id: product-service         # 路由ID 唯一           uri: lb://product-service  # 目标URL,路由到对应的微服务地址           predicates:             - Path=/api/{segment}   # 匹配对应URL请求,将匹配的请求追加到目标URI之后           filters:             - SetPath=/product/{segment} # 请求中分割掉部分路径,  /api/1 重写为/product/1             - AddRequestParameter=flag, test  #在下游请求中增加flag参数值为test

修改下游product-service服务打印参数flag

 @RequestMapping("/{id}") public Product selectProductById(@PathVariable("id") Integer id, @RequestParam(value = "flag", required = false) String flag) {     log.info("flag的值为:{}", flag);     return service.selectProductById(id); }

请求http://localhost:8892/api/1 结果,flag结果为test

SpringCloud系列Gateway:过滤器总结

Status状态过滤器

SetStatusGatewayFilterFactory

修改响应状态码

 spring:   profiles: setStatus   cloud:     gateway:       routes:         - id: product-service         # 路由ID 唯一           uri: lb://product-service  # 目标URL,路由到对应的微服务地址           predicates:             - Path=/api/{segment}   # 匹配对应URL请求,将匹配的请求追加到目标URI之后           filters:             - SetPath=/product/{segment} # 请求中分割掉部分路径,  /api/1 重写为/product/1             - SetStatus=404  #任何响应的HTTP状态将被设置为404

无论任何请求统一响应状态码为404

全局过滤器

全局过滤器不需要在配置文件中配置,作用在所有的路由上,最终通过GatewayFilterAdapter包装成GatewayFilterChain可识别的过滤器,它的请求业务以及路由的URI转换为真实业务服务请求地址的核心过滤器,不需要配置系统初始化时加载,并作用在每个路由上。

全局过滤器主要包括以下几个,如下图所示:

SpringCloud系列Gateway:过滤器总结