Spring Cloud Gateway 3 内置Filter

2021-05-13 03:27

阅读:555

标签:mil   response   thread   gateway   netflix   example   转发   最大   url   

Spring Cloud Gateway 内置Filter

Spring Cloud Gateway中内置了很多过滤器,实现类有二十多个;

分类几类:

AddRequestHeader

给请求加上一条header信息;

spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: https://example.org
        filters:
        - AddRequestHeader=X-Request-red, blue

AddRequestParameter

给请求加上Paramter参数

spring:
  cloud:
    gateway:
      routes:
      - id: add_request_parameter_route
        uri: https://example.org
        filters:
        - AddRequestParameter=key,value

RewritePath

Spring Cloud Gateway 的RewritePath可以替换Zuul的StripPrefix;
修改转发的路径

spring:
  cloud:
    gateway:
      routes:
      - id: rewritepath_route
        uri: https://example.org
        predicates:
        - Path=/red/**
        filters:
        - RewritePath=/red(?/?.*), $\{segment}

AddResponseHeader

对网关的响应添加Header

spring:
  cloud:
    gateway:
      routes:
      - id: add_response_header_route
        uri: https://example.org
        filters:
        - AddResponseHeader=X-Response-Red, Blue

StripPrefix

用于去除url的前缀

spring:
  application:
    name: sc-gateway-server
  cloud:
    gateway:
      discovery:
        locator:
          enabled: false
          lowerCaseServiceId: true
      routes:
      - id: service-hi
        uri: lb://SERVICE-HI
        predicates:
          - Path=/demo/**
          - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2021-01-21T17:42:47.789-07:00[America/Denver]
          - Header=key,value
          - Method=GET,POST
#          - Cookie=mycookie,mycookievalue
#          - After=2017-01-20T17:42:47.789-07:00[America/Denver]
#          - Before=2020-01-20T17:42:47.789-07:00[America/Denver]
        filters:
          - StripPrefix=1

PrefixPath

用于添加url前缀

spring:
  cloud:
    gateway:
      routes:
      - id: prefixpath_route
        uri: https://example.org
        filters:
        - PrefixPath=/mypath

Retry

请求出现异常是进行重试

参数:

  • retries:应尝试的重试次数。
  • statuses:应重试的HTTP状态代码,以表示org.springframework.http.HttpStatus。
  • methods:应该重试的HTTP方法,以表示org.springframework.http.HttpMethod。
  • series:要重试的一系列状态代码,使用表示org.springframework.http.HttpStatus.Series。
  • exceptions:应重试的引发异常的列表。
  • backoff:为重试配置的指数补偿。重试在的退避间隔后执行firstBackoff * (factor ^ n),其中n为迭代。如果maxBackoff已配置,则应用的最大退避限制为maxBackoff。如果basedOnPreviousValue为true,则使用计算退避prevBackoff * factor。
spring:
  cloud:
    gateway:
      routes:
      - id: retry_test
        uri: http://localhost:8080/flakey
        predicates:
        - Host=*.retry.com
        filters:
        - name: Retry
          args:
            retries: 3
            statuses: BAD_GATEWAY
            methods: GET,POST
            backoff:
              firstBackoff: 10ms
              maxBackoff: 50ms
              factor: 2
              basedOnPreviousValue: false

Hystryix

进行服务熔断 降级

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

Hystrix过滤器还可以接受可选fallbackUri参数。当前,仅forward:支持计划的URI。如果调用了后备,则请求将转发到与URI匹配的控制器。以下示例配置了这种后备:

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

/incaseoffailureusethis调用Hystrix后备时,它将转发到URI。请注意,此示例还演示了(可选)Spring Cloud Netflix Ribbon负载平衡(lb在目标URI上定义了前缀)。

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

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

要为前面显示的示例路由设置五秒钟的超时时间,可以使用以下配置:

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

Spring Cloud Gateway 3 内置Filter

标签:mil   response   thread   gateway   netflix   example   转发   最大   url   

原文地址:https://www.cnblogs.com/chenglc/p/13131926.html


评论


亲,登录后才可以留言!