日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【Spring 5】响应式Web框架前瞻

發布時間:2024/1/17 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Spring 5】响应式Web框架前瞻 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 響應式宣言

和敏捷宣言一樣,說起響應式編程,必先提到響應式宣言。

We want systems that are Responsive, Resilient, Elastic and Message Driven. We call these Reactive Systems. -?The Reactive Manifesto

圖片出處:The Reactive Manifesto

不知道是不是為了向敏捷宣言致敬,響應式宣言中也包含了4組關鍵詞:

  • Responsive: 可響應的。要求系統盡可能做到在任何時候都能及時響應。
  • Resilient: 可恢復的。要求系統即使出錯了,也能保持可響應性。
  • Elastic: 可伸縮的。要求系統在各種負載下都能保持可響應性。
  • Message Driven: 消息驅動的。要求系統通過異步消息連接各個組件。

可以看到,對于任何一個響應式系統,首先要保證的就是可響應性,否則就稱不上是響應式系統。從這個意義上來說,動不動就藍屏的Windows系統顯然不是一個響應式系統。

PS: 如果你贊同響應式宣言,不妨到官網上留下的你電子簽名,我的編號是18989,試試看能不能找到我。

2 Spring 5前瞻

作為Java世界首個響應式Web框架,Spring 5最大的亮點莫過于提供了完整的端到端響應式編程的支持。

圖片出處:Spring Framework Reference Documentation

左側是傳統的基于Servlet的Spring Web MVC框架,右側是5.0版本新引入的基于Reactive Streams的Spring WebFlux框架,從上到下依次是Router Functions,WebFlux,Reactive Streams三個新組件。

  • Router Functions: 對標@Controller,@RequestMapping等標準的Spring MVC注解,提供一套函數式風格的API,用于創建Router,Handler和Filter。
  • WebFlux: 核心組件,協調上下游各個組件提供響應式編程支持。
  • Reactive Streams: 一種支持背壓(Backpressure)的異步數據流處理標準,主流實現有RxJava和Reactor,Spring WebFlux默認集成的是Reactor。

在Web容器的選擇上,Spring WebFlux既支持像Tomcat,Jetty這樣的的傳統容器(前提是支持Servlet 3.1 Non-Blocking IO API),又支持像Netty,Undertow那樣的異步容器。不管是何種容器,Spring WebFlux都會將其輸入輸出流適配成Flux<DataBuffer>格式,以便進行統一處理。

值得一提的是,除了新的Router Functions接口,Spring WebFlux同時支持使用老的Spring MVC注解聲明Reactive Controller。和傳統的MVC Controller不同,Reactive Controller操作的是非阻塞的ServerHttpRequest和ServerHttpResponse,而不再是Spring MVC里的HttpServletRequest和HttpServletResponse。

下面是我GitHub上的示例工程里的一個例子,

@RestController public class RestaurantController {private final RestaurantRepository restaurantRepository;private final ReactiveMongoTemplate reactiveMongoTemplate;public RestaurantController(RestaurantRepository restaurantRepository, ReactiveMongoTemplate reactiveMongoTemplate) {this.restaurantRepository = restaurantRepository;this.reactiveMongoTemplate = reactiveMongoTemplate;}@GetMapping("/reactive/restaurants")public Flux<Restaurant> findAll() {return restaurantRepository.findAll();}@GetMapping("/reactive/restaurants/{id}")public Mono<Restaurant> get(@PathVariable String id) {return restaurantRepository.findById(id);}@PostMapping("/reactive/restaurants")public Flux<Restaurant> create(@RequestBody Restaurant[] restaurants) {return Flux.just(restaurants).log().flatMap(r -> Mono.just(r).subscribeOn(Schedulers.parallel()), 10).flatMap(reactiveMongoTemplate::insert);} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

3 小結

除了響應式編程支持,Spring 5還包括了很多Java程序員期待已久的特性,包括JDK 9,Junit 5,Servlet 4以及HTTP/2支持。目前Spring 5的最新版本是RC1,而Spring Boot也剛剛發布了2.0.0 M1版本。根據Spring官方博客,Spring 5將在JDK 9?GA之后隨即發布,也就是今年的7月底前后。

以上就是我對Spring 5中有關響應式編程支持的一些簡單介紹,歡迎你到我的留言板分享,和大家一起過過招。下一篇我將聊一下我對響應式編程的一些理解,敬請期待。

4 參考

  • New in Spring 5: Functional Web Framework
  • Spring Framework 5 - Preview & Roadmap
  • Spring Framework 5: History and Reactive features

總結

以上是生活随笔為你收集整理的【Spring 5】响应式Web框架前瞻的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。