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
© . This site is unofficial and not affiliated with VMware.