Http 超時配置
Http 超時(響應和連線)可以針對所有路由進行配置,併為每個特定路由進行覆蓋。
全域性超時
配置全域性 http 超時
connect-timeout 必須以毫秒為單位指定。
response-timeout 必須指定為 java.time.Duration
全域性 http 超時示例
spring:
cloud:
gateway:
httpclient:
connect-timeout: 1000
response-timeout: 5s
按路由超時
配置按路由超時
connect-timeout 必須以毫秒為單位指定。
response-timeout 必須以毫秒為單位指定。
透過配置實現按路由 http 超時配置
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: 200
connect-timeout: 200
使用 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();
}
負值的按路由 response-timeout 將停用全域性 response-timeout 值。
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: -1