简体   繁体   English

Spring Boot 2反应式webflux

[英]Spring Boot 2 reactive webflux

I'm approaching Spring Boot 2 and its reactive way to implement web services. 我正在接近Spring Boot 2及其实现Web服务的反应方式。 As almost everybody who is used to program with classic synchronous MVC pattern, I have some doubts about this approach. 几乎所有习惯使用经典同步MVC模式编程的人都对这种方法有所怀疑。 I'm trying to implement a restcontroller and to develop some reactive and non-reactive methods as title of example, in order to understand better the concepts. 我正在尝试实现一个restcontroller并开发一些被动和非被动方法作为示例的标题,以便更好地理解这些概念。 For instance, importing WebFlux (which uses Netty as Embedded Server) in the way they are written, are the second and the fourth methods reactive, while the first and the third method non-reactive? 例如,以编写方式导入WebFlux(使用Netty作为嵌入式服务器)是第二种和第四种反应方法,而第一种和第三种方法是非反应性的?

@org.springframework.web.bind.annotation.RestController
public class RestController {

  @Autowired
  NoteDao noteDao;

  /* non-reactive */
  @RequestMapping("/hello")
  public String sayHello(){
    return "Hello pal";
  }

  /* reactive */
  @RequestMapping("/hello/reactive")
  public Mono<String> sayHelloReactively(){
    return Mono.just("Hello reactively, pal!");
  }

  /* non-reactive */
  @RequestMapping("/notes")
  public List<Note> getAllNotes(){
    return noteDao.findAll();
  }

  /* reactive */
  @RequestMapping("/notes/reactive")
  public Flux<List<Note>> getAllNotesReactively(){
    return Flux.just(noteDao.findAll());
  }

}

sayHello and sayHelloReactively can be both considered as reactive - you're basically returning something from memory, no I/O is involved. sayHellosayHelloReactively可以被认为是被动的 - 你基本上是从内存中返回一些东西,不涉及I / O. The second one wraps the String value for no reason - both methods are equivalent. 第二个无缘无故地包装String值 - 两个方法都是等价的。

The core idea behind reactive is that for everything that involves I/O operations (reading and writing from the network, for example), your code should not sit doing nothing while waiting for the result (REST call, database query) to come back. 被动反应背后的核心思想是,对于涉及I / O操作(例如,从网络读取和写入)的所有内容,您的代码在等待结果(REST调用,数据库查询)返回时不应该无所事事。 You can think of it as callbacks, plus many additional features like backpressure. 您可以将其视为回调,以及背压等许多其他功能。

Because NoteDao exposes synchronous+blocking methods, you can't call one method and be notified as soon as the result comes. 因为NoteDao公开了同步+阻塞方法,所以不能调用一个方法并在结果出现时立即通知。 Wrapping a blocking call with Flux.just will just make things worse. Flux.just包装一个阻塞电话会让事情变得更糟。 In a WebFlux application, there are very few threads - and doing that will block one for each call - it is then pretty easy to completely crash your application. 在WebFlux应用程序中,只有很少的线程 - 这样做会阻止每个调用一个 - 然后很容易完全崩溃你的应用程序。

If you want to understand more the core ideas behind blocking vs. reactive, you should check out this talk: Servlet or Reactive Stacks: The Choice is Yours. 如果你想更多地了解阻塞与被动背后的核心思想,你应该看看这个话题: Servlet或Reactive Stacks:选择是你的。 Oh No... The Choice is Mine! 哦不...选择是我的! - Rossen Stoyanchev . - Rossen Stoyanchev

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

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