脱离源码理解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
- 流程图如下
![在这里插入图片描述](https://img-blog.csdnimg.cn/c48952c4b24b48d183d1e6750d2a54f3.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) {
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;
});
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.submit(() -> {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
sink.success("hello");
});
}).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]