zoukankan      html  css  js  c++  java
  • Spring Cloud Gateway(二)

    5. 网关过滤器工厂

    路由过滤器允许以某种方式修改传入的HTTP请求或者传出的HTTP响应。路由过滤器作用于特定的路由。Spring Cloud Gateway包括许多内置的网关过滤器工厂。

    关于如何使用以下过滤器的更详细示例,请查看unit tests

    5.1 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的header头。

    5.2 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的查询字符串。

    5.3 AddResponseHeader GatewayFilter Factory(添加响应头)

    AddResponseHeader GatewayFilter Factory接收一个名称和值参数。

    application.yml.

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

    在匹配的情况下,会为下游响应添加X-Response-Foo:Bar的header头。

    5.4 Hystrix GatewayFilter Factory

    Hystrix是Netflix的一个库,实现了断路器模式。Hystrix网关过滤器允许你引入断路器到你的网关路由,

    保护您的服务免于级联故障,并允许您在发生下游故障时提供备用响应。

    要在项目中启用Hystrix网关过滤器,请添加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 uri的方式。如果调用fallback,请求将被转发到匹配URI的controller。

    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 fallback时将请求转发到/incaseoffailureusethis这个URI。注意,这个示例还通过目标URI上的lb前缀演示了(可选的)Spring Cloud Netflix Ribbon负载平衡。

    Hystrix设置(例如超时)可以使用全局默认值配置,也可以使用在Hystrix wiki上解释的应用程序属性按路由进行配置。

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

    application.yml.

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

    5.5 PrefixPath GatewayFilter Factory(前缀路径)

    PrefixPath GatewayFilter Factory接收单一的参数:

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

    这将为所有匹配请求的路径加上/mypath前缀。因此,对/hello的请求将被发送到/mypath/hello。

    application.yml.

  • 相关阅读:
    git小乌龟的使用
    C语言笔记(二):数据类型(基本数据类型)、类型转换(自动转换、强制转换)
    Edge Chromium 中如何始终允许运行 Flash 内容
    【DTOJ】2704:数字互换
    【DTOJ】2703:两个数的余数和商
    【DTOJ】1001:长方形周长和面积
    Linux_simpl shell-利用Shell脚本for循环输出系统中的用户及其Shell
    Linux_Centos7安装VNC实现远程桌面
    Linux_crontab参数表示的意思
    Docke部署nginx并配置nginx
  • 原文地址:https://www.cnblogs.com/htuao/p/9767302.html
Copyright © 2011-2022 走看看