在Spring Cloud Gateway中如何设置超时时间?

huangapple go评论84阅读模式
英文:

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));

有没有一种方法只为一个ROUTEdoc)设置超时?

英文:

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

huangapple
  • 本文由 发表于 2020年8月7日 19:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63300986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定