如何在Spring Cloud Gateway中为每个路由设置超时?

时间:2018-07-23 10:46:12

标签: spring-cloud spring-cloud-gateway

在Spring Cloud Gateway中是否可以为每个路由设置不同的超时值? 例如 / route1-30s / route2-20秒

2 个答案:

答案 0 :(得分:0)

是的,我们可以通过为不同的路由定义不同的hystrix命令来执行相同的操作。请考虑以下示例,其中route_1的超时时间为15秒,因为此处hystrix使用的default命令的超时时间为15秒。

 # ===========================================
 # Timeout 15 seconds
  - id: route_1
    uri: ${test.uri}
    predicates:
    - Path=/timeout/**
    filters:
    - name: Hystrix
      args:
        name: default
        fallbackUri: forward:/hystrixfallback

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 15000

对于route_2,现在使用的hystrix命令为applicationTimeOut,超时时间为5秒。

# ===========================================
  # Timeout 5 seconds
  - id: route_2
    uri: ${test.uri}/count
    predicates:
    - Path=/count
    filters:
    - name: Hystrix
      args:
        name: applicationTimeOut
        fallbackUri: forward:/hystrixfallback

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

对于route_3,现在使用的hystrix命令为apiTimeOut,超时时间为2秒。

# ===========================================   
  # Timeout 2 seconds
    - id: route_3
    uri: ${test.uri}
    predicates:
    - Path=/event/**
    filters:
    - name: Hystrix
      args:
        name: apiTimeOut
        fallbackUri: forward:/hystrixfallback
hystrix.command.apiTimeOut.execution.isolation.thread.timeoutInMilliseconds: 2000

答案 1 :(得分:0)

根据Spring Cloud Gateway文档,这要简单得多:

https://cloud.spring.io/spring-cloud-gateway/reference/html/#per-route-timeouts

因此,您可以配置每个路由的连接和读取超时,如下所示:

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