简体   繁体   English

使用 Mono 和 vanilla Java 编程的反应式编程之间的区别?

[英]Difference between Reactive programming using Mono and vanilla Java programming?

I tried creating two api endpoints, one which is reactive and another is normal function(don't know what we call it, do let me know if you know:p) So according to theory, if I hit the Reactive end point two-three times at the same time which returns Mono<String> with time variable say 10000ms , then the app behave as it should, it doesn't block and run on separate threads which is expected.我尝试创建两个 api 端点,一个是反应性的,另一个是正常功能的(不知道我们怎么称呼它,如果你知道,请告诉我:p)所以根据理论,如果我达到反应性端点两个 -三次同时返回Mono<String>time变量为10000ms ,然后应用程序正常运行,它不会阻塞并在预期的单独线程上运行。 But with the normal function also, the behaviour is same, why so?但是对于正常的 function 也一样,行为是一样的,为什么会这样? Shouldn't it block the thread and block the other api call until the first one returns?它不应该阻塞线程并阻塞另一个 api 调用,直到第一个调用返回?

I was trying to see how does Mono&Flux practically achieve asynchronous behaviour?我试图了解Mono&Flux是如何实现异步行为的? How does it really make a difference?它如何真正有所作为?


 @GetMapping(path = "/testing")
  public String testing(@RequestParam(value = "time") Long time) throws InterruptedException {
    log.info("Thread {}", Thread.currentThread().getName());
    Thread.sleep(time);
    return "RETURNING" + String.valueOf(time);
  }

  @GetMapping(path = "/testingFlux")
  public Mono<String> testinging(@RequestParam(value = "time") Long time) throws InterruptedException {
    log.info("Thread {}", Thread.currentThread().getName());
    Thread.sleep(time);
    return Mono.just("RETURNING" + String.valueOf(time));
  }

What you observe is Spring handling multiple Threads anyways in order to respond quickly and asynchronously/parallel to incoming requests.您观察到的是 Spring 无论如何都要处理多个线程,以便快速和异步/并行响应传入请求。

There are are always multiple Worker-Threads handling requests at the same time.总是有多个工作线程同时处理请求。 However "the difference" between the traditional and the reactive approach become greater under the hood.然而,传统方法和被动方法之间的“差异”在幕后变得更大。

You usually design an application to be reactive from ground to top, meaning it starts with a reactive data persistence over reactive domain model, reactive logic code and of course an api designed to work with reactive code.您通常将应用程序设计为从头到尾都是响应式的,这意味着它从响应式域 model 上的响应式数据持久性开始,响应式逻辑代码,当然还有设计用于处理响应式代码的 api。

Eventually it all boils down to: »You did not look deep enough into the rabbit hole, to see "the difference".« In fact there are plenty of concepts, which are a "must have" for reactive applications like often mentioned being "message driven", "resilient", "elastic".最终,这一切都归结为:»您没有深入了解兔子洞,看不到“差异”。« 实际上有很多概念,对于反应式应用程序来说,它们是“必须具备的”,例如经常提到的“消息驱动”、“弹性”、“弹性”。 However you must take a deeper dive into programming a reactive application, to discover them.但是,您必须更深入地研究响应式应用程序的编程,才能发现它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM