如何为 Spring Cloud Gateway 中的每个路由设置超时? [英] How to set timeout for each route in Spring Cloud Gateway?

查看:301
本文介绍了如何为 Spring Cloud Gateway 中的每个路由设置超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Spring cloud gateway中为每条路由设置不同的超时值?例如/route1 - 30 秒/route2 - 20 秒

Is there a way to set different timeout values for each route in Spring cloud gateway? eg /route1 - 30s /route2 - 20s

推荐答案

是的,我们可以通过为不同的路由定义不同的 hystrix 命令来做到这一点.考虑以下示例,其中 route_1 的超时为 15 秒,因为此处使用的 hystrix 命令 default 配置了 15 秒的超时.

Yes, We can do the same by defining different hystrix command for different routes. Consider the following example, where for route_1 the timeout is 15 seconds, as the hystrix command used here default is configured with a timeout of 15 seconds.

 # ===========================================
 # 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 秒.

Now for route_2 the the hystrix command used is applicationTimeOut with a timeout of 5 seconds.

# ===========================================
  # 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 秒.

Now for route_3 the the hystrix command used is apiTimeOut with a timeout of 2 seconds.

# ===========================================   
  # 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

这篇关于如何为 Spring Cloud Gateway 中的每个路由设置超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆