脱离源码谈谈spring-flux+webclient线程模型

462 阅读1分钟

脱离源码理解spring-flux+webclient线程模型

线程模型

  • 请求进入web容器到再使用webclient请求第二方或者三方restful接口整个过程都是异步化
    • 1.客户端长连conn1(请求request) 进入spring-flux netty的work线程处理相关业务。
    • 2.切换到webclient的netty的work线程调用接口。
    • 3.接口返回的数据(具体业务eg:json序列化)写入处理后的数据(response)到spring-flux netty fd,再通过conn1将数据写出到客户端client
  • 流程图如下

image.png

模拟实现spring-flux+webclient线程模型

  • 以下代码可以帮助理解spring-flux +webclient线程模型,执行打印线程结果可以看出
  • 以下代码的执行流程也是spring-flux +webclient实际的执行流程
public class ReactorTest {
    static ExecutorService executors = new ThreadPoolExecutor(2, 10,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());

    public static void main(String[] args) {
        //模拟webclient
        Mono mono0 = new MonoTest().flatMap(v -> {
            //在此可以编写业务处理代码
            System.out.println(Thread.currentThread());
            return Mono.just(v);
        });

        Mono mono1 = Mono.just("").flatMap(v -> {
            //在此可以编写业务处理代码
            System.out.println(Thread.currentThread());
            return mono0;
        }).flatMap(v -> {
            System.out.println(Thread.currentThread());
            return v;
        });

        //模拟spring-flux netty的work线程
        mono1.subscribe();

        try {
            System.out.println("main end");
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    static class MonoTest extends Mono {

        @Override
        public void subscribe(CoreSubscriber actual) {
            Mono.create(sink -> {
               //executors线程模拟webclient的work线程
                executors.submit(() -> {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    sink.success("hello");
                });
            }).subscribe(actual);
            //Mono.just("hello").flatMap(v->Mono.just(v))/*.subscribeOn(Schedulers.elastic())*/.subscribe(actual);
        }
    }

}

  • 执行结果
  • 从spring-flux线程切换到webclient线程
Thread[#1,main,5,main]
main end
Thread[#31,pool-1-thread-1,5,main]
Thread[#31,pool-1-thread-1,5,main]