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

脱离源码理解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
  • 流程图如下

在这里插入图片描述

模拟实现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);
        }
    }

}

  • 执行结果
Thread[#1,main,5,main]
main end
Thread[#31,pool-1-thread-1,5,main]
Thread[#31,pool-1-thread-1,5,main]
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值