【问题标题】:Spring Cloud Gate Request Timeout Not working for pathSpring Cloud Gate 请求超时不适用于路径
【发布时间】:2021-03-04 04:55:34
【问题描述】:

在 Spring Cloud Gateway 中,路径未按预期工作的请求超时。

我尝试指定全局超时,但它没有按预期工作。

我正在尝试调用微服务 A。在微服务中,我已将延迟添加到 5 秒。

现在在路径中,如果我添加响应超时超过 5 秒,它应该等待响应,但当前断路器已打开并且当前它在 1000 毫秒内返回回退消息

我已经在网关中配置了 Resilience4j 断路器。

我无法理解为什么 Path 的响应超时和全局超时不起作用。

application.yml

server:
  port: 9090
spring:
  cloud:
    gateway:
      routes:
        - id: ms1_1
          uri: http://localhost:9091/
          predicates:
            - Path=/ms1/**
        - id: ms2
          uri: http://localhost:9092/
          predicates:
            - Path=/ms2/**
          metadata:
            response-timeout: 20000
            connect-timeout: 20000
          filters:
            - RewritePath=/ms2/(?<path>.*), /$\{path}
            - name: RequestRateLimiter
              args: 
               redis-rate-limiter.replenishRate: 2000
               redis-rate-limiter.burstCapacity: 4000
               key-resolver: "#{@userRemoteAddressResolver}"
            - name: CircuitBreaker
              args:
                name: backendA
                fallbackUri: forward:/fallback/ms2
            - name: Retry
              args:
                retries: 3
                statues: INTERNAL_SERVER_ERROR
                backoff:
                  firstBackoff: 50ms
                  maxBackoff: 500ms
                  factor: 2
                  basedOnPreviousValue: true
      
                 
               
  application:
    name: gateway-service
    
  redis:
    host: localhost
    port: 6379
          
management:
  endpoints:
    web:
      exposure:
        include:
        - '*'
  endpoint:
    health:
      show-details: always
  
      

resilience4j.circuitbreaker:
  configs:
    default:
      register-health-indicator: true
      sliding-window-size: 10
      minimum-number-of-calls: 5 
      permitted-number-of-calls-in-half-open-state: 3
      automatic-transition-from-open-to-half-open-enabled: true
      wait-duration-in-open-state: 10s
      failure-rate-threshold: 50
      slow-call-rate-threshold: 50
      slow-call-duration-threshold: 60000ms
      recordExceptions:
        - java.lang.IllegalArgumentException
        - org.springframework.web.client.HttpServerErrorException
        - java.io.IOException
        - java.lang.Exception
        - java.net.UnknownHostException
        - org.springframework.web.reactive.function.client.WebClientResponseException
        - org.springframework.web.reactive.function.client.WebClientResponseException$NotFound
        - org.springframework.web.client.HttpClientErrorException$NotFound
        - java.net.ConnectException
        - io.netty.channel.AbstractChannel$AnnotatedConnectException

        
      ignoreExceptions:
        - java.lang.IllegalStateException
  instances:
    backendA:
      baseConfig: default
        

        

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.BUILD-SNAPSHOT</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.portal</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.BUILD-SNAPSHOT</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <!-- needed for Resilience4j -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
           <groupId>io.github.resilience4j</groupId>
           <artifactId>resilience4j-spring-boot2</artifactId>
           <version>1.5.0</version>
       </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

【问题讨论】:

  • 可能您正在使用 TimeLimiterConfig 并将 timeoutDuration 的默认值设置为 1 秒。尝试配置它。
  • 不,我正在使用 TimeLimiterConfig ,我还注意到 response-timeout: 20000 在与断路器一起使用时不起作用
  • 似乎断路器使用的超时时间(默认 1000 毫秒)与为网关定义的超时时间不同。您可能只需要为弹性 4j 定义超时时间,根本不需要网关的超时时间(您仍然可以添加它,但那时并不是真的需要)。

标签: java spring spring-cloud spring-cloud-gateway


【解决方案1】:

我发现我们需要用 Circuit Breaker 定义 Timelimiter,默认超时时间是 1 秒

@Bean
    public ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory(
            CircuitBreakerRegistry circuitBreakerRegistry) {
        ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory = new ReactiveResilience4JCircuitBreakerFactory();
        reactiveResilience4JCircuitBreakerFactory.configureCircuitBreakerRegistry(circuitBreakerRegistry);
        
        TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
                .timeoutDuration(Duration.ofSeconds(1)).cancelRunningFuture(true)
                .build();
        reactiveResilience4JCircuitBreakerFactory.configure(builder -> builder.timeLimiterConfig(timeLimiterConfig).build(),"backendA","backendB");
        return reactiveResilience4JCircuitBreakerFactory;
    }

【讨论】:

    猜你喜欢
    • 2014-09-14
    • 2017-10-04
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多