如何在Spring Cloud关定个时间出来?

hjzp0vay  于 2022-11-23  发布在  Spring
关注(0)|答案(2)|浏览(122)

如何在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设置超时。

nwnhqdif

nwnhqdif1#

参考Spring文件
设置global time-out

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;
}
ohfgkhjo

ohfgkhjo2#

  • 如果 * 您没有使用R4J断路器或时间限制器,则可接受的答案有效...但如果您使用R4J断路器或时间限制器,则上述设置将不充分,实际上将被R4J设置覆盖。我们将讨论herehere,在此处可以找到截至本文撰写时的当前解决方案...基本上,您编写一个自定义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;
}

相关问题