有没有办法为Spring云网关中的每个路由设置不同的超时值?例如/路线1-30/路线2-20s
是的,我们可以通过为不同的路由定义不同的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
根据Spring Cloud Gateway文档,这要简单得多:
https://cloud.spring.io/spring-cloud-gateway/reference/html/#per-路由超时
因此,您可以为每条路由配置连接和读取超时,如下所示:
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: 200
connect-timeout: 200