意外的取消信号和 503 响应代码

Mar*_*pel 6 spring-boot spring-webflux

当使用 Spring Webflux 处理我的反应式 REST 端点时,我意识到,如果返回的内容Publisher在大约 30 秒内没有完成,它将被取消并返回响应代码 503。

我还没有在任何地方看到这种行为的记录。

REST 控制器示例:

package myapp.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;

@RestController
public class TestAlationIndexingRestController {

    @RequestMapping(
        value = "/test",
        method = RequestMethod.GET
    )

    public Mono<String> test(HttpServletRequest request) {

        return Mono.delay(Duration.ofMinutes(2)).thenReturn("Late");
    }
}

Run Code Online (Sandbox Code Playgroud)

响应:503 无响应内容

我的问题:如何禁用此行为,或者至少增加超时?

注意:我尝试使用应用程序属性server.connection-timeout: -1,但server.connection-timeout: 15没有成功 - 请求在 30 秒多一点后仍然超时。


我正在使用 Spring Boot 依赖项(版本2.1.6-RELEASE):

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-reactor-netty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
            <version>3.2.12.RELEASE</version>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

Sal*_*ndo 1

我已经被这个问题困扰好几天了。最后我找到了解决方案。

首先在application.yml上添加异步超时配置

 spring:
  mvc:
    async:
      request-timeout: 60s
Run Code Online (Sandbox Code Playgroud)

如果以上配置不起作用。然后尝试在java代码中添加以下配置

@Configuration
@EnableWebMvc
public class ConfigurerAdapter implements WebMvcConfigurer {
    @Override
    public  void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(3600000);
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以从这里获得一些线索https://docs.spring.io/spring-framework/docs/5.0.2.RELEASE/kdoc-api/spring-framework/org.springframework.web.context.request.async/ -超时延迟结果处理拦截器/index.html