Gateway 配置
路由超时时间配置
路由超时配置可以为所有路由配置 HTTP 超时(响应和连接),并为每个特定路由覆盖 HTTP 超时。
全局路由的超时时间配置
配置全局 HTTP 超时(响应和连接),需要使用以下两个参数:
connect-timeout
:必须以毫秒为单位指定连接超时时间response-timeout
:必须指定为 java.time.Duration
1 2 3 4 5 6
| spring: cloud: gateway: httpclient: connect-timeout: 1000 response-timeout: 5s
|
每个路由的超时时间配置
可以通过路由的 metadata
以下两个参数配置每个路由的超时时间:
connect-timeout
:必须以毫秒为单位指定连接超时时间response-timeout
:必须以毫秒为单位指定响应超时时间
1 2 3 4 5 6 7 8 9
| - id: per_route_timeouts uri: https://example.org predicates: - name: Path args: pattern: /delay/{timeout} metadata: response-timeout: 1000 connect-timeout: 1000
|
使用 Java DSL 为每个路由配置超时时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 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, 1000) .metadata(CONNECT_TIMEOUT_ATTR, 1000); }) .build(); }
|