spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.52k stars 3.32k forks source link

Does spirng-cloud-gateway use blocking I/O when forwarding requests? #2255

Closed YunWZ closed 3 years ago

YunWZ commented 3 years ago

When I send 40 requests at the same time by Jmeter,i found that spring-cloud-gateway can only forward 12 requests at the same time(the gateway server has 12 "reactor-http-nio-*" threads) and the remaining requests will not be processed until proxied server return responses.

In my proxied server, i defined an api:

@RestController
public class DemoController {
    private AtomicInteger integer = new AtomicInteger(1);

    @GetMapping("/sleep")
    public Mono<Void> sleep() throws InterruptedException {
        System.out.println(integer.getAndIncrement());
        Thread.sleep(TimeUnit.SECONDS.toMillis(60L));
        return Mono.empty();
    }
}

In my gateway server,i configured route:

spring:
  cloud:
    gateway:
      routes: 
        - id: all
          uri: http://proxiedserver
          predicates:
            - Path=/**
spencergibb commented 3 years ago

It does not use blocking io. It uses reactor-netty HttpClient underneath. Can you describe your setup more in depth?

YunWZ commented 3 years ago

It does not use blocking io. It uses reactor-netty HttpClient underneath. Can you describe your setup more in depth?

I create a spring boot application(reactor web service,the spring boot version is 2.3.7.RELEASE, server.port=8081), and defined a restful api. For example:

@RestController
public class DemoController {
    private AtomicInteger times = new AtomicInteger(1);

    @GetMapping("/demo/sleep")
    public Mono<Void> sleep() throws InterruptedException {
        System.out.println("get request:" + times.getAndIncrement());
        Thread.sleep(TimeUnit.SECONDS.toMillis(60L)); //This is to simulate a long poll request
        System.out.println("end");
        return Mono.empty();
    }
}

And then created a spring-cloud-gateway application(the spring boot version is 2.3.7.RELEASE, spring-cloud version is Hoxton.SR11, spring-cloud-starter-gateway version is 2.2.8.RELEASE, server.port=8080).I defined a route like this:

spring:
  cloud:
    gateway:
      routes:
        - id: toProxiedService
          uri: http://localhost:8081
          order: 1
          predicates:
            - Path=/**

After running these two services,I send 40 requests(http:/localhost:8080/demo/sleep) to gateway service at the same time by Jmeter,i expacted that the gateway service will forward all requests to the proxied service, but in fact the gateway can only forward 12 requests at the same time