12. Http超时配置
可以为所有路由配置 Http 超时(响应和连接),并为每个特定路由覆盖。
12.1. 全局超时
要配置全局 http 超时:
connect-timeout
必须以毫秒为单位指定。
response-timeout
必须指定为 java.time.Duration
spring:
cloud:
gateway:
httpclient:
connect-timeout: 1000
response-timeout: 5s
12.2. 每路由超时
要配置每路由超时:
connect-timeout
必须以毫秒为单位指定。
response-timeout
必须以毫秒为单位指定。
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: 200
connect-timeout: 200
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();
}
12.3. Fluent Java 路由 API
为了允许在 Java 中进行简单配置,请使用RouteLocatorBuilder
bean 包括一个流畅的 API。
以下列表显示了它的工作原理:
// static imports from GatewayFilters and RoutePredicates
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) {
return builder.routes()
.route(r -> r.host("**.abc.org").and().path("/image/png")
.filters(f ->
f.addResponseHeader("X-TestHeader", "foobar"))
.uri("http://httpbin.org:80")
)
.route(r -> r.path("/image/webp")
.filters(f ->
f.addResponseHeader("X-AnotherHeader", "baz"))
.uri("http://httpbin.org:80")
.metadata("key", "value")
)
.route(r -> r.order(-1)
.host("**.throttle.org").and().path("/get")
.filters(f -> f.filter(throttle.apply(1,
1,
10,
TimeUnit.SECONDS)))
.uri("http://httpbin.org:80")
.metadata("key", "value")
)
.build();
}
这种样式还允许更多自定义谓词断言。
定义的谓词由RouteDefinitionLocator
bean 使用 logical 组合and
.
通过使用流畅的 Java API,您可以使用and()
,or()
和negate()
运算符Predicate
类。
12.4.DiscoveryClient
路径定义定位器
您可以将网关配置为基于向DiscoveryClient
兼容的服务注册表。
要启用此功能,请将spring.cloud.gateway.discovery.locator.enabled=true
并确保DiscoveryClient
实现(例如 Netflix、Eureka、Consul 或 Zookeeper)位于类路径上并已启用。
12.4.1. 配置谓词和过滤器DiscoveryClient
路线
默认情况下,网关为使用DiscoveryClient
.
默认谓词是使用 pattern 定义的路径谓词/serviceId/**
哪里serviceId
是
服务的 ID 来自DiscoveryClient
.
默认过滤器是带有正则表达式的重写路径过滤器/serviceId/?(?<remaining>.*)
和替换/${remaining}
.
这会在请求向下游发送之前从路径中剥离服务 ID。
如果要自定义DiscoveryClient
路线, 设置spring.cloud.gateway.discovery.locator.predicates[x]
和spring.cloud.gateway.discovery.locator.filters[y]
.
这样做时,如果要保留该功能,则需要确保包含前面显示的默认谓词和过滤器。
以下示例显示了它的外观:
spring.cloud.gateway.discovery.locator.predicates[0].name: Path spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/'+serviceId+'/**'" spring.cloud.gateway.discovery.locator.predicates[1].name: Host spring.cloud.gateway.discovery.locator.predicates[1].args[pattern]: "'**.foo.com'" spring.cloud.gateway.discovery.locator.filters[0].name: CircuitBreaker spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/?(?<remaining>.*)'" spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"