Spring Cloud Gateway(译)(Part 3)

7. Global Filters

GlobalFilter 接口具有与GatewayFilter相同的签名. 这些特殊 filters有条件的应用与所有路由.

此接口及其用法可能会在将来的里程碑版本中更改.

7.1. Combined Global Filter and GatewayFilter Ordering

当请求与路由匹配时, filtering web handler 将所有的GlobalFilter 和特定于路由的GatewayFilter 添加到filter链中. 该组合的过滤器链由 org.springframework.core.Ordered 接口排序, 你可以通过重写getOrder() 方法来设置.

由于 Spring Cloud Gateway 有“pre” 和 “post” 两个阶段,filter 执行逻辑(参考 How it Works), 因此具有最高优先级的filter 在 “pre”-phase 是第一个,在 “post”-phase 是最后一个.

下面是一个 filter chain 的示例:

Example 56. ExampleConfiguration.java

@Bean
public GlobalFilter customFilter() {
    return new CustomGlobalFilter();
}

public class CustomGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("custom global filter");
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -1;
    }
}

7.2. Forward Routing Filter

ForwardRoutingFilterServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR 的 exchange 属性中查找URI. 如果 URL 有 forward scheme (例如 forward:///localendpoint), 则使用 Spring DispatcherHandler 处理请求. 请求的 URL 路径会被 forward URL 覆盖. 未经修改的原始URL会被添加到ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR 属性的列表中.

7.3. The LoadBalancerClient Filter

LoadBalancerClientFilter在exchange中名为ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR 的 属性中查找URI. 如果 URL 包含 scheme lb (例如 lb://myservice), 它使用 Spring Cloud LoadBalancerClient 将名称(本例中为myservice)解析为实际的主机和端口,并替换同一属性中的URI. 未经修改的原始URL会被添加到ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR 属性的列表中. filter 还会在 ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR 属性中查找其是否等于lb. 如果是, 则应用相同的规则. 下面是LoadBalancerClientFilter`的配置示例:

Example 57. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: myRoute
        uri: lb://service
        predicates:
        - Path=/service/**

默认情况下, 当LoadBalancer找不到ServiceInstance 时, 将返回 503 . 你可以通过设置spring.cloud.gateway.loadbalancer.use404=true将Gateway配置为返回 404 .

LoadBalancerClientFilter 使用阻塞的 LoadBalancerClient 引擎. 我们建议你使用 ReactiveLoadBalancerClientFilter 替代. 你可以通过将spring.cloud.loadbalancer.ribbon.enabledfalse 来切换成ReactiveLoadBalancerClientFilter .

7.4. The ReactiveLoadBalancerClientFilter

ReactiveLoadBalancerClientFilter 在 exchange中的名为 ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR属性中查找URI. 如果URL 是lb scheme (比如 lb://myservice), 则它使用 Spring Cloud ReactorLoadBalancer 来根据名称 (本示例中为myservice) 解析成实际的主机和端口,并替换URI中的相同属性. 未经修改的原始URL会被添加到 ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR 属性列表中. 该 filter还会在 ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR 属性中查找其是否等 lb. 如果有, 则适用相同的规则. 下面是ReactiveLoadBalancerClientFilter的配置示例:

Example 58. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: myRoute
        uri: lb://service
        predicates:
        - Path=/service/**

默认情况下, 当ReactorLoadBalancer找不到ServiceInstance 时, 将返回 503 . 你可以通过设置spring.cloud.gateway.loadbalancer.use404=true将Gateway配置为返回 404 .

7.5. The Netty Routing Filter

如果 exchange 中名 ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR 属性中的url的scheme是 http or https scheme. 则执行Netty routing filter, 并使用 Netty HttpClient 发出下游代理请求. 请求的响应会放在 exchange 中名为 ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR 属性中, 以便后面的filter做进一步的处理 . (还有一个实验性的 WebClientHttpRoutingFilter 执行相同的功能,但不需要Netty.)

7.6. The Netty Write Response Filter

如果NettyWriteResponseFilter 发现exchange的名为ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR属性中存在Netty HttpClientResponse 类型的实例,它在所有其他过滤器执行完成后运行,将代理响应写回到gateway客户端的响应中. (还有一个实验性的WebClientWriteResponseFilter 执行相同的功能 ,但不需要Netty.)

7.7. The RouteToRequestUrl Filter

如果exchange的ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR存在Route 对象, 则RouteToRequestUrlFilter 会执行. 它基于请求URI创建一个新URI,但使用 Route 对象的URL属性进行更新. 新的URI会更新 exchange 的ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR 属性中.

如果URI有scheme前缀, 例如lb:ws://serviceid, lb scheme 会从URI中截取出来放到 ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR属性中, 方便后面的filter chain使用.

7.8. The Websocket Routing Filter

如果exchange 的 ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR 属性中含有 ws or wss scheme, websocket routing rilter 则会执行. 它使用Spring WebSocket 基础模块来向下游转发websocket请求.

你可是在URI前面加上lb前缀来实现 websockets 负载均衡, 例如 lb:ws://serviceid.

下面是websocket routing filter的配置示例:

Example 59. application.yml

spring:
  cloud:
    gateway:
      routes:
      # SockJS route
      - id: websocket_sockjs_route
        uri: http://localhost:3001
        predicates:
        - Path=/websocket/info/**
      # Normal Websocket route
      - id: websocket_route
        uri: ws://localhost:3001
        predicates:
        - Path=/websocket/**

7.9. The Gateway Metrics Filter

若想启用 gateway metrics , 需要添加spring-boot-starter-actuator 作为项目依赖项.然后,默认情况下, gateway metrics filter 会自动启动,若想关闭,只要将 spring.cloud.gateway.metrics.enabled设置为 false. 该filter会添加一个名为a gateway.requests 时间度量, 包含如下tags:

  • routeId: 路由 ID.
  • routeUri: API路由到的URI.
  • outcome: 结果, 按 HttpStatus.Series 分类.
  • status: 返回给客户端HTTP status .
  • httpStatusCode: 返回给客户端HTTP status code.
  • httpMethod: 请求的HTTP 方法.

然后可以从 /actuator/metrics/gateway.requests 中抓取这些指标,并且可以轻松地将它们与Prometheus集成以创建 Grafana dashboard.

若要启用 prometheus endpoint, 添加micrometer-registry-prometheus作为项目依赖项.

7.10. Marking An Exchange As Routed

filter方法有一个参数ServerWebExchange, 在路由转发到下游的时候通过设置exchange 的gatewayAlreadyRouted 属性标识为"已路由". 一旦请求被标识为"已路由", 其他路由filters就不会再次去匹配路由请求, 实质上会跳过该filter. 你可以使用多种便捷的方法将exchange标记为"已路由",或者检查exchange是否已经路由.

  • ServerWebExchangeUtils.isAlreadyRouted 接收一个ServerWebExchange 实例参数并检查是否“routed”.
  • ServerWebExchangeUtils.setAlreadyRouted接收一个 ServerWebExchange 实例参数并并将它设置为 “routed”.

8. HttpHeadersFilters

HttpHeadersFilters 在将请求发送到下游之前先应用于请求, 例如NettyRoutingFilter.

8.1. Forwarded Headers Filter

Forwarded Headers Filter 创建一个 Forwarded header 然后发送到下游服务. 它将当前请求的 Host header, scheme 和 port 添加到现有的 Forwarded header.

8.2. RemoveHopByHop Headers Filter

RemoveHopByHop Headers Filter 从转发的请求中删除headers. 默认被删除的 headers 列表来自 IETF.

The default removed headers are:

  • Connection
  • Keep-Alive
  • Proxy-Authenticate
  • Proxy-Authorization
  • TE
  • Trailer
  • Transfer-Encoding
  • Upgrade

要更改此设置,设置 spring.cloud.gateway.filter.remove-non-proxy-headers.headers 属性为要删除的header names 列表.

8.3. XForwarded Headers Filter

XForwarded Headers Filter 创建各种X-Forwarded-* headers 以发送到下游服务. 它使用当前请求的 Host header, scheme, port 和 path 来创建各种头.

可以通过以下boolean属性(默认为true)创建创建独立的headers:

  • spring.cloud.gateway.x-forwarded.for.enabled
  • spring.cloud.gateway.x-forwarded.host.enabled
  • spring.cloud.gateway.x-forwarded.port.enabled
  • spring.cloud.gateway.x-forwarded.proto.enabled
  • spring.cloud.gateway.x-forwarded.prefix.enabled

可以通过以下boolean属性(默认为true)控制追加多个headers :

  • spring.cloud.gateway.x-forwarded.for.append
  • spring.cloud.gateway.x-forwarded.host.append
  • spring.cloud.gateway.x-forwarded.port.append
  • spring.cloud.gateway.x-forwarded.proto.append
  • spring.cloud.gateway.x-forwarded.prefix.append

9. TLS and SSL

网关可以通过Spring server configuration来侦听HTTPS上的请求 . 如下所示:

Example 60. application.yml

server:
  ssl:
    enabled: true
    key-alias: scg
    key-store-password: scg1234
    key-store: classpath:scg-keystore.p12
    key-store-type: PKCS12

网关路由可以路由到HTTP和HTTPS后端. 如果路由到HTTPS后端, 你可以将网关配置为信任所有具有证书的下游服务.如下所示:

Example 61. application.yml

spring:
  cloud:
    gateway:
      httpclient:
        ssl:
          useInsecureTrustManager: true

不建议在生产环境使用不安全的信任管理器。对于生产部署,可以使用一组已知证书配置网关,这些证书可以通过以下方式进行配置. 如下所示:

Example 62. application.yml

spring:
  cloud:
    gateway:
      httpclient:
        ssl:
          trustedX509Certificates:
          - cert1.pem
          - cert2.pem

如果Spring Cloud Gateway未配置受信任证书,则使用默认信任库(可以使用系统属性javax.net.ssl.trustStore覆盖)。

9.1. TLS Handshake

网关维护一个用于路由到后端的client池。当通过HTTPS通信时,客户端启动一个TLS握手,其中可能会有很多超时。这些超时可以如下配置(显示默认值):

Example 63. application.yml

spring:
  cloud:
    gateway:
      httpclient:
        ssl:
          handshake-timeout-millis: 10000
          close-notify-flush-timeout-millis: 3000
          close-notify-read-timeout-millis: 0

10. Configuration

Spring Cloud Gateway 的配置由RouteDefinitionLocator 实例的集合驱动. 下面显示 RouteDefinitionLocator 借口:

Example 64. RouteDefinitionLocator.java

public interface RouteDefinitionLocator {
    Flux getRouteDefinitions();
}

默认情况下 , PropertiesRouteDefinitionLocator 使用Spring Boot的 @ConfigurationProperties` 机制加载属性.

较早的配置示例均使用shortcut notation,shortcut notation使用位置参数而不是命名参数。

以下两个示例是等效的:

Example 65. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: setstatus_route
        uri: https://example.org
        filters:
        - name: SetStatus
          args:
            status: 401
      - id: setstatusshortcut_route
        uri: https://example.org
        filters:
        - SetStatus=401

对于网关的大部分用法,配置文件方式是够用的,但一些生产用例更建议从外部源(如数据库)加载配置. 未来的里程碑版本将有基于Spring Data Repositories(例如 Redis, MongoDB, and Cassandra) 的RouteDefinitionLocator实现 .

11. Route Metadata Configuration

你可以通过使用metadata为每一个路由配置额外的参数, 如下所示:

Example 66. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: route_with_metadata
        uri: https://example.org
        metadata:
          optionName: "OptionValue"
          compositeObject:
            name: "value"
          iAmNumber: 1

你可以从exchange中获取所有的metadata 属性, 如下所示:

Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
// get all metadata properties
route.getMetadata();
// get a single metadata property
route.getMetadata(someKey);

12. Http timeouts configuration

可以为所有路由配置Http超时(响应和连接),并为每个特定路由覆盖Http超时。

12.1. Global timeouts

配置全局 http 超时:
connect-timeout 必须是毫秒为单位.
response-timeout 必须是 java.time.Duration 对象

全局 http 超时示例

spring:
  cloud:
    gateway:
      httpclient:
        connect-timeout: 1000
        response-timeout: 5s

12.2. Per-route timeouts

配置单个路由的 timeouts:
connect-timeout 必须以毫秒为单位
response-timeout 必须以毫秒为单位

per-route http timeouts configuration via configuration

      - id: per_route_timeouts
        uri: https://example.org
        predicates:
          - name: Path
            args:
              pattern: /delay/{timeout}
        metadata:
          response-timeout: 200
          connect-timeout: 200

per-route timeouts configuration using Java DSL

import static org.springframework.cloud.gateway.support.RouteMetadataUtils.CONNECT_TIMEOUT_ATTR;
import static org.springframework.cloud.gateway.support.RouteMetadataUtils.RESPONSE_TIMEOUT_ATTR;

      @Bean
      public RouteLocator customRouteLocator(RouteLocatorBuilder routeBuilder){
         return routeBuilder.routes()
               .route("test1", r -> {
                  return r.host("*.somehost.org").and().path("/somepath")
                        .filters(f -> f.addRequestHeader("header1", "header-value-1"))
                        .uri("http://someuri")
                        .metadata(RESPONSE_TIMEOUT_ATTR, 200)
                        .metadata(CONNECT_TIMEOUT_ATTR, 200);
               })
               .build();
      }

12.3. Fluent Java Routes API

为了可以更简单在Java中配置,在 RouteLocatorBuilder bean 中定义了一个 fluent API. 如下所示:

Example 67. GatewaySampleApplication.java

// static imports from GatewayFilters and RoutePredicates
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) {
    return builder.routes()
            .route(r -> r.host("**.abc.org").and().path("/image/png")
                .filters(f ->
                        f.addResponseHeader("X-TestHeader", "foobar"))
                .uri("http://httpbin.org:80")
            )
            .route(r -> r.path("/image/webp")
                .filters(f ->
                        f.addResponseHeader("X-AnotherHeader", "baz"))
                .uri("http://httpbin.org:80")
                .metadata("key", "value")
            )
            .route(r -> r.order(-1)
                .host("**.throttle.org").and().path("/get")
                .filters(f -> f.filter(throttle.apply(1,
                        1,
                        10,
                        TimeUnit.SECONDS)))
                .uri("http://httpbin.org:80")
                .metadata("key", "value")
            )
            .build();
}

这种样式还允许使用更多的自定义断言. 由 RouteDefinitionLocator beans定义的断言使用逻辑 and组合. 通过使用fluent Java API, 可以在Predicate 类上使用 and(), or(), and negate() 运算符.

12.4. The DiscoveryClient Route Definition Locator

可以将网关配置为基于使用兼容 DiscoveryClient 注册中心注册的服务来创建路由.

要启用此功能,请设置 spring.cloud.gateway.discovery.locator.enabled=true ,并确保DiscoveryClient 实现位于classpath上并已启用如 Netflix Eureka, Consul, or Zookeeper) .

12.4.1. Configuring Predicates and Filters For DiscoveryClient Routes

默认情况下,网关为通过 DiscoveryClient创建的路由定义单个断言和过滤器.

默认断言是使用 /serviceId/**定义的path断言, 其中serviceIdDiscoveryClient中服务的ID.

默认过滤器是使用正则表达式 /serviceId/(?.*) 和替换的/${remaining}进行重写. 只是在请求被发送到下游之前从路径中截取掉 service ID .

可以通过设置spring.cloud.gateway.discovery.locator.predicates[x] and spring.cloud.gateway.discovery.locator.filters[y]来实现自定义DiscoveryClient 路由使用的断言and/or过滤器. 当你这样做时,如果你想实现相同的功能, 需要确保包括前面显示的默认predicates和filters. 如下所示:

Example 68. application.properties

spring.cloud.gateway.discovery.locator.predicates[0].name: Path
spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/'+serviceId+'/**'"
spring.cloud.gateway.discovery.locator.predicates[1].name: Host
spring.cloud.gateway.discovery.locator.predicates[1].args[pattern]: "'**.foo.com'"
spring.cloud.gateway.discovery.locator.filters[0].name: Hystrix
spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId
spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath
spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/(?.*)'"
spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"

13. Reactor Netty Access Logs

要想开启 Reactor Netty access logs, 需要设置-Dreactor.netty.http.server.accessLogEnabled=true.

必须是Java System Property而不是Spring Boot property

logging 模块也可以通过配置单独输出一个access log文件,下面是logback的配置例子:

Example 69. logback.xml

    
        access_log.log
        
            %msg%n
        
    
    
        
    

    
        
    

14. CORS Configuration

我们可以通过配置网关来控制CORS行为. “global” CORS 配置是URL 模式到 Spring Framework CorsConfiguration的映射,如下所示:

Example 70. application.yml

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "https://docs.spring.io"
            allowedMethods:
            - GET

在前面的示例中,对于所有GET请求的路径,允许来自docs.spring.io的请求中的CORS请求。

要为没有被gateway route predicate处理的请求提供相同的CORS 配置, 需要设置 spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping 属性为 true. 当你尝试支持CORS预检请求并且您的路由谓词未评估为true时, 这很有用,因为 HTTP 方法是 options.

15. Actuator API

/gateway actuator endpoint 允许你监视Spring Cloud Gateway应用并与之交互. 为了可远程访问,必须在应用程序配置文件中 暴露HTTP or JMX . 如下所示:

Example 71. application.properties

management.endpoint.gateway.enabled=true # default value
management.endpoints.web.exposure.include=gateway

15.1. Verbose Actuator Format

Spring Cloud Gateway已经支持一种新的,更详细的格式. 它为每个路由添加了更多详细信息, 您可以查看与每个路由关联的predicates 和 filters 以及任何可用的配置. 以下是/actuator/gateway/routes配置示例:

[
  {
    "predicate": "(Hosts: [**.addrequestheader.org] && Paths: [/headers], match trailing slash: true)",
    "route_id": "add_request_header_test",
    "filters": [
      "[[AddResponseHeader X-Response-Default-Foo = 'Default-Bar'], order = 1]",
      "[[AddRequestHeader X-Request-Foo = 'Bar'], order = 1]",
      "[[PrefixPath prefix = '/httpbin'], order = 2]"
    ],
    "uri": "lb://testservice",
    "order": 0
  }
]

默认情况下启用此功能。 要禁用它,请设置以下属性:

Example 72. application.properties

spring.cloud.gateway.actuator.verbose.enabled=false

在未来的版本中,它将默认为 true .

15.2. Retrieving Route Filters

本节详细介绍如何检索 route filters, 包括:

  • Global Filters
  • [gateway-route-filters]
15.2.1. Global Filters

要检索 应用于所有路由的 global filters , 发起一个get请求 /actuator/gateway/globalfilters. 返回的结果类似于以下内容 :

{
  "org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@77856cc5": 10100,
  "org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@4f6fd101": 10000,
  "org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@32d22650": -1,
  "org.springframework.cloud.gateway.filter.ForwardRoutingFilter@106459d9": 2147483647,
  "org.springframework.cloud.gateway.filter.NettyRoutingFilter@1fbd5e0": 2147483647,
  "org.springframework.cloud.gateway.filter.ForwardPathFilter@33a71d23": 0,
  "org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@135064ea": 2147483637,
  "org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@23c05889": 2147483646
}

返回结果包含已就绪的global filters的详细信息 . 对于每个 global filter, 过滤器对象都有一个字符串表示形式(例如, org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@77856cc5以及过 filter chain中的相应 order ) .

15.2.2. Route Filters

要检索应用于路由的 GatewayFilter factories , 发起一个get请求 /actuator/gateway/routefilters. 返回结果类似于以下内容 :

{
  "[AddRequestHeaderGatewayFilterFactory@570ed9c configClass = AbstractNameValueGatewayFilterFactory.NameValueConfig]": null,
  "[SecureHeadersGatewayFilterFactory@fceab5d configClass = Object]": null,
  "[SaveSessionGatewayFilterFactory@4449b273 configClass = Object]": null
}

响应包含应用于任何特定路由的GatewayFilter工厂的详细信息. 对于每个工厂,都有一个对应对象的字符串表示形式 (例如, [SecureHeadersGatewayFilterFactory@fceab5d configClass = Object]). 请注意, null 值是由于端点控制器的实现不完整所致,因为它试图设置对象在 filter chain中的顺序, 而该顺序不适用于GatewayFilter 工厂对象.

15.3. Refreshing the Route Cache

如果要清理路由的缓存,请POST请求 /actuator/gateway/refresh. 该请求将返回一个没有body的200返回码 .

15.4. Retrieving the Routes Defined in the Gateway

要检索网关中定义的路由,发送GET请求 /actuator/gateway/routes. 返回结果如下所示 :

[{
  "route_id": "first_route",
  "route_object": {
    "predicate": "org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$432/1736826640@1e9d7e7d",
    "filters": [
      "OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.PreserveHostHeaderGatewayFilterFactory$$Lambda$436/674480275@6631ef72, order=0}"
    ]
  },
  "order": 0
},
{
  "route_id": "second_route",
  "route_object": {
    "predicate": "org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$432/1736826640@cd8d298",
    "filters": []
  },
  "order": 0
}]

返回结果中包含网关中所有定义的路由信息,下面表格中描述了返回结果信息 :

Path Type Description
route_id String The route ID.
route_object.predicate Object The route predicate.
route_object.filters Array The GatewayFilter factories applied to the route.
order Number The route order.

15.5. Retrieving Information about a Particular Route

要获取单个路由的信息,发送GET请求 /actuator/gateway/routes/{id} ( 如 , /actuator/gateway/routes/first_route). 返回结果如下所示 :

{
  "id": "first_route",
  "predicates": [{
    "name": "Path",
    "args": {"_genkey_0":"/first"}
  }],
  "filters": [],
  "uri": "https://www.uri-destination.org",
  "order": 0
}]

下面表格中描述了返回结果信息 :

Path Type Description
id String The route ID.
predicates Array The collection of route predicates. Each item defines the name and the arguments of a given predicate.
filters Array The collection of filters applied to the route.
uri String The destination URI of the route.
order Number The route order.

15.6. Creating and Deleting a Particular Route

要创建一个路由,发送 POST 请求 /gateway/routes/{id_route_to_create} 参数为 JSON body ,具体route数据结构 (参见 Retrieving Information about a Particular Route).

要删除一个路由,发送 DELETE请求 /gateway/routes/{id_route_to_delete}.

15.7. Recap: The List of All endpoints

下表总结了 Spring Cloud Gateway actuator endpoints ( 注意,每个 endpoint 都需要/actuator/gateway 作为基本路径):

ID HTTP Method Description
globalfilters GET Displays the list of global filters applied to the routes.
routefilters GET Displays the list of GatewayFilter factories applied to a particular route.
refresh POST Clears the routes cache.
routes GET Displays the list of routes defined in the gateway.
routes/{id} GET Displays information about a particular route.
routes/{id} POST Adds a new route to the gateway.
routes/{id} DELETE Removes an existing route from the gateway.

你可能感兴趣的:(Spring Cloud Gateway(译)(Part 3))