Spring Cloud GatewayFilter工厂(一)

路由过滤器允许以某种方式修改传入的HTTP请求或传出的HTTP响应。路径过滤器的范围限定为特定路径。Spring Cloud Gateway包含许多内置的GatewayFilter工厂。

注意有关如何使用以下任何过滤器的更详细示例,请查看单元测试。

AddRequestHeader GatewayFilter Factory

AddRequestHeader GatewayFilter Factory采用名称和值参数。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: http://example.org
        filters:
        - AddRequestHeader=X-Request-Foo, Bar

这将为所有匹配请求的下游请求标头添加X-Request-Foo:Bar标头。

AddRequestParameter GatewayFilter Factory 

AddRequestParameter GatewayFilter Factory采用名称和值参数。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: add_request_parameter_route
        uri: http://example.org
        filters:
        - AddRequestParameter=foo, bar

 这会将foo = bar添加到下游请求的所有匹配请求的查询字符串中。

AddResponseHeader GatewayFilter Factory

AddResponseHeader GatewayFilter Factory采用名称和值参数。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: add_response_header_route
        uri: http://example.org
        filters:
        - AddResponseHeader=X-Response-Foo, Bar

这会将X-Response-Foo:Bar标头添加到所有匹配请求的下游响应标头中。

扫描二维码关注公众号,回复: 5618310 查看本文章

DedupeResponseHeader GatewayFilter Factory 

DedupeResponseHeader GatewayFilter Factory采用名称参数和可选策略参数。name可以包含标题名称列表,空格分隔。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: dedupe_response_header_route
        uri: http://example.org
        filters:
        - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin

在网关CORS逻辑和下游添加它们的情况下,这将删除Access-Control-Allow-Credentials和Access-Control-Allow-Origin响应头的重复值。

DedupeResponseHeader过滤器还接受可选的策略参数。接受的值为RETAIN_FIRST(默认值),RETAIN_LAST和RETAIN_UNIQUE。

Hystrix GatewayFilter Factory

Hystrix是Netflix的一个库,它实现了断路器模式。Hystrix GatewayFilter允许您将断路器引入网关路由,保护您的服务免受级联故障的影响,并允许您在下游故障时提供回退响应。

要在项目中启用Hystrix GatewayFilters,请在Spring Cloud Netflix上添加对spring-cloud-starter-netflix-hystrix的依赖关系

Hystrix GatewayFilter Factory需要单个名称参数,该参数是HystrixCommand的名称。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: hystrix_route
        uri: http://example.org
        filters:
        - Hystrix=myCommandName

 这将使用命令名myCommandName将剩余的过滤器包装在HystrixCommand中。

Hystrix过滤器还可以接受可选的fallbackUri参数。目前,仅支持forward:schemed URIs。如果调用了回退,则请求将被转发到与URI匹配的控制器。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: hystrix_route
        uri: lb://backing-service:8088
        predicates:
        - Path=/consumingserviceendpoint
        filters:
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/incaseoffailureusethis
        - RewritePath=/consumingserviceendpoint, /backingserviceendpoint

当调用Hystrix后备时,这将转发到/ incaseoffailureuset这个URI。请注意,此示例还通过目标URI上的lb前缀演示(可选)Spring Cloud Netflix Ribbon负载平衡。

主要方案是将fallbackUri用于网关应用程序内的内部控制器或处理程序。但是,也可以将请求重新路由到外部应用程序中的控制器或处理程序,如下所示: 

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: ingredients
        uri: lb://ingredients
        predicates:
        - Path=//ingredients/**
        filters:
        - name: Hystrix
          args:
            name: fetchIngredients
            fallbackUri: forward:/fallback
      - id: ingredients-fallback
        uri: http://localhost:9994
        predicates:
        - Path=/fallback

在此示例中,网关应用程序中没有回退端点或处理程序,但是,在http:// localhost:9994下注册的另一个应用程序中有一个

如果请求被转发到回退,Hystrix网关过滤器还会提供导致它的Throwable。它作为ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR属性添加到ServerWebExchange中,可以在处理网关应用程序中的回退时使用该属性。

对于外部控制器/处理程序方案,可以添加包含异常详细信息的标头。

您可以在FallbackHeaders GatewayFilter Factory部分中找到有关它的更多信息。Hystrix设置(例如超时)可以使用全局默认值配置,也可以使用应用程序属性逐个路径配置,如Hystrix wiki中所述。

要为上面的示例路由设置5秒超时,将使用以下配置: 

application.yml

hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 5000

FallbackHeaders GatewayFilter Factory

FallbackHeaders工厂允许您在转发到外部应用程序中的fallbackUri的请求的标头中添加Hystrix执行异常详细信息,如下所示: 

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: ingredients
        uri: lb://ingredients
        predicates:
        - Path=//ingredients/**
        filters:
        - name: Hystrix
          args:
            name: fetchIngredients
            fallbackUri: forward:/fallback
      - id: ingredients-fallback
        uri: http://localhost:9994
        predicates:
        - Path=/fallback
        filters:
        - name: FallbackHeaders
          args:
            executionExceptionTypeHeaderName: Test-Header

在此示例中,在运行HystrixCommand时发生执行异常后,请求将转发到localhost:9994上运行的应用程序中的回退端点或处理程序。

具有异常类型,消息和-if available- root的标头会导致异常类型和消息由FallbackHeaders过滤器添加到该请求中。 

通过设置下面列出的参数的值及其默认值,可以在配置中覆盖标头的名称:

  • executionExceptionTypeHeaderName(“Execution-Exception-Type”)
  • executionExceptionMessageHeaderName(“Execution-Exception-Message”)
  • rootCauseExceptionTypeHeaderName(“Root-Cause-Exception-Type”)
  • rootCauseExceptionMessageHeaderName(“Root-Cause-Exception-Message”)

您可以在Hystrix GatewayFilter Factory部分中找到有关Hystrix如何与Gateway配合使用的更多信息。

猜你喜欢

转载自blog.csdn.net/u013702678/article/details/88713542