spring-cloud-gateway: reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response,springboot2.1.6

springboot2.1.6.RELEASE and Spring Cloud Greenwich.SR1

image

application.yml

spring: application: name: gateway-service cloud: gateway: default-filters: - StripPrefix=1 routes: - id: provider_api_route uri: lb://provider-service predicates: - Path=/web/** discovery: locator: enabled: true consul: enabled: true discovery: instanceId: ${spring.application.name}😒{vcap.application.instance_id:${spring.application.instance_id:${random.value}}} #prefer-ip-address: true retry: initial-interval: 10 multiplier: 1 max-interval: 30000 max-attempts: 15

my pom.xml

` <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-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>
</project> `

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 24 (11 by maintainers)

Most upvoted comments

@creaton60 have you tried to set the maxIdleTime property?

spring:
    gateway:
      httpclient:
        pool:
          max-idle-time: <PUT_YOUR_VALUE_HERE>ms

AFAIR by default, it is NULL so your connection will be waiting forever without closing it on the gateway and it may be closed on server site.

spring.cloud.gateway.httpclient.pool.max-idle-time: Time in millis after which the channel will be closed. If NULL, there is no max.

server.connection-timeout or connectionTimeout: The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented. Use a value of -1 to indicate no (i.e. infinite) timeout. The default value is 60000 (i.e. 60 seconds) but note that the standard server.xml that ships with Tomcat sets this to 20000 (i.e. 20 seconds). Unless disableUploadTimeout is set to false, this timeout will also be used when reading the request body (if any). keepAliveTimeout: The number of milliseconds this Connector will wait for another HTTP request before closing the connection. The default value is to use the value that has been set for the connectionTimeout attribute. Use a value of -1 to indicate no (i.e. infinite) timeout.

@Ziemowit Thank you very mutch. It solved my problem. The Spring Cloud Gateway project spring.cloud.gateway.httpclient.pool.max-idle-time defalut value was null (there is no max). my proxy target server was tomcat, server.connection-timeout default was 20000ms. My server was set to 100ms, 100ms was to short, it cause many PrematureCloseException.

My solution:

server.connection-timeout=5000
spring.cloud.gateway.httpclient.pool.max-idle-time=2000ms

Notice: spring.cloud.gateway.httpclient.pool.max-idle-time must be less than or server.connection-timeout

https://github.com/reactor/reactor-netty/issues/1092

Please open a new issue and provide the stack trace

Closed via 069f24d568678d3698bd9ac3a8ea7d8a1c0f1943

How do we recreate the problem?

We need step by step instructions and maybe a project that recreates the problem (not pasted code).

Please, don’t use screenshots.

How do we recreate the problem?