reactor / reactor-netty

TCP/HTTP/UDP/QUIC client/server with Reactor over Netty
https://projectreactor.io
Apache License 2.0
2.6k stars 645 forks source link

Connection prematurely closed BEFORE response #2876

Closed a13519 closed 1 year ago

a13519 commented 1 year ago

I have 2 services: order service and product service, the order service will call product service simply to retrieve all the products. I can call product service by either PostMan and Java standalone app and get response without issue.

  1. A Spring Cloud API gateway was used with OAuth2.0
  2. org.springframework.boot version 3.0.9
  3. spring-cloud-dependencies version 2022.0.3

build.gradle:

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    compileOnly 'org.projectlombok:lombok:1.18.28'
    runtimeOnly 'com.mysql:mysql-connector-j'
    annotationProcessor 'org.projectlombok:lombok'
    runtimeOnly 'org.springframework.boot:spring-boot-starter-tomcat'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
ext {
    set('springCloudVersion', "2022.0.3")
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

application.properties:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://10.10.50.90:3306/order-service
    username: root
    password: mysqlpass
  jpa.hibernate.ddl-auto: update
  application:
    name: order-service
  cloud:
    gateway:
      httpclient:
        pool:
          max-idle-time: 50s
server:
  port: 0

eureka:
  client:
    serviceUrl:
      defaultZone: http://apiuser:passit@10.10.50.90:8761/eureka
    register-with-eureka: true
    fetch-registry: true
  instance:
    instance-id: ${spring.application.name}-${random.int}

management:
  info:
    env:
      enabled: true
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"
  health:
    circuitbreakers:
      enables: true

resilience4j:
  circuitbreaker:
    instances:
      inventory:
        registerHeaathIndicator: true
        event-consumer-buffer-size: 10
        slidingWindowType: COUNT_BASED
        slidingWindowSize: 5
        failureRateThreshold: 50
        waitDurationInOpenState: 5s
        permittedNumberOfCallsInHalfOpenState: 3
        automaticTransitionFromOpenToHalfOpenEnabled: true

Service Code:

String mono = webClient.get().uri("http://product-service/api/product")
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToMono(String.class)
                .block();

It throws the exception:

reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response

if I access api gateway directly by

String mono = webClient.get().uri("http://10.10.50.90:8080/api/product")
                .accept(MediaType.APPLICATION_JSON)
                .header("Authorization", token)
                .retrieve()
                .bodyToMono(String.class)
                .block();

It succeed.

How to fix this?

Thanks!

violetagg commented 1 year ago

@a13519 Your example includes too many other libraries. Try to simplify it to a reproducible example only with Reactor Netty. Also review our FAQ https://projectreactor.io/docs/netty/release/reference/index.html#faq.connection-closed

a13519 commented 1 year ago

Hi violetagg,

Thanks for your reply and your manual gives me the hint. after a serial of tests I figure out it is because of something in the middle, which I put eureka and api gateway on a machine and run the service on my laptop. After I deploying the service on the target machine the issue was gone.

Thanks a lot! Song