环境:Springboot2.4.12 + Spring Cloud Gateway2.2.9.RELEASE
http超时(响应和连接)可以为所有路由配置,并覆盖每个特定的路由。
全局超时配置
使用实例设置http全局超时。
connect-timeout必须以毫秒为单位指定。
response-timeout必须指定为java.time.Duration
示例:
spring:cloud:gateway:httpclient:: 2000: 5s
每个路由超时配置
设置路由超时。
connect-timeout必须以毫秒为单位指定。
response-timeout必须以毫秒为单位指定。
spring:cloud:gateway:enabled: truediscovery:locator:enabled: truelowerCaseServiceId: true:StripPrefix=1routes:id: R003uri: http://localhost:8082predicates:Path=/api-x/demos/date3metadata:#局部超时设置: 10000: 50000
使用Java DSL配置
import static org.springframework.cloud.gateway.support.RouteMetadataUtils.CONNECT_TIMEOUT_ATTR;import static org.springframework.cloud.gateway.support.RouteMetadataUtils.RESPONSE_TIMEOUT_ATTR;@Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder routeBuilder){return routeBuilder.routes().route("test1", r -> {return r.host("*.pack.com").and().path("/products").filters(f -> f.addRequestHeader("api-key", "abc")).uri("http://someuri").metadata(RESPONSE_TIMEOUT_ATTR, 2000).metadata(CONNECT_TIMEOUT_ATTR, 2000);}).build();}
每个路由的response-timeout为负值将禁用全局响应超时值。
id: per_route_timeoutsuri: https://pack.compredicates:name: Pathargs:pattern: /delay/{timeout}metadata:: -1
流式路由配置
为了在Java中进行简单的配置,RouteLocatorBuilder bean包含一个流畅的API。其工作原理如下列代码清单所示:
@Beanpublic 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,可以在Predicate类上使用and()、or()和negate()操作符。
DiscoveryClient路由定位
你可以将网关配置为基于在DiscoveryClient兼容服务注册表中注册的服务创建路由,直接通过服务名就能访问。
要启用此功能,请将spring.cloud.gateway.discovery.locator.enabled设置为true,并确保在类路径上启用了DiscoveryClient实现(如Netflix Eureka、Consul、Zookeeper或者Nacos)。
默认情况下,网关为DiscoveryClient创建的路由定义一个谓词和过滤器。
默认谓词是用模式/serviceId/**定义的路径谓词,其中serviceId是来自DiscoveryClient的服务的ID。
默认过滤器是一个正则表达式/serviceId/?(?<remaining>.*)和替换/${remaining}的重写路径过滤器。这将在请求发送到下游之前从路径中删除服务ID。
如果想定制DiscoveryClient路由使用的谓词或过滤器,可以设置spring.cloud.gateway.discovery.locator.predicate[x]和spring.cloud.gateway.discovery.locator.filters[y]。这样做时,如果你想保留默认的谓词和过滤器,就需要确保包含前面展示的默认谓词和过滤器。如下面的例子所示:
spring.cloud.gateway.discovery.locator.predicates[0].name: Pathspring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/'+serviceId+'/**'"spring.cloud.gateway.discovery.locator.predicates[1].name: Hostspring.cloud.gateway.discovery.locator.predicates[1].args[pattern]: "'**.foo.com'"spring.cloud.gateway.discovery.locator.filters[0].name: CircuitBreakerspring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceIdspring.cloud.gateway.discovery.locator.filters[1].name: RewritePathspring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/?(?<remaining>.*)'"spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"
完毕!!!
求关注
关注公众《SpringBoot Cloud实战案例锦集》 送《SpringBoot实战案例》



