英文:
How to set a time out in spring cloud gateway?
问题
如何在Spring Cloud Gateway中设置time-out
?
routeLocatorBuilder.routes()
.route("test-api", r -> r.path("/api/")).uri(apiLb))
.route("test-doc", r -> r.path("/doc/")).uri(docLb));
有没有一种方法只为一个ROUTE
(doc
)设置超时?
英文:
How to set a time-out
in spring cloud gateway
?
routeLocatorBuilder.routes()
.route("test-api", r -> r.path("/api/**")).uri(apiLb))
.route("test-doc", r -> r.path("/doc/**")).uri(docLb));
Is there a way to set a time out for just one ROUTE
- doc
.
答案1
得分: 10
以下是翻译好的内容:
从Spring文档引用
设置全局超时时间
spring:
cloud:
gateway:
httpclient:
connect-timeout: 1000
response-timeout: 5s
针对每个路由的超时时间:
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: 200
connect-timeout: 200
在Java中添加以下Bean:
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();
}
@Bean
public ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory(
final CircuitBreakerRegistry circuitBreakerRegistry, final TimeLimiterRegistry timeLimiterRegistry) {
ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory = new
ReactiveResilience4JCircuitBreakerFactory(circuitBreakerRegistry, timeLimiterRegistry);
reactiveResilience4JCircuitBreakerFactory.configureDefault(id -> {
CircuitBreakerConfig circuitBreakerConfig = circuitBreakerRegistry.find(id).isPresent()
? circuitBreakerRegistry.find(id).get().getCircuitBreakerConfig()
: circuitBreakerRegistry.getDefaultConfig();
TimeLimiterConfig timeLimiterConfig = timeLimiterRegistry.find(id).isPresent()
? timeLimiterRegistry.find(id).get().getTimeLimiterConfig()
: timeLimiterRegistry.getDefaultConfig();
return new Resilience4JConfigBuilder(id).circuitBreakerConfig(circuitBreakerConfig)
.timeLimiterConfig(timeLimiterConfig).build();
});
return reactiveResilience4JCircuitBreakerFactory;
}
英文:
Referred from spring doc
To set global time-out
spring:
cloud:
gateway:
httpclient:
connect-timeout: 1000
response-timeout: 5s
Per-route timeouts:-
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: 200
connect-timeout: 200
In Java, add the below bean(s):
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();
}
@Bean
public ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory(
final CircuitBreakerRegistry circuitBreakerRegistry, final TimeLimiterRegistry timeLimiterRegistry) {
ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory = new
ReactiveResilience4JCircuitBreakerFactory(circuitBreakerRegistry, timeLimiterRegistry);
reactiveResilience4JCircuitBreakerFactory.configureDefault(id -> {
CircuitBreakerConfig circuitBreakerConfig = circuitBreakerRegistry.find(id).isPresent()
? circuitBreakerRegistry.find(id).get().getCircuitBreakerConfig()
: circuitBreakerRegistry.getDefaultConfig();
TimeLimiterConfig timeLimiterConfig = timeLimiterRegistry.find(id).isPresent()
? timeLimiterRegistry.find(id).get().getTimeLimiterConfig()
: timeLimiterRegistry.getDefaultConfig();
return new Resilience4JConfigBuilder(id).circuitBreakerConfig(circuitBreakerConfig)
.timeLimiterConfig(timeLimiterConfig).build();
});
return reactiveResilience4JCircuitBreakerFactory;
}
答案2
得分: 5
接受的答案在不使用R4J断路器或时间限制器时有效...但如果使用,上述设置将不足够,实际上将被R4J设置覆盖。这在这里和这里有讨论,截至撰写本文,当前的解决方法可以在这里找到...基本上,您需要编写一个自定义Bean,以便遵守配置设置:
@Bean
public ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory(final CircuitBreakerRegistry circuitBreakerRegistry, final TimeLimiterRegistry timeLimiterRegistry) {
ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory = new ReactiveResilience4JCircuitBreakerFactory();
reactiveResilience4JCircuitBreakerFactory.configureCircuitBreakerRegistry(circuitBreakerRegistry);
reactiveResilience4JCircuitBreakerFactory.configureDefault(id -> {
CircuitBreakerConfig circuitBreakerConfig = circuitBreakerRegistry.find(id).isPresent() ? circuitBreakerRegistry.find(id).get().getCircuitBreakerConfig()
: circuitBreakerRegistry.getDefaultConfig();
TimeLimiterConfig timeLimiterConfig = timeLimiterRegistry.find(id).isPresent() ? timeLimiterRegistry.find(id).get().getTimeLimiterConfig()
: timeLimiterRegistry.getDefaultConfig();
return new Resilience4JConfigBuilder(id)
.circuitBreakerConfig(circuitBreakerConfig)
.timeLimiterConfig(timeLimiterConfig)
.build();
});
return reactiveResilience4JCircuitBreakerFactory;
}
英文:
the accepted answer works if you are not using R4J circuitbreakers or timelimiters...but if you do, the above settings will be insufficient and in fact will be overridden by the R4J settings. it is discussed here and here, the current workaround as of this writing can be found here...basically, you write a custom bean so it will honor the configuration settings:
@Bean
public ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory(final CircuitBreakerRegistry circuitBreakerRegistry, final TimeLimiterRegistry timeLimiterRegistry) {
ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory = new ReactiveResilience4JCircuitBreakerFactory();
reactiveResilience4JCircuitBreakerFactory.configureCircuitBreakerRegistry(circuitBreakerRegistry);
reactiveResilience4JCircuitBreakerFactory.configureDefault(id -> {
CircuitBreakerConfig circuitBreakerConfig = circuitBreakerRegistry.find(id).isPresent() ? circuitBreakerRegistry.find(id).get().getCircuitBreakerConfig()
: circuitBreakerRegistry.getDefaultConfig();
TimeLimiterConfig timeLimiterConfig = timeLimiterRegistry.find(id).isPresent() ? timeLimiterRegistry.find(id).get().getTimeLimiterConfig()
: timeLimiterRegistry.getDefaultConfig();
return new Resilience4JConfigBuilder(id)
.circuitBreakerConfig(circuitBreakerConfig)
.timeLimiterConfig(timeLimiterConfig)
.build();
});
return reactiveResilience4JCircuitBreakerFactory;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论